A python logging implementation to send messages to Slack. Design goals:
- Use Python logging interface such that it can be integrated with standard logging tools.
- Fully customizable messages with full control over message layout and design.
- Simple authentication via webhook.
- Powerful filtering to filter e.g. for environments or particular values only known at runtime.
- Easy usage to cover most use cases when slack is used for basic alerting and automatic traceback visualization.
To achieve aforementioned goals, the library provides logging handler, formatter and filter implementations.
- Install with
pip install slack-logger-python
. - Now, you can use the
slack_logger
module in your python code.
This basic examples shows the usage and the implementation of design goal (1) and (3).
from slack_logger import SlackFormatter, SlackHandler
logger = logging.getLogger(__name__)
formatter = SlackFormatter.plain() # plain message, no decorations
handler = SlackHandler.from_webhook(os.environ["SLACK_WEBHOOK"])
handler.setFormatter(formatter)
handler.setLevel(logging.WARN)
logger.addHandler(handler)
logger.info("I won't appear.")
logger.warning("I will show up.")
logger.error("Mee too.")
You can use the SlackFormatter.minimal()
and SlackFormatter.default()
formatter for more visually appealing log messages.
For now, those require a configuration to show e.g. the header.
Extra fields are shown in blocks at the bottom of the message and can be dynamically added at runtime.
Everything else stays the same:
from slack_logger import FormatConfig, SlackFormatter, SlackHandler
logger = logging.getLogger(__name__)
format_config = FormatConfig(service="testrunner", environment="test", extra_fields={"foo": "bar"})
formatter = SlackFormatter.default(format_config)
handler = SlackHandler.from_webhook(os.environ["SLACK_WEBHOOK"])
handler.setFormatter(formatter)
handler.setLevel(logging.WARN)
logger.addHandler(handler)
logger.info("I won't appear.")
logger.warning("I will show up.")
logger.error("Mee too.")
Adding extra fields on single log message is achieved by just putting it in the extra fields of the logging interface:
logger.warning("I will show up.", extra = {"extra_fields": {"foo": "baba"}})
To do basic customizations, you can provide a configuration to the SlackFormatter
:
service: Optional[str] = None
environment: Optional[str] = None
context: List[str] = []
emojis: Dict[int, str] = DEFAULT_EMOJIS
extra_fields: Dict[str, str] = {}
Let's look at an example error log from a division by zero error. Given the following code snippet with a configuration and the default formatter:
import os
from slack_logger import FormatConfig, SlackFormatter, SlackHandler
format_config = FormatConfig(
service="testrunner", environment="test", extra_fields={"foo": "bar", "raven": "caw"}
)
slack_handler = SlackHandler.from_webhook(os.environ["SLACK_WEBHOOK"])
formatter = SlackFormatter.default(format_config)
slack_handler.setFormatter(formatter)
log.addHandler(slack_handler)
try:
1/0
except Exception:
log.exception("Something terrible happened!")
We will get the following error message in slack:
It contains a header, a context, a body and a section containing extra fields. Let's identify those in the image above.
The header is composed of:
- Log level emoji: β
- Log level name: ERROR
- Service name: testrunner
The context contains:
- Environment: test
- Service name: testrunner
The body includes:
- The log error message: "Something terrible happened!"
- The Traceback error
Extra fields:
- Field "foo" with value "bar"
- Field "raven" with value "caw"
Messages are fully customizable using slacks block layout, see Creating rich message layouts and Reference: Layout blocks.
By implementing the MessageDesign
interface, you can fully control in the message design, which requires you to implement a function format_blocks(record: LogRecord) -> Sequence[Optional[Block]]
to transform a LogRecord
into a sequence of slack message blocks.
Of course, you can add configurations and helper functions as well.
Let's create our own warning message. This demonstrates the usage and the implementation of design goal (2).
import attrs # for convenience, but not required
from slack_sdk.models.blocks import Block, DividerBlock, HeaderBlock, SectionBlock
from slack_logger import SlackFormatter
@define
class CustomDesign(MessageDesign):
def format_blocks(self, record: LogRecord) -> Sequence[Optional[Block]]:
level = record.levelname
message = record.getMessage()
blocks: Sequence[Block] = [
HeaderBlock(text=PlainTextObject(text=f"{level} {message}")),
DividerBlock(),
SectionBlock(text=MarkdownTextObject(text=message)),
]
return blocks
formatter = SlackFormatter(design=CustomDesign())
To the default emoji set is a defined by the following dict:
DEFAULT_EMOJIS = {
logging.CRITICAL: ":fire:", # π₯
logging.ERROR: ":x:", # β
logging.FATAL: ":x:", # β
logging.WARNING: ":warning:", # β οΈ
logging.WARN: ":warning:", # β οΈ
logging.INFO: ":bell:", # π
logging.DEBUG: ":microscope:", # π¬
logging.NOTSET: ":mega:", # π£
}
You can import and overwrite it partially - or you can define a complete new set of emoji.
The following example demonstrates how you can add the emoji set to the SlackFormatter
:
from slack_logger import FormatConfig, SlackFormatter
my_emojis = {
logging.CRITICAL: ":x:", # β
logging.ERROR: ":x:", # β
logging.FATAL: ":x:", # β
logging.WARNING: ":bell:", # π
logging.WARN: ":bell:", # π
logging.INFO: ":mega:", # π£
logging.DEBUG: ":mega:", # π£
logging.NOTSET: ":mega:", # π£
}
config = FormatConfig(service="testrunner", environment="test", emojis=my_emojis)
formatter = SlackFormatter.default(config)
Filters implement the logging interface of Filters
.
They are designed to work as a companion to a LogFormatter
, as it can filter on the formatters config.
A message is logged if a filter is matched successfully.
Design goal (4) and (5) are partially demonstrated.
Here is a quick example:
from slack_logger import FilterConfig, SlackFilter, SlackHandler
import os
import logging
logger = logging.getLogger("ProdFilter")
# Allow only logs from `prod` environment
filter = SlackFilter(config=FilterConfig(environment="prod"), filterType=FilterType.AnyAllowList)
slack_handler.addFilter(filter)
logger.addHandler(slack_handler)
# When the ENV enviroment variable is set to prod, the message will be send.
# Otherwise, the message is filtered out and not send (e.g. if ENV is `dev`)
logger.warning(f"{log_msg} in some environment and allow listed prod", extra={"filter": {"environment": os.getenv("ENV", "dev")}})
# Will be filtered
logger.warning(f"{log_msg} in dev environment and allow listed prod", extra={"filter": {"environment": "dev"}})
# Will be send
logger.warning(f"{log_msg} in dev environment and allow listed prod", extra={"filter": {"environment": "prod"}})
Note that we used the "filter"
property in the extra
field option here to inject a config, as we don't use a SlackFormatter
.
You can think of it as a reserved word.
This on-the-fly
configurations allow to specify properties on messages to alter the filter behavior for a single log message.
There are 4 different types of filter lists:
FilterType.AnyAllowList
: Pass filter if any of the provided conditions are met.FilterType.AllAllowList
: Pass filter only if all provided conditions are met.FilterType.AnyDenyList
: Pass filter if no condition is met and deny if any condition is met.FilterType.AllDenyList
: Pass filter if any condition is met and deny if all conditions are met.
It is important to note that as soon a message does not meet any filter condition, it is filtered out and won't appear in the logs, as it is simply the overlap of all filter conditions. Therefore it is not possible to allow a denied message afterwards. Furthermore, the order of filters do not matter.
The composition of configurations, filters and dynamic extra fields allow for a flexible way of specifying your message content and filter unwanted messages.
More examples can be found it the tests folder.