comparison MoinMoin/log.py @ 3112:dc182f4936b1

logging: add missing file for previous changeset 79f850129782)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 25 Feb 2008 21:07:30 +0100
parents
children b8dc6c2e6c53
comparison
equal deleted inserted replaced
3111:79f850129782 3112:dc182f4936b1
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - init "logging" system
4
5 WARNING
6 -------
7 logging must be configured VERY early, before any moin module calls
8 log.getLogger(). Because most modules call getLogger on the module
9 level, this basically means that MoinMoin.log must be imported first
10 and load_config must be called afterwards, before any other moin
11 module gets imported.
12
13 Usage
14 -----
15 Typically, do this at top of your module:
16
17 from MoinMoin import log
18 logging = log.getLogger(__name__)
19
20 This will create a logger with 'MoinMoin.your.module' as name.
21 The logger can optionally get configured in the logging configuration.
22 If you don't configure it, some upperlevel logger (e.g. the root logger)
23 will do the logging.
24
25 @copyright: 2008 MoinMoin:ThomasWaldmann
26 @license: GNU GPL, see COPYING for details.
27 """
28
29 # This is the "last resort" fallback logging configuration for the case
30 # that load_config() is either not called at all or with a non-working
31 # logging configuration.
32 # See http://www.python.org/doc/lib/logging-config-fileformat.html
33 # We just use moin.log in current directory by default, if you want
34 # anything else, override logging_conf in your server script's Config class.
35 logging_defaults = {
36 'logdir': '.',
37 'loglevel': 'DEBUG',
38 }
39 logging_config = """\
40 [loggers]
41 keys=root
42
43 [handlers]
44 keys=logfile,stderr
45
46 [formatters]
47 keys=logfile
48
49 [logger_root]
50 level=%(loglevel)s
51 handlers=logfile,stderr
52
53 [handler_logfile]
54 class=FileHandler
55 level=NOTSET
56 formatter=logfile
57 args=('%(logdir)s/moin.log', 'at')
58
59 [handler_stderr]
60 class=StreamHandler
61 level=NOTSET
62 formatter=logfile
63 args=(sys.stderr, )
64
65 [formatter_logfile]
66 format=%(asctime)s %(levelname)s %(name)s:%(lineno)d %(message)s
67 datefmt=
68 class=logging.Formatter
69 """
70
71 from MoinMoin.support import logging_fix
72 import logging, logging.config
73
74 configured = False
75 fallback_config = False
76
77 strip_MoinMoin = False
78
79 def load_config(conf_fname):
80 """ load logging config from conffile """
81 global configured
82 try:
83 logging.config.fileConfig(conf_fname)
84 configured = True
85 except Exception, err: # XXX be more precise
86 load_fallback_config(err)
87
88 def load_fallback_config(err=None):
89 """ load builtin fallback logging config """
90 global configured
91 from StringIO import StringIO
92 logging.config.fileConfig(StringIO(logging_config), logging_defaults)
93 configured = True
94 l = getLogger(__name__)
95 l.warning('Using built-in fallback logging configuration!')
96 if err:
97 l.warning('load_config failed with "%s".' % str(err))
98
99
100 def getLogger(name):
101 """ wrapper around logging.getLogger, so we can do some more stuff:
102 - preprocess logger name
103 - patch loglevel constants into logger object, so it can be used
104 instead of the logging module
105 """
106 global configured
107 if not configured: # should not happen
108 load_fallback_config()
109 if strip_MoinMoin and name.startswith('MoinMoin.'):
110 name = name[9:]
111 logger = logging.getLogger(name)
112 for levelnumber, levelname in logging._levelNames.items():
113 if isinstance(levelnumber, int): # that list has also the reverse mapping...
114 setattr(logger, levelname, levelnumber)
115 return logger
116