Convenient wrapper for logging python applications with options to add color, select logging style, rotate files by size and time, etc.
Output from demo.py
:
$ pip install NeatLogger
-
Import the library.
from NeatLogger import Log
-
Create an instance.
log = Log()
Arguments (all are optional):
project_name
: Project name, which would serve as the logger's name (if specified), and the prefix for log filenames.log_level
: Level for logging. Choices:"critical"
"error"
"warning"
"info"
"debug"
"notset"
assign_logger_name
: Flag to decide whether to assignproject_name
as the name to the logger.formatter
: Logging formatter. Choices:- an instance of
logging.Formatter
"json"
"apache"
- an instance of
log_to_stdout
: Flag to decide whether to display the logs in stdout.log_to_file
: Flag to decide whether to store the logs in a file.log_dir
: Directory where the log files are stored.log_file_suffix
: Suffix for the log filenames. Ignored ifrotate_file_by_time=True
. Choices:"S"
:%Y-%m-%d_%H-%M-%S
is appended to the filename."M"
:%Y-%m-%d_%H-%M-00
"H"
:%Y-%m-%d_%H-00-00
"D"
:%Y-%m-%d
rotate_file_by_size
: Flag to decide whether to rotate the log files by size.rotating_file_max_size_bytes
: Size (in bytes) threshold to rollover the log files.rotate_file_by_time
: Flag to decide whether to rotate the log files by time. Ignoreslog_file_suffix
.rotation_period
: Rotation period for the log files. Choices:"S"
: log file rollovers every second. Ignoresrotation_time
."M"
: log file rollovers every minute. Ignoresrotation_time
."H"
: log file rollovers every hour. Ignoresrotation_time
."D"
: log file rollovers every day. Ignoresrotation_time
."MIDNIGHT"
: log file rollovers at midnight, or atrotation_time
if specified."W0"
: log file rollovers on weekday 0 i.e. Monday, atrotation_time
if specified."W1"
"W2"
"W3"
"W4"
"W5"
"W6"
rotation_interval
: Intervals of rotation period to rollover the log files. Ignored ifrotation_time
is a weekday.rotation_time
: Time of the day to rollover the log file whenrotation_period
="MIDNIGHT"
or a weekday.rotating_file_backup_count
: Number of old files to be retained.use_utc
: Flag to decide whether to use UTC in the log timestamp and filenames.colors_to_stdout
: Flag to decide whether to have colorful log. Only works forlog_to_stdout=True
andlog_formatter="apache"
.ignore_log_attribute_list
: List of log attributes to be ignored in the log. By default, some attributes are ignored. If all the attributes are desired, useignore_log_attribute_list=list()
.
⚠️ If more than 1 of the following are set toTrue
, only one of them is implemented. Their priority follows the order:rotate_file_by_size
rotate_file_by_time
log_to_file
-
Get a logger and start logging.
logger = log.get_logger() logger.info("Testing 1 2 3 ...")
© 2021, Samyak Tamrakar.