fb45f2d218b6669634818734a968263a0e0e4e97
[update-copyright.git] / update_copyright / log.py
1 # Copyright (C) 2012 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of update-copyright.
4 #
5 # update-copyright is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
8 # later version.
9 #
10 # update-copyright is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # update-copyright.  If not, see <http://www.gnu.org/licenses/>.
17
18 """Tools for setting up a package logging.
19
20 This module is separate from `tools` to avoid module dependency
21 cycles.  This module has no internal dependencies, while `tools`
22 depends on many of the other modules.  With this module separate, the
23 other internal modules have access to the default logger before the
24 package configuration is built up enough to configure it according to
25 your external specifications.
26 """
27
28 import logging as _logging
29
30
31 def get_basic_logger(name, level=_logging.WARN):
32     """Create and return a basic logger
33
34     This utility function encapsulates a bunch of `logging`
35     boilerplate that I use in several packages.
36     """
37     log = _logging.getLogger(name)
38     log.setLevel(level)
39     formatter = _logging.Formatter(
40         '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
41     stream_handler = _logging.StreamHandler()
42     stream_handler.setLevel(_logging.DEBUG)
43     stream_handler.setFormatter(formatter)
44     log.addHandler(stream_handler)
45     # Cache handlers for easy swapping depending on config settings
46     log._handler_cache = {'stream': stream_handler}
47     return log