Coverage for /home/ubuntu/flatiron/python/flatiron/core/logging.py: 100%
9 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 21:55 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-08 21:55 +0000
1from typing import Any, Optional # noqa F401
3import lunchbox.tools as lbt
5import flatiron.core.tools as fict
6# ------------------------------------------------------------------------------
9class SlackLogger(lbt.LogRuntime):
10 '''
11 SlackLogger is a class for logging information to stdout and Slack.
12 '''
13 def __init__(
14 self,
15 message, # type: str
16 config, # type: dict
17 slack_channel=None, # type: Optional[str]
18 slack_url=None, # type: Optional[str]
19 timezone='UTC', # type: str
20 level='warn',
21 **kwargs, # type: Any
22 ):
23 # type: (...) -> None
24 '''
25 SlackLogger is a class for logging information to stdout and Slack.
27 If slack_url and slack_channel are specified, SlackLogger will
28 attempt to log custom formatted output to Slack.
30 Args:
31 message (str): Log message or Slack title.
32 config (dict): Config dict.
33 slack_channel (str, optional): Slack channel name. Default: None.
34 slack_url (str, optional): Slack URL name. Default: None.
35 timezone (str, optional): Timezone. Default: UTC.
36 level (str or int, optional): Log level. Default: warn.
37 **kwargs (optional): LogRuntime kwargs.
38 '''
39 super().__init__(message=message, level=level, **kwargs)
41 if slack_channel is not None and slack_url is not None:
42 self._message_func = lambda _, stp: fict.slack_it(
43 title=message,
44 channel=str(slack_channel),
45 url=str(slack_url),
46 config=config,
47 stopwatch=stp,
48 timezone=timezone,
49 suppress=True,
50 ) # type: Any
51 self._callback = lambda msg: lbt.post_to_slack(
52 slack_url,
53 slack_channel,
54 msg,
55 )