Skip to main content

A logger library which builds on, combines, and simplifies various logging features of Python 3

Project description

lovely-logger

A logger library which builds on, combines, and simplifies various logging features of Python 3.

lovely-logger is a highly robust production-ready feature-rich logger which is used throughout the Tier2 Technologies software stack

Why using lovely-logger is better than using the built-in python logging module:

  1. It is much easier to use
  2. It handles automatic logging of uncaught exception tracebacks
  3. It is thread-safe and will flush the log to disk on program crash/exit
  4. It handles simultaneous logging to both stdout (the console) and automatically rotating log files by default
  5. It supports independent formatters for console vs files out-of-the-box
  6. It supports both timezones and milliseconds in the timestamp
  7. It support shorthand which makes it just as easy to log as it is to print()

example usage:

import lovely-logger as log

log.init('my_log_file.log')

log.debug('here are the in-scope variables right now: %s', dir())
log.info('%s v1.2 HAS STARTED', __file__)
log.warning('here is a warning message')
log.error('generally you would use warning for handled exceptions')
log.critical('generally you would use critical for uncaught exceptions')

The init() function has more optional parameters:

init(filename, to_console=True, level=DEBUG, max_kb=1024, max_files=5)

Setting to_console to False is useful for windowed applications such as those compiled with pyinstaller which have no console.

The valid options for level are DEBUG, WARNING, ERROR, and CRITICAL in that order. setting the level to ERROR, for example, will silence your log.debug() and log.warning() calls while still logging your log.error() and log.critical() calls.

max_kb sets the max logfile size before the log is rotated

max_files sets the max number of rotating logs that are to be kept before the oldest is deleted. So, for example, the default max_kb of 1024 and max_files of 5 means that up to 5 megabytes of logs will be kept, split among five 1MB files. Once the log reaches 5MB and 1byte, the oldest of the 5 files is rotated away, leaving four 1MB archived log files, and a 1byte active log file

There is another special type of log function that can only be used inside of an exception handler. It will log the full exception traceback for you, along with any helpful comments you may have about the exception

try:
    a = 1/0
except:
    log.exception("You can't divide by zero, dummy")

because nobody has time to type out log complicated words like exception or critical, and code looks worse when the print statements are all different lengths, there is shorthand here for you.

instead of this:

import lovely-logger as log
log.init('my_log_file.log')

log.debug('This is a debug log entry')
log.info('This is an info log entry')
log.warning('This is a warning log entry')
log.error('This is an error log entry')
log.critical('This is a critical log entry')
try:
    a = 1/0
except:
    log.exception('This is an exception log entry')

you can write it like this:

import lovely-logger as log
log.init('my_log_file.log')

log.d('This is a debug log entry')
log.i('This is a info log entry')
log.w('This is a warning log entry')
log.e('This is an error log entry')
log.c('This is a critical log entry')
try:
    a = 1/0
except:
    log.x('This is an exception log entry')

By default, the logger is going to output the date/time, level, message, filename, and line number into the file and all that except the date/time to the console. If you want to override what gets outputted, or change the format, you can manually set the formatting:

import lovely-logger as log

log.FILE_FORMAT = "[%(asctime)s] [%(levelname)-8s] - %(message)s (%(filename)s:%(lineno)s)"
log.CONSOLE_FORMAT = "[%(levelname)-8s] - %(message)s (%(filename)s:%(lineno)s)"
log.DATE_FORMAT = '%Y-%m-%d %H:%M:%S.uuu%z'

log.init('my_log_file.log')

log.d('This is a debug log entry')

DATE_FORMAT follows the formatting of the built in python time.strftime() with the exception of the "uuu" which was added to support milliseconds

CONSOLE_FORMAT and FILE_FORMAT follow the formatting from the built in python logging library LogRecord attributes

Note that those values must be set before log.init() is called.

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

lovely-logger-1.0.2.tar.gz (4.7 kB view hashes)

Uploaded Source

Built Distribution

lovely_logger-1.0.2-py3-none-any.whl (18.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page