Skip to main content

Logging Extensions

Project description

https://img.shields.io/badge/code%20style-black-000000.svg https://img.shields.io/github/license/open-things/loggingex.svg https://travis-ci.com/open-things/loggingex.svg?branch=master https://img.shields.io/pypi/v/loggingex.svg?color=green https://img.shields.io/pypi/pyversions/loggingex.svg https://img.shields.io/pypi/dm/loggingex.svg

Implements a few extensions for the standard python logging library.

Currently, this project lacks documentation. Sorry about that.

Logging Context Filter

Logging Context Filter injects context variables into LogRecord objects as extra fields.

Here’s a usage example:

import logging
import sys

from loggingex.context import LoggingContextFilter, context

log = logging.getLogger()


def process_lines(lines):
    for index, line in enumerate(lines):
        line = line.strip()
        with context(current_line=index + 1):
            log.debug("processing line: %s", line)
            if not line:
                log.error("empty line!")
                continue
            log.info("processed line: %s", line)


def process_files(filenames):
    log.debug("starting...")
    for filename in filenames:
        with context(current_file=filename):
            log.info("processing file: %s", filename)
            with open(filename, "r") as f:
                process_lines(f)
            log.info("processed file: %s", filename)
    log.debug("work is complete!")


if __name__ == "__main__":
    formatter = logging.Formatter(
        "%(current_file)s:%(current_line)s:%(levelname)s: %(message)s"
    )
    handler = logging.StreamHandler(stream=sys.stdout)
    handler.setFormatter(formatter)
    handler.setLevel(logging.DEBUG)
    handler.addFilter(LoggingContextFilter())  # it's that simple
    logging.basicConfig(handlers=[handler], level=logging.DEBUG)

    with context(current_file="-", current_line="-"):
        # The context above sets default value, so that the formatter does
        # not crash, when they are not defined.
        process_files(sys.argv[1:])

Project details


Download files

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

Files for loggingex, version 1.2.0
Filename, size File type Python version Upload date Hashes
Filename, size loggingex-1.2.0-py3-none-any.whl (11.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size loggingex-1.2.0.tar.gz (20.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page