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