diff MoinMoin/server/__init__.py @ 1585:a843bc5793fb

introduce stdlib's logging module
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 25 Sep 2006 22:17:59 +0200
parents 1a1d6e0fe14f
children bb2e053067fb
line wrap: on
line diff
--- a/MoinMoin/server/__init__.py	Mon Sep 25 16:15:57 2006 +0200
+++ b/MoinMoin/server/__init__.py	Mon Sep 25 22:17:59 2006 +0200
@@ -9,6 +9,8 @@
 """
 
 import os
+import logging
+
 from MoinMoin import config
 
 def switchUID(uid, gid):
@@ -32,8 +34,7 @@
         # those calls.
         raise RuntimeError("can't change uid/gid to %s/%s" %
                            (uid, gid))
-    import sys
-    sys.stderr.write("Running as uid/gid %d/%d\n" % (uid, gid))
+    logging.info("Running as uid/gid %d/%d" % (uid, gid))
 
 
 class Config:
@@ -52,11 +53,45 @@
     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
+
     def __init__(self):
         """ Validate and post process configuration values
 
         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)
+
+        logging.info("logging initialized")
 
         # Check that docs path is accessible
         if self.docs: