Skip to main content

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

Project description

PyPI version Downloads

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
  4. It will flush the log to disk on program crash/exit
  5. It handles simultaneous logging to both stdout (the console) and automatically rotating log files by default
  6. It supports independent formatters for console vs files out-of-the-box
  7. It supports both timezones and milliseconds in the timestamp
  8. It has sane defaults, like logging a timestamp in the first place
  9. It supports shorthand which makes it just as quick to log as it is to print()
Getting Started:
import lovely_logger as log # pip install lovely-logger

log.init('filename.log')
log.info('Hello World!')

It's that easy!

Another Example:
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 error for handled exceptions which prevent further execution')
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, INFO, 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, (as level=CRITICAL) along with any helpful comments you may have about the exception

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

because nobody has time to type out 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 a info log entry')
log.warning('This is a warning log entry')
log.error('This is a 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 a 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 log file. It will print all of that same info 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.7.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

lovely_logger-1.0.7-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file lovely_logger-1.0.7.tar.gz.

File metadata

  • Download URL: lovely_logger-1.0.7.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.9

File hashes

Hashes for lovely_logger-1.0.7.tar.gz
Algorithm Hash digest
SHA256 091773dbafe19b1bd3819e20e959314731b216e776a6a8c1466be6d98202b806
MD5 2c42d934b43d201760877b066cf77c2d
BLAKE2b-256 70b2995e8dff7761fb7e3312ec07b9d0b2ede425ec695b5dd5bce2d6de2e6c67

See more details on using hashes here.

File details

Details for the file lovely_logger-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: lovely_logger-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.0 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.7.9

File hashes

Hashes for lovely_logger-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 91b04f04f4a6353a6c13503ceedb76eee47589e1081aab7933245b1524abf473
MD5 88b9d067cfebf461bb57ae6ecab56028
BLAKE2b-256 f0145eda82d2d772e83759c3496d8c78699eaa440e29c5a964303818abd0d9f5

See more details on using hashes here.

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