comparison MoinMoin/server/__init__.py @ 3105:80e1a910a2f1

make logging more configurable and flexible, packages/modules shall call MoinMoin.server.getLogger
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 23 Feb 2008 21:43:47 +0100
parents a78bf6977df8
children c6e39279f83b
comparison
equal deleted inserted replaced
3102:a78bf6977df8 3105:80e1a910a2f1
7 @copyright: 2004 Nir Soffer 7 @copyright: 2004 Nir Soffer
8 @license: GNU GPL, see COPYING for details. 8 @license: GNU GPL, see COPYING for details.
9 """ 9 """
10 10
11 import os 11 import os
12 import logging 12 from StringIO import StringIO
13 13
14 from MoinMoin import config 14 from MoinMoin import config
15
16 import logging as _logging
17
18 def configureLogging(conf, defaults):
19 from logging.config import fileConfig
20 fileConfig(StringIO(conf), defaults)
21
22 def getLogger(name):
23 logger = _logging.getLogger(name)
24 for levelnumber, levelname in _logging._levelNames.items():
25 if isinstance(levelnumber, int): # that list has also the reverse mapping...
26 setattr(logger, levelname, levelnumber)
27 return logger
28
29 logging = getLogger('')
15 30
16 def switchUID(uid, gid): 31 def switchUID(uid, gid):
17 """ Switch identity to safe user and group 32 """ Switch identity to safe user and group
18 33
19 Does not support Windows, because the necessary calls are not available. 34 Does not support Windows, because the necessary calls are not available.
51 docs = None # document root (if supported) 66 docs = None # document root (if supported)
52 user = None # user we shall use for running (if supported) 67 user = None # user we shall use for running (if supported)
53 group = None # group ... 68 group = None # group ...
54 port = None # tcp port number (if supported) 69 port = None # tcp port number (if supported)
55 70
56 # log levels for different log handlers 71 # Here you can configure the default logging used when running moin,
57 # None means "don't use this handler", otherwise specify the minimum loglevel, e.g. logging.DEBUG 72 # see http://www.python.org/doc/lib/logging-config-fileformat.html
58 # TODO: change later to an appropriate level, for now, we want everything 73 # We just use moin.log in current directory by default, if you want
59 loglevel_file = logging.DEBUG # None 74 # anything else, override logging_conf in your server script's Config class.
60 loglevel_stderr = logging.DEBUG # None 75 logging_defaults = {
61 logPath = None 76 'logdir': '.',
77 'loglevel': 'DEBUG',
78 }
79 logging_config = """\
80 [loggers]
81 keys=root
82
83 [handlers]
84 keys=logfile
85
86 [formatters]
87 keys=logfile
88
89 [logger_root]
90 level=%(loglevel)s
91 handlers=logfile
92
93 [handler_logfile]
94 class=StreamHandler
95 level=NOTSET
96 formatter=logfile
97 args=('%(logdir)s/moin.log', 'at')
98
99 [formatter_logfile]
100 format=%(asctime)s %(name)s %(levelname)s %(message)s
101 datefmt=
102 class=logging.Formatter
103 """
62 104
63 def __init__(self): 105 def __init__(self):
64 """ Validate and post process configuration values 106 """ Validate and post process configuration values
65 107
66 Will raise RuntimeError for any wrong config value. 108 Will raise RuntimeError for any wrong config value.
67 """ 109 """
68 # First, initialize the logging 110 # First, initialize the logging
69 logger = logging.getLogger('') # root logger 111 configureLogging(self.logging_config, self.logging_defaults)
70 logger.setLevel(logging.NOTSET) # otherwise it has WARNING by default!
71
72 if self.loglevel_file is not None and self.logPath is not None:
73 # define a Handler which writes to a log file
74 logfile = logging.FileHandler(self.logPath, 'at') # XXX we can't say ", 0" for sync here :(
75 logfile.setLevel(self.loglevel_file)
76 # set a format which is better for logfile use
77 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
78 # tell the handler to use this format
79 logfile.setFormatter(formatter)
80 # add the handler to the root logger
81 logger.addHandler(logfile)
82
83 if self.loglevel_stderr is not None:
84 # define a Handler which writes INFO to sys.stderr
85 logstderr = logging.StreamHandler()
86 logstderr.setLevel(self.loglevel_stderr)
87 # set a format which is simpler for console use
88 formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', '%H%M%S')
89 # tell the handler to use this format
90 logstderr.setFormatter(formatter)
91 # add the handler to the root logger
92 logger.addHandler(logstderr)
93 112
94 # Check that docs path is accessible 113 # Check that docs path is accessible
95 if self.docs: 114 if self.docs:
96 self.docs = os.path.normpath(os.path.abspath(self.docs)) 115 self.docs = os.path.normpath(os.path.abspath(self.docs))
97 if not os.access(self.docs, os.F_OK | os.R_OK | os.X_OK): 116 if not os.access(self.docs, os.F_OK | os.R_OK | os.X_OK):