conlog
Anti-Boilerplate Console Logging Module for Python
About
conlog is a configurable console logging mechanism featuring:
- Boilerplate-free setup
- Only one line per Logger
- Export to optional rotating log file
- Show additional log columns when debugging, suppress when not
- YAML or argument based configuration
Installation
Install via pip: (Recommended)
pip install conlog
Or with setup_tools:
python setup.py install
Documentation
Quick start
import conlog log = conlog.start(level='INFO') log.info('Hello World!')
Functions
start(...)
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 parameters are optional. If any parameter is not supplied, its default value be used. It is even possible to run with no parameters, in which case the default values would be used across all options.
Parameters
yaml_file : |
Pull configurations from YAML file. All parameters 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 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 Default: |
log_format : |
Logging format for all levels EXCEPT Default: |
debug_format : |
Logging format for 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: Default: |
max_retention : |
Maximum number of rollover logs to keep. Rotated logs will be
saved in the format Default: |
new(inst)
Get a new Logger instance for the calling class. Recommended
usage is self.log = conlog.new(self)
.
Parameters
inst : |
Instance of class which new Logger is for, (HINT: use Required |
---|
Examples
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. (Remember to start()
first)
class Example(object): def __init__(self): self.log = conlog.new(self)
Author
- Ryan Miller - ryan@devopsmachine.com