Convert update-copyright.py to a more modular framework.
[update-copyright.git] / update_copyright / log.py
1 # Copyright
2
3 """Tools for setting up a package logging.
4
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.
11 """
12
13 import logging as _logging
14
15
16 def get_basic_logger(name, level=_logging.WARN):
17     """Create and return a basic logger
18
19     This utility function encapsulates a bunch of `logging`
20     boilerplate that I use in several packages.
21     """
22     log = _logging.getLogger(name)
23     log.setLevel(level)
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}
32     return log