Skip to main content

logconfig

version travis coveralls license

Simple helper moudle for configuring Python logging.

Requirements

Compatibility

  • Python 2.6

  • Python 2.7

  • Python 3.2

  • Python 3.3

  • Python 3.4

Dependencies

  • PyYAML

  • logutils (if using Python 2)

Installation

pip install logconfig

Overview

This simple library exposes several helper methods for configuring the standard library’s logging module. There’s nothing fancy about it. Under the hood logconfig uses logging.config to load various configuartion formats.

In addition to configuration loading, logconfig provides helpers for easily converting a configured logger’s handlers utilize a queue.

Supported Configuration Formats

  • JSON

  • YAML

  • ConfigParser

  • Python Dict

Quickstart

Configuration Loading

import logconfig
import logging

# Load config from JSON file
logconfig.from_json('path/to/file.json')

# Load config from YAML file
logconfig.from_yaml('path/to/file.yml')

# Load config from ConfigParser file
logconfig.from_yaml('path/to/file.cfg')

# Load config from dict
logconfig.from_dict(config_dict)

log = logging.getLogger()
log.debug('Configuration loaded using logconfig')

Queue Utilization

import logconfig
import logging

logconfig.from_dict({
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'level': 'DEBUG'
        }
    },
    'loggers': {
        'mylogger': {
            'handlers': ['console']
        }
    }
})

# Convert logger's handlers to utilize a queue
queue = logconfig.Queue(-1)
listener = logconfig.QueueListener(queue)
handler = logconfig.QueueHandler(queue)

mylogger = logging.getLogger('mylogger')

# You can also pass in the logger name instead of the actual logger.
# logconfig.queuify_logger('mylogger', handler, listener)
logconfig.queuify_logger(mylogger, handler, listener)

assert isinstance(mylogger.handlers[0], logconfig.QueueHandler)

# Start the listener.
listener.start()

# When finished, stop the listener.
# This is optional, but not doing so may prevent some logs from being processed.
listener.stop()

Usage

Use logconfig to easily load logging configurations. For more details on configuring logging, visit https://docs.python.org/library/logging.config.html.

import logconfig

Configuration from JSON

Configure logging using JSON file.

logconfig.from_json(filename)

Example JSON file:

{
    "version": 1,
    "disable_existing_loggers": false,
    "formatters": {
        "simple": {
            "format": "%(asctime)s. - %(name)s - %(levelname)s - %(message)s"
        }
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "formatter": "simple",
            "stream": "ext://sys.stdout"
        }
    },
    "root": {
        "level": "DEBUG",
        "handlers": ["console"]
    }
}

Configuration from YAML

Configure logging using YAML file.

logconfig.from_yaml(filename)

Example YAML file:

version: 1
disable_existing_loggers: False
formatters:
  simple:
    format: "%(asctime)s. - %(name)s - %(levelname)s - %(message)s"
handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
root:
  level: DEBUG
  handlers: [console]

Configuration from ConfigParser File

Configure logging using ConfigParser compatible file.

logconfig.from_file(filename)

Example CFG file:

[loggers]
keys=root

[handlers]
keys=console

[formatters]
keys=simple

[logger_root]
level=DEBUG
handlers=console

[handler_console]
class=StreamHandler
level=DEBUG
formatter=simple
args=(sys.stdout,)

[formatter_simple]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

Configuration from Dict

Configure logging using Python dictionary.

logconfig.from_dict(dct)

Example dict:

{
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(asctime)s. - %(name)s - %(levelname)s - %(message)s'
        }
    },
    'handlers': {
        'console': {
            'formatter': 'simple',
            'class': 'logging.StreamHandler',
            'level': 'DEBUG',
            'stream': 'ext://sys.stdout'
        }
    },
    'root': {
        'handlers': ['console'],
        'level': 'DEBUG'
    }
}

Configuration from Autodetection

If, for whatever reason, you do not know what the source of the configuration will be (or if you’re just feeling lucky), then you can try to coerce logging configuration using one of the autodetection methods:

logconfig.from_filename(filename)
logconfig.from_autodetect(filename_or_dict)

try:
    logconfig.from_filename(filename)
    logconfig.from_autodetect(filename_or_dict)
except logconfig.LogConfigException as ex:
    # Unrecognized configuration argument.
    pass

These methods will try to dispatch the function argument to the proper configuration loader or fail trying.

Configuration from Environment Variable

Configure logging using filename provided via environment variable.

logconfig.from_env(variable_name)

NOTE: Environment variable value will be passed to from_filename().

Metadata

Release files for logconfig 0.4.0

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

Source distribution (sdist)

Source distribution for logconfig 0.4.0
File Size Uploaded
logconfig-0.4.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logconfig 0.4.0
File Interpreter ABI Platform
logconfig-0.4.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 17.7 kB

Release files / logconfig-0.4.0.tar.gz

Download URL logconfig-0.4.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
04280128fa1762604281dfafc2dbf527975a5e532c9dc4f76c68cd080274a663
BLAKE2b-256 checksum
How to use checksums
4cee615dc71711e101ffa22746437e86a10eee5fd8dd78dfb559ea39133ff213
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / logconfig-0.4.0-py2.py3-none-any.whl

Download URL logconfig-0.4.0-py2.py3-none-any.whl
Size 9.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
19500458c84858ad5f6efe90c9cc6d1bbd06bba998ecbc8ddb621ae37d2743fd
BLAKE2b-256 checksum
How to use checksums
0f587593f76aa0a393c23492f28860c9afe9adb8c640aeb507daad4fe46685a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

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