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