annotate MoinMoin/log.py @ 4478:66e11e4ce6df

make logging handlers defined in logging.handlers work (e.g. class=handlers.RotatingFileHandler)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 06 Jan 2009 20:49:20 +0100
parents fea6b6afce05
children 3f0bc26f3d5a
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 -------
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
7 logging must be configured VERY early, before the code in log.getLogger
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
8 gets executed. Thus, logging is configured either by:
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
9 a) an environment variable MOINLOGGINGCONF that contains the path/filename
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
10 of a logging configuration file - this method overrides all following
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
11 methods (except if it can't read or use that configuration, then it
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
12 will use c))
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
13 b) by an explicit call to MoinMoin.log.load_config('logging.conf') -
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
14 you need to do this very early or a) or c) will happen before
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
15 c) by using a builtin fallback logging conf
3582
27ba7445b480 logging: fix typos
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3579
diff changeset
16
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
17 If logging is not yet configured, log.getLogger will do an implicit
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
18 configuration call - then a) or c) is done.
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
20 Usage (for wiki server admins)
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
21 ------------------------------
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
22 Either use something like this in some shell script:
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
23 MOINLOGGINGCONF=/path/to/logging.conf
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
24 export MOINLOGGINGCONF
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
25
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
26 Or, modify your server adaptor script (e.g. moin.cgi) to do this:
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
27
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
28 from MoinMoin import log
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
29 log.load_config('wiki/config/logging/logfile') # XXX please fix this path!
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
30
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
31 You have to fix that path to use a logging configuration matching your
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
32 needs (we provide some examples in the path given there, it is relative to
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
33 the uncompressed moin distribution archive - if you use some moin package,
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
34 you maybe find it under /usr/share/moin/).
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
35 It is likely that you also have to edit the sample logging configurations
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
36 we provide (e.g. to fix the logfile location).
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
37
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
38 Usage (for developers)
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
39 ----------------------
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
40 If you write code for moin, do this at top of your module:
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 from MoinMoin import log
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43 logging = log.getLogger(__name__)
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 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
46 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
47 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
48 will do the logging.
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49
3165
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
50 @copyright: 2008 MoinMoin:ThomasWaldmann,
3204
aae4a2384804 whitespace fixes
Mikko Virkkil? <mvirkkil@cc.hut.fi>
parents: 3165
diff changeset
51 2007 MoinMoin:JohannesBerg
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52 @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
53 """
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 # 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
56 # 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
57 # logging configuration.
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 # See http://www.python.org/doc/lib/logging-config-fileformat.html
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
59 # We just use stderr output by default, if you want anything else,
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
60 # you will have to configure logging.
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 logging_defaults = {
3487
71b403707c1d use INFO loglevel in fallback logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3204
diff changeset
62 'loglevel': 'INFO',
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 }
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 logging_config = """\
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 [loggers]
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 keys=root
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 [handlers]
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
69 keys=stderr
3112
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 [formatters]
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
72 keys=default
3112
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 [logger_root]
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 level=%(loglevel)s
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
76 handlers=stderr
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 [handler_stderr]
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 class=StreamHandler
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 level=NOTSET
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
81 formatter=default
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 args=(sys.stderr, )
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83
3505
cd43bbf56c33 logging: removed logging to ./moin.log from fallback config, better docs
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3487
diff changeset
84 [formatter_default]
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85 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
86 datefmt=
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 class=logging.Formatter
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 """
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
90 import os
4478
66e11e4ce6df make logging handlers defined in logging.handlers work (e.g. class=handlers.RotatingFileHandler)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4361
diff changeset
91 import logging
66e11e4ce6df make logging handlers defined in logging.handlers work (e.g. class=handlers.RotatingFileHandler)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4361
diff changeset
92 import logging.config
66e11e4ce6df make logging handlers defined in logging.handlers work (e.g. class=handlers.RotatingFileHandler)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4361
diff changeset
93 import logging.handlers # needed for handlers defined there being configurable in logging.conf file
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 configured = False
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 fallback_config = False
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97
4361
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
98 import warnings
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
99
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
100 # hashlib was added in python 2.5 - we can't use it as long as we do not require 2.5
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
101 # sha is deprecated since 2.5, but still present in 2.6
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
102 warnings.filterwarnings('ignore', r'the sha module is deprecated; use the hashlib module instead')
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
103
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
104 # 'CacheNeedsUpdate' string exception in Page.py is supported for backwards compat reasons:
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
105 warnings.filterwarnings('ignore', r'catching of string exceptions is deprecated', module='MoinMoin.Page')
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
106
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
107 # subprocess was added in python 2.4 - we can't use it as long as we do not require 2.4:
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
108 warnings.filterwarnings('ignore', r'The popen\d? module is deprecated. Use the subprocess module.')
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
109
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
110
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
111 def _log_warning(message, category, filename, lineno, file=None, line=None):
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
112 # for warnings, we just want to use the logging system, not stderr or other files
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
113 msg = "%s:%s: %s: %s" % (filename, lineno, category.__name__, message)
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
114 logger = getLogger(__name__)
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
115 logger.warning(msg) # Note: the warning will look like coming from here,
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
116 # but msg contains info about where it really comes from
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
117
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
118
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
119 def load_config(conf_fname=None):
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 """ 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
121 global configured
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
122 err_msg = None
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
123 conf_fname = os.environ.get('MOINLOGGINGCONF', conf_fname)
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
124 if conf_fname:
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
125 try:
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
126 conf_fname = os.path.abspath(conf_fname)
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
127 logging.config.fileConfig(conf_fname)
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
128 configured = True
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
129 l = getLogger(__name__)
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
130 l.info('using logging configuration read from "%s"' % conf_fname)
4361
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
131 warnings.showwarning = _log_warning
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
132 except Exception, err: # XXX be more precise
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
133 err_msg = str(err)
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
134 if not configured:
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
135 # load builtin fallback logging config
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
136 from StringIO import StringIO
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
137 config_file = StringIO(logging_config)
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
138 logging.config.fileConfig(config_file, logging_defaults)
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139 configured = True
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
140 l = getLogger(__name__)
3582
27ba7445b480 logging: fix typos
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3579
diff changeset
141 if err_msg:
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
142 l.warning('load_config for "%s" failed with "%s".' % (conf_fname, err_msg))
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
143 l.warning('using logging configuration read from built-in fallback in MoinMoin.log module!')
4361
fea6b6afce05 use logging framework for messages emitted by warnings module (e.g. DeprecationWarning), silence some specific warnings
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3582
diff changeset
144 warnings.showwarning = _log_warning
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 def getLogger(name):
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 """ 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
149 - preprocess logger name
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150 - 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
151 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
152 """
3579
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
153 if not configured:
ff4afe930cb2 logging configuration: you can use MOINLOGGINGCONF env var now to override from where your logging conf is read, fix wikiserverlogging.conf to not create empty moin.log files
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3505
diff changeset
154 load_config()
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 logger = logging.getLogger(name)
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156 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
157 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
158 setattr(logger, levelname, levelnumber)
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159 return logger
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160
3165
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
161
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
162 # Python 2.3's logging module has no .log, this provides it:
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
163 if not hasattr(logging, 'log'):
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
164 def log(level, msg, *args, **kwargs):
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
165 if len(logging.root.handlers) == 0:
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
166 logging.basicConfig()
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
167 if logging.root.manager.disable >= level:
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
168 return
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
169 if level >= logging.root.getEffectiveLevel():
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
170 logging.root._log(level, msg, args, **kwargs)
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
171 logging.log = log
b8dc6c2e6c53 integrate MoinMoin.support.logging_fix into MoinMoin.log
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3112
diff changeset
172