comparison MoinMoin/log.py @ 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
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 11 May 2008 22:38:06 +0200
parents cd43bbf56c33
children 27ba7445b480
comparison
equal deleted inserted replaced
3578:9cb46c423384 3579:ff4afe930cb2
2 """ 2 """
3 MoinMoin - init "logging" system 3 MoinMoin - init "logging" system
4 4
5 WARNING 5 WARNING
6 ------- 6 -------
7 logging must be configured VERY early, before any moin module calls 7 logging must be configured VERY early, before the code in log.getLogger
8 log.getLogger(). Because most modules call getLogger on the module 8 gets executed. Thus, logging is configured either by:
9 level, this basically means that MoinMoin.log must be imported first 9 a) an environment variable MOINLOGGINGCONF that contains the path/filename
10 and load_config must be called afterwards, before any other moin 10 of a logging configuration file - this method overrides all following
11 module gets imported. 11 methods (except if it can't read or use that configuration, then it
12 will use c))
13 b) by an explicit call to MoinMoin.log.load_config('logging.conf') -
14 you need to do this very early or a) or c) will happen before
15 c) by using a builtin fallback logging conf
16
17 If logging is not yet configured, log.getLogger will do an implicit
18 configuration call - then a) or c) is done.
12 19
13 Usage (for wiki server admins) 20 Usage (for wiki server admins)
14 ------------------------------ 21 ------------------------------
15 Typically, your server adaptor script (e.g. moin.cgi) will have this: 22 Either use something like this in some shell script:
23 MOINLOGGINGCONF=/path/to/logging.conf
24 export MOINLOGGINGCONF
25
26 Or, modify your server adaptor script (e.g. moin.cgi) to do this:
16 27
17 from MoinMoin import log 28 from MoinMoin import log
18 log.load_config('wiki/config/logging/logfile') # XXX please fix this path! 29 log.load_config('wiki/config/logging/logfile') # XXX please fix this path!
19 30
20 You have to fix that path to use a logging configuration matching your 31 You have to fix that path to use a logging configuration matching your
74 format=%(asctime)s %(levelname)s %(name)s:%(lineno)d %(message)s 85 format=%(asctime)s %(levelname)s %(name)s:%(lineno)d %(message)s
75 datefmt= 86 datefmt=
76 class=logging.Formatter 87 class=logging.Formatter
77 """ 88 """
78 89
90 import os
79 import logging, logging.config 91 import logging, logging.config
80 92
81 configured = False 93 configured = False
82 fallback_config = False 94 fallback_config = False
83 95
84 96
85 def load_config(conf_fname): 97 def load_config(conf_fname=None):
86 """ load logging config from conffile """ 98 """ load logging config from conffile """
87 global configured 99 global configured
88 try: 100 err_msg = None
89 logging.config.fileConfig(conf_fname) 101 conf_fname = os.environ.get('MOINLOGGINGCONF', conf_fname)
102 if conf_fname:
103 try:
104 conf_fname = os.path.abspath(conf_fname)
105 logging.config.fileConfig(conf_fname)
106 configured = True
107 l = getLogger(__name__)
108 l.info('using logging configuration read from "%s"' % conf_fname)
109 except Exception, err: # XXX be more precise
110 err_msg = str(err)
111 if not configured:
112 # load builtin fallback logging config
113 from StringIO import StringIO
114 config_file = StringIO(logging_config)
115 logging.config.fileConfig(config_file, logging_defaults)
90 configured = True 116 configured = True
91 except Exception, err: # XXX be more precise 117 l = getLogger(__name__)
92 load_fallback_config(err) 118 if err:
93 119 l.warning('load_config for "%s" failed with "%s".' % (conf_fname, err_msg))
94 def load_fallback_config(err=None): 120 l.warning('using logging configuration read from built-in fallback in MoinMoin.log module!')
95 """ load builtin fallback logging config """
96 global configured
97 from StringIO import StringIO
98 logging.config.fileConfig(StringIO(logging_config), logging_defaults)
99 configured = True
100 l = getLogger(__name__)
101 l.warning('Using built-in fallback logging configuration!')
102 if err:
103 l.warning('load_config failed with "%s".' % str(err))
104 121
105 122
106 def getLogger(name): 123 def getLogger(name):
107 """ wrapper around logging.getLogger, so we can do some more stuff: 124 """ wrapper around logging.getLogger, so we can do some more stuff:
108 - preprocess logger name 125 - preprocess logger name
109 - patch loglevel constants into logger object, so it can be used 126 - patch loglevel constants into logger object, so it can be used
110 instead of the logging module 127 instead of the logging module
111 """ 128 """
112 if not configured: # should not happen 129 if not configured:
113 load_fallback_config() 130 load_config()
114 logger = logging.getLogger(name) 131 logger = logging.getLogger(name)
115 for levelnumber, levelname in logging._levelNames.items(): 132 for levelnumber, levelname in logging._levelNames.items():
116 if isinstance(levelnumber, int): # that list has also the reverse mapping... 133 if isinstance(levelnumber, int): # that list has also the reverse mapping...
117 setattr(logger, levelname, levelnumber) 134 setattr(logger, levelname, levelnumber)
118 return logger 135 return logger