The logging_handler library is intended to provide a quick method to create Python logging handler objects.
Table of Contents
LearningToPi Posts | |
Git: https://github.com/LearningToPi/logging_handler | |
PyPi: https://pypi.org/project/logging-handler/ | |
Python logging facility Documentation: https://docs.python.org/3/library/logging.html |
logging_handler
– Install from PyPi
pip3 install -U logging_handler
Usage Example
from logging_handler import create_logger
logger = create_logger(console_level='DEBUG', log_file='out.log', file_level='INFO')
logger.warning('this is a warning!')
logger.info('just useful info')
logger.debug('debug message!')
In the example above, a log file will be created in the working directory called ‘out.log’ (if the file exists messages will be appended by default). The warning and info message will be printed to the screen and logged to the file. The debug message will only be printed to the screen.
Core Functions
logging_handler.create_logger(console_level='WARNING', log_file='', file_level='WARNING', name='', file_mode='a', console=True, syslog=False, syslog_script_name='', log_file_vars=[], log_file_retation_days=0, propagate=False)
Return: Python logging facility (see https://docs.python.org/3/library/logging.html for specifics)
Parameters: name (default_value) – description
- console_level (‘WARNING’) – Level of messages to write to the console
- log_file (”) – Name of file to log to relative to working dir
- file_level (‘WARNING’) – Level of messages to write to the log file
- console (True) – Enable / Disable logging messages on the console
- name (”) – Name for the Pypthon logging facility, blank string is equiv to the root facility
- file_mode (‘a’) – File mode for writing (‘a’ = append, ‘w’ = overwrite)
- syslog (False) – Enables / Disables logging to the local syslog on the system (not sending to a specific syslog server, this can be enabled with standard rsyslogd configuration)
- syslog_script_name (”) – Name to use for the script in the syslog message
- log_file_vars ([]) – List of dict in the following format: {“var”: “{var_name}”, “set”: “var_parameters“}. Supported variables (more may be added):
- var_name: “{date}” – for “set”, pass a datetime formatting string. i.e. “%Y-%m-%d-%Y-%M”. See Python datetime format codes for available options.
- log_file_retention_days (0) – Specifies number of days worth of logs to retain. If zero, no logs will be removed. Logs modified older than the days specified will be deleted (NOTE: the create_logger function will search only for log files that match the naming provided)
- propagate (False) – If set to True, logs sent to a named logger will also be sent to the root logger. By default this is disabled as it will likely result in duplicate log messages. Python sets this to enabled by default which can cause some confusing log messages.
Notes:
- The logger prints messages using a predefined logging formatter. ‘%(asctime)s – %(name)s – %(levelname)s – %(message)s’. This format will print the timestamp, the name of the logger (or root if one was not provided), the level of the log message, and the message. This formatter was selected to closely match the Linux system logs.
- Using a date variable in log_file_vars does not handle rotating a log file at midnight. This is only used when the logger is called. The use case was for a script that ran at a recurring interval.