Skip to main content

Easy logger management for libraries and CLI tools.

Project description

https://travis-ci.org/drcloud/magiclog.svg?branch=master

magiclog simplifies application and library logging, allowing you to use loggers where you would use print statements without complicated setup or logger discovery.

from magiclog import log


def main():
    log.configure()
    log.info('Hello, INFO.')


if __name__ == '__main__':
    main()

magiclog tunes the logging config in a way that’s friendlier than the logging module’s basic config, without asking any more work from the library author:

  • from magiclog import log retrieves the logger for your application, using a little meta-programming, obviating the need to figure whether you should use __package__, __name__ or the sys.argv[0].
  • When you’re running your task in the background, magiclog recognizes this and logs to Syslog; otherwise it logs to stderr.
  • magiclog provides a simply one-line format for stderr, with the time and a message, but can also log in an extended format, with logging and level, when requested. And magiclog always includes this information when logging to Syslog.

magiclog only arranges to log at all when you call log.configure(). If you’d like to customize levels, you can explicitly pass them to log.configure() with the level=..., syslog=... and stderr=... named parameters. Otherwise, your logger has a null handler, as described in Configuring Logging for a Library

log.configure()

# Same as the above.
log.configure(level='info')

# Use Syslog or stderr based on whether or not we're in the foreground,
# but at debug level instead of info level.
log.configure(level='debug')

# Enables both channels, whether foregrounded or backgrounded.
log.configure(syslog='info', stderr='info')

# Enables the more verbose console logging format, with module and function
# name information as well as time.
log.configure(level='info', extended=True)

Configuring Other Modules’ Loggers

The logger function in magiclog provides for logger discovery, walking the stack and using the module or package name, or the name of the running executable, depending on the situation. You can use logger to retrieve and configure the loggers of other modules, too, following the same rules as your application logger.

import magiclog
from magiclog import log


def main():
    # You can pass a module object or a string to ``logger``. Here, we
    # configure magiclog's own (usually not very useful) logger.
    magiclog.logger(magiclog).configure()
    # Obtain a reference to Boto3's logger and auto-configure it. The
    # logger for ``magiclog`` will print out a few things as it sets this
    # logger up.
    magiclog.logger('boto3').configure()
    log.configure()
    log.info('Hello, INFO.')


if __name__ == '__main__':
    main()

Project details


Release history Release notifications

This version
History Node

1.0.2

History Node

0.99.4

History Node

0.99.3

History Node

0.99.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
magiclog-1.0.2.tar.gz (5.2 kB) Copy SHA256 hash SHA256 Source None Oct 27, 2016

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page