3 """Tools for setting up a package logging.
5 This module is separate from `tools` to avoid module dependency
6 cycles. This module has no internal dependencies, while `tools`
7 depends on many of the other modules. With this module separate, the
8 other internal modules have access to the default logger before the
9 package configuration is built up enough to configure it according to
10 your external specifications.
13 import logging as _logging
16 def get_basic_logger(name, level=_logging.WARN):
17 """Create and return a basic logger
19 This utility function encapsulates a bunch of `logging`
20 boilerplate that I use in several packages.
22 log = _logging.getLogger(name)
24 formatter = _logging.Formatter(
25 '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
26 stream_handler = _logging.StreamHandler()
27 stream_handler.setLevel(_logging.DEBUG)
28 stream_handler.setFormatter(formatter)
29 log.addHandler(stream_handler)
30 # Cache handlers for easy swapping depending on config settings
31 log._handler_cache = {'stream': stream_handler}