Skip to main content

Easy logger management for libraries and CLI tools.

Project description

https://travis-ci.org/drcloud/magiclog.svg?branch=master https://img.shields.io/pypi/v/magiclog.svg https://img.shields.io/pypi/dd/magiclog.svg

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


Download files

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

Source Distribution

magiclog-1.0.2.tar.gz (5.2 kB view details)

Uploaded Source

File details

Details for the file magiclog-1.0.2.tar.gz.

File metadata

  • Download URL: magiclog-1.0.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for magiclog-1.0.2.tar.gz
Algorithm Hash digest
SHA256 ca33bb9f8ae7c87e45bade611f2230b788dd408e75e745d0ff94356a096df7a0
MD5 8b9bd482579687a1ad3afee6da2dfc7d
BLAKE2b-256 bfdc91c28a511c82bec1e798f04177fb311a708622a6100de36ea1ba8f2dccf0

See more details on using hashes here.

Supported by

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