Skip to content

Use custom log filter to send warning logs and above to slack#134

Open
bram-vdberg wants to merge 6 commits intomainfrom
Fix-Info-Alerts
Open

Use custom log filter to send warning logs and above to slack#134
bram-vdberg wants to merge 6 commits intomainfrom
Fix-Info-Alerts

Conversation

@bram-vdberg
Copy link

@bram-vdberg bram-vdberg commented Feb 17, 2025

This PR updates the logging to send warning logs and above to slack for monitoring.

@github-actions
Copy link

github-actions bot commented Feb 17, 2025

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@bram-vdberg
Copy link
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Feb 17, 2025
@bram-vdberg bram-vdberg changed the title Use custom log filter to send info to stdout and error to stderr Use custom log filter to send warning logs and above to slack Feb 17, 2025
@bram-vdberg bram-vdberg requested review from fhenneke and harisang and removed request for fhenneke February 18, 2025 08:17
"""
self._logger.critical(msg, *args, **kwargs)

def _post_to_slack(self, msg: str) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that function be used e.g. in error and critical (and warning)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should, sorry about that.

"""
Info logs
"""
self._logger.info(msg, *args, **kwargs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not sent to stderr? Or did the config of which messages appear on slack change somehow?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we changed the config so that doesn't matter anymore. But that also means that warning, error, and critical logs are no longer sent to slack automatically.

"""
if self.slack_client:
self.slack_client.chat_postMessage(
channel=os.environ.get("SLACK_CHANNEL", "#alerts-ebbo"), text=msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a good idea to use channel names instead of channel ids 👍
We do the latter in other repos but it is always a pain to reconstruct where messages are sent.

@fhenneke
Copy link
Contributor

One alternative would be the following, which uses python loggers directly.

class SlackHandler(logging.Handler):
    def __init__(self, name):
        super().__init__(name)
        self.slack_client = WebClient(token=os.environ["SLACK_BOT_TOKEN"])

    def emit(self, record):
        if record.level >= logging.WARNING:
            self._post_to_slack(record.msg)

    def _post_to_slack(self, msg):
        """Post log to slack"""
        self.slack_client.chat_postMessage(
            channel=os.environ.get("SLACK_CHANNEL", "#alerts-ebbo"), text=msg
        )

def get_logger(filename: Optional[str] = None) -> logging.Logger:
    ...
    if "SLACK_BOT_TOKEN" in os.environ:
        std_handler = StreamHandler()
        ...
        slack_handler = SlackHandler()
        ...
        self._logger.addHandler(std_handler)
        self._logger.addHandler(slack_handler)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants