Boilerplate-free Console Logging Module for Python
conlog is a configurable console logging mechanism featuring:
- One-line setup per Logger
- Optional export to rotating log file
- Show additional information when debugging
- YAML or argument based configuration
Install via pip: (Recommended)
pip install conlog
Or with setup_tools:
python setup.py install
Getting a Logger started with conlog is easy:
import conlog log = conlog.start(level='INFO') log.info('Hello World!')
start( yaml_file=None, log_file=None, log_format='%(asctime)22s - %(levelname)8s - %(name)20s - %(message)s', debug_format='%(asctime)22s - %(levelname)8s - %(name)20s - %(funcName)20s - %(message)s', level='INFO', max_file_size='5000000', max_retention=5 )
Configure and return the root Logger instance. All arguments are optional. If any argument is not supplied, its default value is used. This means it is possible to run with no arguments if default values are desired across all options.
yaml_file : | Pull configurations from a YAML file. All arguments from
conlog: log_file: log/app.log log_format: '%(asctime)22s - %(levelname)8s - %(name)20s - %(message)s' debug_format: '%(asctime)22s - %(levelname)8s - %(name)20s - %(funcName)20s - %(message)s' level: INFO max_file_size: 5 MB max_retention: 5 Like NOTE: If Default: |
---|---|
log_file : | Path to log file. By default, file logging is disabled. If
NOTE: In the example By default, the log file will rotate when it reaches 5 MB, with up
to 5 rotations being kept before overwriting the oldest file.
These values can be adjusted using the Default: |
log_format : | Logging format for all levels EXCEPT Default: |
debug_format : | Logging format for only the Default: |
level : | Logging level. Only messages sent to this level or higher will appear in log. Default: |
max_file_size : | Maximum log file size before rollover. This value can either
be an integer byte size or a proper string like: This option has no impact if Default: |
max_retention : | Maximum number of rollover logs to keep. Rotated logs will be
saved in the format This option has no impact if Default: |
Get a new Logger instance. Recommended usage is
self.log = conlog.new(self)
from the __init__
method of
the calling class.
inst : | Required - Instance of the class which new Logger is for,
(Typically use self ) |
---|
This is the easiest way to add a root Logger using conlog with INFO
level
logging to the console.
log = conlog.start(level='INFO')
Start logging based on configuration in the YAML file, conf/conlog.yml
.
log = conlog.start(yaml_file='conf/conlog.yml')
Start DEBUG
level Logger with console logging and rotating file logging to
logs/app.log
.
log = conlog.start( log_file='logs/app.log', level='DEBUG' )
Similar to above but with specific values set for rotation of log files. This
will rotate the log file when it reaches 1 MB
and retain up to 10
archived log files before overwriting the oldest.
log = conlog.start( log_file='log/app.log', level='INFO', max_file_size='1 MB', max_retention=10, )
Start console logging with a different log format.
log = conlog.start(log_format='%(levelname)s:%(name)s:%(message)s')
Get a Logger instance for a class. In this example, a root Logger has already been set up by start() in the main function of the program.
class Example(object): def __init__(self): self.log = conlog.new(self)
- Ryan Miller - ryan@devopsmachine.com