Skip to main content

Python `logging.Formatter` class for syslog style messages

Project description

syslogformat

Python logging.Formatter class for syslog style messages

GitHub last commit License: Apache-2.0 PyPI version

📑 Documentation   |   🧑‍💻 Source Code   |   🐛 Bug Tracker

Installation

pip install syslogformat

Usage

Basic configuration

As is the case with any logging formatter setup, you need to use the special () key to indicate the custom class to use. (See the Dictionary Schema Details and User-defined objects sections in the official logging.config documentation.)

For example, you could use the following config dictionary, pass it to the logging.config.dictConfig function, and start logging like this:

import logging.config

log_config = {
    "version": 1,
    "formatters": {
        "my_syslog_formatter": {
            "()": "syslogformat.SyslogFormatter",
        }
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "my_syslog_formatter",
            "stream": "ext://sys.stdout",
        }
    },
    "root": {"handlers": ["console"], "level": "DEBUG"},
}
logging.config.dictConfig(log_config)

logging.debug("foo")
logging.info("bar")
logging.warning("baz")
try:
    raise ValueError("this is bad")
except ValueError as e:
    logging.exception("oof")

This will send the following to your stdout:

<15>foo | root
<14>bar | root
<12>baz | root
<11>oof | root --> Traceback (most recent call last): --> File "/path/to/module.py", line 26, in <module> --> raise ValueError("this is bad") --> ValueError: this is bad

The PRI prefix

To adhere to the syslog standard outlined in RFC 3164, every log message must begin with the so called PRI part. This is a code enclosed in angle brackets that indicates the facility generating the message and severity of the event. The facility is encoded as an integer between 0 and 23 and the severity is encoded as an integer between 0 and 7. The PRI code is calculated by multiplying the facility by 8 and adding the severity.

Programs like systemd-journald hide the PRI part in their output, but interpret it behind the scenes to allow things like highlighting messages of a certain level a different color and filtering by severity.

By default the facility code 1 is used, which indicates user-level messages, but this can be easily configured (see below). Since a DEBUG log message corresponds to a severity of 7, the resulting PRI part of the first log message in the example above is <15> (since 1 * 8 + 7 == 15). An ERROR has the severity 3, so that message has the PRI part <11>.

Default message format

By default the message format of the SyslogFormatter is %(message)s | %(name)s (and equivalent for $ or { styles).

In addition, all line-breaks (including those in the exception traceback) are replaced with --> by default.

All of this can be easily changed and configured to fit your needs (see below).

Configuration options

In addition to the usual formatter options, the SyslogFormatter provides the following parameters:

Parameter Description Default
facility The facility value to use for every log message 1
line_break_repl To prevent a single log message taking up more than one line, every line-break (and consecutive whitespace) is replaced with this string. Passing None disables this behavior. -->
level_formats If provided a mapping of log level thresholds to format strings, the formatter will prioritize the format with the highest level threshold for all log records at or above that level. None

For more details, check the API of the SyslogFormatter constructor in the documentation.

Extended configuration example

Here is an example using a custom message format and specifying a different facility and line break replacement:

import logging.config

log_config = {
    "version": 1,
    "formatters": {
        "my_syslog_formatter": {
            "()": "syslogformat.SyslogFormatter",
            "format": "{levelname:<8}{message} [{name}]",
            "style": "{",
            "facility": 16,
            "line_break_repl": " 🚀 ",
        }
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "my_syslog_formatter",
            "stream": "ext://sys.stdout",
        }
    },
    "root": {"handlers": ["console"], "level": "DEBUG"},
}
logging.config.dictConfig(log_config)

logging.debug("foo")
logging.info("bar")
logging.warning("baz")
try:
    raise ValueError("this is bad")
except ValueError as e:
    logging.exception("oof")

Output:

<135>DEBUG   foo [root]
<134>INFO    bar [root]
<132>WARNING baz [root]
<131>ERROR   oof [root] 🚀 Traceback (most recent call last): 🚀 File "/path/to/module.py", line 30, in <module> 🚀 raise ValueError("this is bad") 🚀 ValueError: this is bad

Since the facility was set to 16, the PRI code ends up being 16 * 8 + 7 == 135 for DEBUG level messages and 16 * 8 + 3 == 131 for ERROR messages.

Exception texts are of course still appended, when the exception log method is called (or the exc_info argument is passed), but the custom line_break_repl here is used for reformatting those texts.

Dependencies

  • Python >=3.8 <=3.12
  • No third-party dependencies
  • OS agnostic

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

syslogformat-1.0.0.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

syslogformat-1.0.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file syslogformat-1.0.0.tar.gz.

File metadata

  • Download URL: syslogformat-1.0.0.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for syslogformat-1.0.0.tar.gz
Algorithm Hash digest
SHA256 83bd396047892f72bad0387f7310fc5f6ab84df556e59c0b76d34d6f0e5e1838
MD5 89a226edad0ac4208bc6c6dca11a2f99
BLAKE2b-256 0067976ab275fd7b8b1f0d991780a04341a13f4997d48d0df5104f27549c180b

See more details on using hashes here.

File details

Details for the file syslogformat-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: syslogformat-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for syslogformat-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff7375f341098e802ef7225e56290f789578ddc6a235365475af42456e7ea3c2
MD5 7eef7352b087ac0f92eeda5b355aeb8f
BLAKE2b-256 d998f5bed1f2efb472411fcc2668ef20bd588183478d01724e7d4a1672f03b92

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page