logfmter

0.0.12 · active · verified Thu Apr 16

logfmter is a Python package that enables logfmt-formatted logging using the standard `logging` module without requiring changes to existing log calls. It aims to provide human and machine-readable logs, adhering to best practices like those recommended by Splunk. The current version is 0.0.12, and new releases are typically made for bug fixes, Python version support, and minor feature enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `logfmter` with the standard Python `logging` module using `basicConfig`. It sets up a `StreamHandler` to output logfmt-formatted messages to the console, including `extra` dictionary parameters which `logfmter` automatically flattens into key-value pairs. It also shows logging a dictionary as the main message.

import logging
import os
from logfmter import Logfmter

# Configure a basic StreamHandler with Logfmter
handler = logging.StreamHandler()
handler.setFormatter(Logfmter())

# Basic configuration to use the Logfmter handler
logging.basicConfig(handlers=[handler], level=os.environ.get('LOG_LEVEL', 'INFO'))

# Example logs
logging.info("Application started", env=os.environ.get('ENV', 'development'))
logging.warning("User login failed", user_id=123, ip_address="192.168.1.1", extra={"attempts": 3})
logging.error("An unexpected error occurred", error_code="E1001")

# Logging a dictionary directly as a message (keys will be flattened)
logging.info({"event": "user_data_processed", "status": "success", "records": 100})

view raw JSON →