Skip to main content

Logging Formatter to format log messages in logfmt format

Project description

Logfmt for python

Logging Formatter to format log messages in logfmt format.

PyPI version publish workflow test workflow

Logfmt is a simple text-based format for structured logging. It is designed to be easy to read by both humans and machines. It is also designed to be easy to parse and generate.

Example output

ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Hello World"

Log context

Additionally, this library supports to add additional information to the log message via LogContexts. This is also known as MDC (Mapped Diagnostic Context). This adds values, to the log message across multiple log statements.

with LogContext(foo="bar"):
    logger.info("Hello World")
    logger.info("This is a second test")
logger.info("Outside of context")
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Hello World" foo=bar
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="This is a second test" foo=bar
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Outside of context"

Installation

pip install logfmt-julian-sobott

Example usage

import logging
from logfmt import LogFmtFormatter, LogContext, CallableLogContext

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = LogFmtFormatter()
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)

logger.info("Hello World")
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Hello World"

logger.info("Hello World", extra={"foo": "bar"})
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Hello World" foo=bar

with LogContext(foo="bar"):
    logger.info("Hello World")
    logger.info("This is a second test")
logger.info("Outside of context")
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Hello World" foo=bar
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="This is a second test" foo=bar
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Outside of context"

@CallableLogContext()
def foo(name: str):
    logger.info("Hello World")
foo("bar")
# ts=2022-10-14T17:41:15+0200 lvl=INFO msg="Hello World" name=bar


@CallableLogContext("name", "args")
def foo(name: str, surname: str, *args):
    logger.info("Hello World")
foo("bar with spaces", "baz", "qux")
# ts=2022-10-14T17:45:58+0200 lvl=INFO msg="Hello World" args=('qux',) name="bar with spaces"

from dataclasses import dataclass
@dataclass
class Person:
    name: str

@CallableLogContext(name="person.name")
def foo(person: Person):
    logger.info("Hello World")

foo(Person(name="Tom"))
# ts=2022-10-15T00:56:16+0200 lvl=INFO msg="Hello World" name=Tom

try:
    raise Exception("Something went wrong")
except Exception as e:
    logger.exception("trying to fix it")
# ts=2022-10-14T22:49:31+0200 lvl=ERROR msg="trying to fix it" 
# exception="
# Traceback (most recent call last):
#   File \"/src/tests/x.py\", line 17, in <module>
#     raise Exception(\"Something went wrong\")
# Exception: Something went wrong"

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

logfmt_julian_sobott-0.4.1.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

logfmt_julian_sobott-0.4.1-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

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