diff 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
line wrap: on
line diff
--- a/MoinMoin/server/__init__.py	Sat Feb 23 16:14:04 2008 +0100
+++ b/MoinMoin/server/__init__.py	Sat Feb 23 21:43:47 2008 +0100
@@ -9,10 +9,25 @@
 """
 
 import os
-import logging
+from StringIO import StringIO
 
 from MoinMoin import config
 
+import logging as _logging
+
+def configureLogging(conf, defaults):
+   from logging.config import fileConfig
+   fileConfig(StringIO(conf), defaults)
+
+def getLogger(name):
+    logger = _logging.getLogger(name)
+    for levelnumber, levelname in _logging._levelNames.items():
+        if isinstance(levelnumber, int): # that list has also the reverse mapping...
+            setattr(logger, levelname, levelnumber)
+    return logger
+
+logging = getLogger('')
+
 def switchUID(uid, gid):
     """ Switch identity to safe user and group
 
@@ -53,12 +68,39 @@
     group = None # group ...
     port = None # tcp port number (if supported)
 
-    # log levels for different log handlers
-    # None means "don't use this handler", otherwise specify the minimum loglevel, e.g. logging.DEBUG
-    # TODO: change later to an appropriate level, for now, we want everything
-    loglevel_file = logging.DEBUG # None
-    loglevel_stderr = logging.DEBUG # None
-    logPath = None
+    # Here you can configure the default logging used when running moin,
+    # see http://www.python.org/doc/lib/logging-config-fileformat.html
+    # We just use moin.log in current directory by default, if you want
+    # anything else, override logging_conf in your server script's Config class.
+    logging_defaults = {
+        'logdir': '.',
+        'loglevel': 'DEBUG',
+}
+    logging_config = """\
+[loggers]
+keys=root
+
+[handlers]
+keys=logfile
+
+[formatters]
+keys=logfile
+
+[logger_root]
+level=%(loglevel)s
+handlers=logfile
+
+[handler_logfile]
+class=StreamHandler
+level=NOTSET
+formatter=logfile
+args=('%(logdir)s/moin.log', 'at')
+
+[formatter_logfile]
+format=%(asctime)s %(name)s %(levelname)s %(message)s
+datefmt=
+class=logging.Formatter
+"""
 
     def __init__(self):
         """ Validate and post process configuration values
@@ -66,30 +108,7 @@
         Will raise RuntimeError for any wrong config value.
         """
         # First, initialize the logging
-        logger = logging.getLogger('') # root logger
-        logger.setLevel(logging.NOTSET) # otherwise it has WARNING by default!
-
-        if self.loglevel_file is not None and self.logPath is not None:
-            # define a Handler which writes to a log file
-            logfile = logging.FileHandler(self.logPath, 'at') # XXX we can't say ", 0" for sync here :(
-            logfile.setLevel(self.loglevel_file)
-            # set a format which is better for logfile use
-            formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
-            # tell the handler to use this format
-            logfile.setFormatter(formatter)
-            # add the handler to the root logger
-            logger.addHandler(logfile)
-
-        if self.loglevel_stderr is not None:
-            # define a Handler which writes INFO to sys.stderr
-            logstderr = logging.StreamHandler()
-            logstderr.setLevel(self.loglevel_stderr)
-            # set a format which is simpler for console use
-            formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s', '%H%M%S')
-            # tell the handler to use this format
-            logstderr.setFormatter(formatter)
-            # add the handler to the root logger
-            logger.addHandler(logstderr)
+        configureLogging(self.logging_config, self.logging_defaults)
 
         # Check that docs path is accessible
         if self.docs: