Skip to main content

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"

Metadata

Release files for logfmt-julian-sobott 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logfmt-julian-sobott 0.4.1
File Size Uploaded Provenance Actions
logfmt_julian_sobott-0.4.1.tar.gz 4.2 kB Without provenance Details for logfmt_julian_sobott-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for logfmt-julian-sobott 0.4.1
File Interpreter ABI Platform Actions
logfmt_julian_sobott-0.4.1-py3-none-any.whl Python 3 none any Details for logfmt_julian_sobott-0.4.1-py3-none-any.whl

Total release size: 8.2 kB

Release files / logfmt_julian_sobott-0.4.1.tar.gz

Download URL logfmt_julian_sobott-0.4.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5d225a5b86ff939178184cbd62bb2eef260deff2e951854f7533e026bfeace05
BLAKE2b-256 checksum
How to use checksums
dc3a1400df78e06da0e7f9fa41eeaade096c82725676bfcbf693f3d6cb77a3b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure

Release files / logfmt_julian_sobott-0.4.1-py3-none-any.whl

Download URL logfmt_julian_sobott-0.4.1-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
931162f774fc437adfa752900ce54213f00480b7711f05e26aa0c046d38123b4
BLAKE2b-256 checksum
How to use checksums
6de2dc94a0e272c6f07115665ea045fe1e696d2594ca50e4869aea34b7af8111
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure

Security for logfmt-julian-sobott 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page