Logging Formatter to format log messages in logfmt format
Project description
Logfmt for python
Logging Formatter to format log messages in logfmt format.
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
Built Distribution
File details
Details for the file logfmt_julian_sobott-0.4.1.tar.gz
.
File metadata
- Download URL: logfmt_julian_sobott-0.4.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d225a5b86ff939178184cbd62bb2eef260deff2e951854f7533e026bfeace05 |
|
MD5 | dfc7f0a0e837096e441545bb087269de |
|
BLAKE2b-256 | dc3a1400df78e06da0e7f9fa41eeaade096c82725676bfcbf693f3d6cb77a3b6 |
File details
Details for the file logfmt_julian_sobott-0.4.1-py3-none-any.whl
.
File metadata
- Download URL: logfmt_julian_sobott-0.4.1-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.2.2 CPython/3.10.7 Linux/5.15.0-1020-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 931162f774fc437adfa752900ce54213f00480b7711f05e26aa0c046d38123b4 |
|
MD5 | d97f2608e2a0d77e91a7702f7b385704 |
|
BLAKE2b-256 | 6de2dc94a0e272c6f07115665ea045fe1e696d2594ca50e4869aea34b7af8111 |