annotate MoinMoin/log.py @ 5943:51ea4cb7b390

at startup, announce moin version and code path
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 19 Feb 2013 23:59:31 +0100
parents c14974df2b58
children
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_config = """\
5931
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
62 [DEFAULT]
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
63 # Default loglevel, to adjust verbosity: DEBUG, INFO, WARNING, ERROR, CRITICAL
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
64 loglevel=INFO
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
65
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 [loggers]
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 keys=root
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 [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
70 keys=stderr
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 [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
73 keys=default
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 [logger_root]
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 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
77 handlers=stderr
3112
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 [handler_stderr]
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 class=StreamHandler
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81 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
82 formatter=default
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 args=(sys.stderr, )
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84
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
85 [formatter_default]
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86 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
87 datefmt=
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 class=logging.Formatter
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 """
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90
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
91 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
92 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
93 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
94 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
95
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 configured = False
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 fallback_config = False
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98
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
99 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
100
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 # '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
102 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
103
4583
124c1919133d added TODO for removing popen and replace it by subprocess
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4582
diff changeset
104 # TODO: subprocess was added in python 2.4, we now can refactor the code to use it and remove this:
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
105 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
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
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 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
109 # 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
110 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
111 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
112 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
113 # 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
114
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115
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
116 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
117 """ 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
118 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
119 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
120 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
121 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
122 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
123 conf_fname = os.path.abspath(conf_fname)
5916
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
124 # we open the conf file here to be able to give a reasonable
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
125 # error message in case of failure (if we give the filename to
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
126 # fileConfig(), it silently ignores unreadable files and gives
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
127 # unhelpful error msgs like "No section: 'formatters'"):
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
128 f = open(conf_fname)
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
129 try:
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
130 logging.config.fileConfig(f)
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
131 finally:
881d9053592a logging: if the logging config file can't be read, give a helpful error msg
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4583
diff changeset
132 f.close()
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
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 # 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
141 from StringIO import StringIO
5931
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
142 f = StringIO(logging_config)
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
143 try:
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
144 logging.config.fileConfig(f)
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
145 finally:
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
146 f.close()
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 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
148 l = getLogger(__name__)
3582
27ba7445b480 logging: fix typos
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3579
diff changeset
149 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
150 l.warning('load_config for "%s" failed with "%s".' % (conf_fname, err_msg))
5931
c14974df2b58 logging configuration: use info loglevel for telling about using the builtin default logging config
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5916
diff changeset
151 l.info('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
152 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
153
5943
51ea4cb7b390 at startup, announce moin version and code path
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5931
diff changeset
154 import MoinMoin
51ea4cb7b390 at startup, announce moin version and code path
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5931
diff changeset
155 code_path = os.path.dirname(MoinMoin.__file__)
51ea4cb7b390 at startup, announce moin version and code path
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5931
diff changeset
156 from MoinMoin.version import project, release, revision
51ea4cb7b390 at startup, announce moin version and code path
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5931
diff changeset
157 l.info('Running %s %s %s code from %s' % (project, release, revision, code_path))
51ea4cb7b390 at startup, announce moin version and code path
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 5931
diff changeset
158
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 def getLogger(name):
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 """ 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
162 - preprocess logger name
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163 - 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
164 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
165 """
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
166 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
167 load_config()
3112
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
168 logger = logging.getLogger(name)
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
169 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
170 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
171 setattr(logger, levelname, levelnumber)
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172 return logger
dc182f4936b1 logging: add missing file for previous changeset 79f850129782)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
173