Skip to main content

simple-logging-setup

license MIT Latest Version

simple-logging-setup is a simple yet highly configurable library that provides sane configuration defaults for the logging system in the Python standard library.

Screenshot

Features:

  • No external dependencies
  • Simple coloring and detection if the terminal supports colored output
  • Formatting for exceptions
  • Support for syslog severity levels
  • Log filtering
  • Easy to integrate into command line options

Installation

simple-logging-setup can be installed using pip

pip install simple-logging-setup

Usage

Simple Setup

import logging

from simple_logging_setup import setup

logger = logging.getLogger('my-logger')


setup(
    level='info',
    # put your configuration here
) 

logger.info('Hi mom!')

Setup using command line options

import argparse

from simple_logging_setup import setup


# parse command line
parser = argparse.ArgumentParser()

parser.add_argument(
    '-l',
    '--log-level',
    choices=['debug', 'info', 'warn', 'error', 'critical'],
    default='info',
)

parser.add_argument(
    '--loggers',
    type=str,
    nargs='+',
)

parser.add_argument(
    '--show-timestamps',
    action='store_true',
)

args = parser.parse_args()

# setup logging
setup(
    level=args.log_level,
    loggers=args.loggers,
    show_timestamp=args.show_timestamps,
)
$ my-tool -l debug --loggers my-logger -disabled-logger
$ my-tool --show-timestamps

Configuration

All configuration is done by adding keyword arguments to simple_logging_setup.setup.

Name Type Default Description
level str info Log level. Choices (case insensitive): ['debug', 'info', 'warn', 'warning', 'error', 'critical']
colors switch True Enables or disables colors. Gets disabled by default if the terminal does not support colored output
syslog_priorities switch False Enables or disables syslog severity levels. Gets enabled by default if running in journald
show_thread_name switch True Show thread name in log output
show_level_name switch True Show log level name in log output. Gets enabled by default if colors are disabled or not available
show_timestamp switch True Show timestamp in log output
show_logger_name switch True Show logger name in log output

switch is a special type that does best-effort parsing of incoming values.

Examples: True, 1, 'True', 'TRUE', 'yes', 'on', [...]

Filtering

Especially when the log level is set to debug, logging output can become hard to read. simple-logging-setup allows you to filter loggers by including or excluding specific logger names.

from simple_logging_setup import setup

setup(
    loggers=[

        # include loggers to the output (the `+` is optional)
        'my-project.logger-1',
        '+my-project.logger-1',

        # exclude loggers from the output (both `-` and `_` can be used)
        '-my-project.logger-1',
        '_my-project.logger-1',
    ],
)

Why two prefixes to choose for excludes? The - notation makes more sense in code but may not be suitable for command line options. This optional extra notation allows for command line options like

--loggers +included-logger _excluded-logger.

Name Filtering

In some cases, it is not useful to always print all logger names. For example, when creating a command line tool, a logger name like my-project.my-sub-system is worth printing, but root is not. simple-logging-setup allows you to filter out logger names, without excluding the logger entirely.

from simple_logging_setup import setup

setup(
    filter_logger_names=['root'],
)

Presets

simple-logging-setup comes with a list of useful presets which can be enabled via the preset keyword argument.

from simple_logging_setup import setup

setup(preset='service')  # 'service' is the default
Name Description
service Enables all switches and features available
cli Disables all switches but show_logger_name and filters the logger name root

Metadata

Release files for simple-logging-setup 0.5

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

Source distribution (sdist)

Source distribution for simple-logging-setup 0.5
File Size Uploaded Provenance
simple-logging-setup-0.5.tar.gz 8.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for simple-logging-setup 0.5
File Interpreter ABI Platform
simple_logging_setup-0.5-py3-none-any.whl Python 3 none any Details

Total release size: 17.1 kB

Release files / simple-logging-setup-0.5.tar.gz

Download URL simple-logging-setup-0.5.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b31d978b1d41c0a60fe86a44a7627e9db8dbf4292a7a03df0a5dab4f458a6579
BLAKE2b-256 checksum
How to use checksums
c1949c6f12cab1685495b1e9a268611f493a5a4a9881b02cb2172ff87cb6ce2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / simple_logging_setup-0.5-py3-none-any.whl

Download URL simple_logging_setup-0.5-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
780610f342cc65ddf0a2af960cf8fa0d1b63940e42a46625d5a46f9c712b96fb
BLAKE2b-256 checksum
How to use checksums
eef5eabb943b400dd86a05e005b00b931d06dc1821ba07997a00aee5f00b3c2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Security for simple-logging-setup 0.5

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