Nice and simple log formatting for the standard library
Project description
simple-logging-setup
simple-logging-setup is a simple yet highly configurable library that provides sane configuration defaults for the logging system in the Python standard library.
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 |
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 simple-logging-setup-0.5.tar.gz
.
File metadata
- Download URL: simple-logging-setup-0.5.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b31d978b1d41c0a60fe86a44a7627e9db8dbf4292a7a03df0a5dab4f458a6579 |
|
MD5 | ceee6f6e05a32bf1516e965bbdd693b7 |
|
BLAKE2b-256 | c1949c6f12cab1685495b1e9a268611f493a5a4a9881b02cb2172ff87cb6ce2e |
File details
Details for the file simple_logging_setup-0.5-py3-none-any.whl
.
File metadata
- Download URL: simple_logging_setup-0.5-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 780610f342cc65ddf0a2af960cf8fa0d1b63940e42a46625d5a46f9c712b96fb |
|
MD5 | 1d53c62d7dacf0dbb42f8a5f8fc73647 |
|
BLAKE2b-256 | eef5eabb943b400dd86a05e005b00b931d06dc1821ba07997a00aee5f00b3c2d |