changeset 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 9cb46c423384
children fc2f05f2b0bd
files MoinMoin/log.py docs/CHANGES wikiserverlogging.conf
diffstat 3 files changed, 47 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/log.py	Sun May 11 18:55:59 2008 +0200
+++ b/MoinMoin/log.py	Sun May 11 22:38:06 2008 +0200
@@ -4,15 +4,26 @@
 
     WARNING
     -------
-    logging must be configured VERY early, before any moin module calls
-    log.getLogger(). Because most modules call getLogger on the module
-    level, this basically means that MoinMoin.log must be imported first
-    and load_config must be called afterwards, before any other moin
-    module gets imported.
+    logging must be configured VERY early, before the code in log.getLogger
+    gets executed. Thus, logging is configured either by:
+    a) an environment variable MOINLOGGINGCONF that contains the path/filename
+       of a logging configuration file - this method overrides all following
+       methods (except if it can't read or use that configuration, then it
+       will use c))
+    b) by an explicit call to MoinMoin.log.load_config('logging.conf') -
+       you need to do this very early or a) or c) will happen before
+    c) by using a builtin fallback logging conf
+    
+    If logging is not yet configured, log.getLogger will do an implicit
+    configuration call - then a) or c) is done.
 
     Usage (for wiki server admins)
     ------------------------------
-    Typically, your server adaptor script (e.g. moin.cgi) will have this:
+    Either use something like this in some shell script:
+    MOINLOGGINGCONF=/path/to/logging.conf
+    export MOINLOGGINGCONF
+
+    Or, modify your server adaptor script (e.g. moin.cgi) to do this:
 
     from MoinMoin import log
     log.load_config('wiki/config/logging/logfile') # XXX please fix this path!
@@ -76,31 +87,37 @@
 class=logging.Formatter
 """
 
+import os
 import logging, logging.config
 
 configured = False
 fallback_config = False
 
 
-def load_config(conf_fname):
+def load_config(conf_fname=None):
     """ load logging config from conffile """
     global configured
-    try:
-        logging.config.fileConfig(conf_fname)
+    err_msg = None
+    conf_fname = os.environ.get('MOINLOGGINGCONF', conf_fname)
+    if conf_fname:
+        try:
+            conf_fname = os.path.abspath(conf_fname)
+            logging.config.fileConfig(conf_fname)
+            configured = True
+            l = getLogger(__name__)
+            l.info('using logging configuration read from "%s"' % conf_fname)
+        except Exception, err: # XXX be more precise
+            err_msg = str(err)
+    if not configured:
+        # load builtin fallback logging config
+        from StringIO import StringIO
+        config_file = StringIO(logging_config)
+        logging.config.fileConfig(config_file, logging_defaults)
         configured = True
-    except Exception, err: # XXX be more precise
-        load_fallback_config(err)
-
-def load_fallback_config(err=None):
-    """ load builtin fallback logging config """
-    global configured
-    from StringIO import StringIO
-    logging.config.fileConfig(StringIO(logging_config), logging_defaults)
-    configured = True
-    l = getLogger(__name__)
-    l.warning('Using built-in fallback logging configuration!')
-    if err:
-        l.warning('load_config failed with "%s".' % str(err))
+        l = getLogger(__name__)
+        if err:
+            l.warning('load_config for "%s" failed with "%s".' % (conf_fname, err_msg))
+        l.warning('using logging configuration read from built-in fallback in MoinMoin.log module!')
 
 
 def getLogger(name):
@@ -109,8 +126,8 @@
         - patch loglevel constants into logger object, so it can be used
           instead of the logging module
     """
-    if not configured: # should not happen
-        load_fallback_config()
+    if not configured:
+        load_config()
     logger = logging.getLogger(name)
     for levelnumber, levelname in logging._levelNames.items():
         if isinstance(levelnumber, int): # that list has also the reverse mapping...
--- a/docs/CHANGES	Sun May 11 18:55:59 2008 +0200
+++ b/docs/CHANGES	Sun May 11 22:38:06 2008 +0200
@@ -92,7 +92,9 @@
   New Features:
     * New powerful and flexible logging, please see wiki/config/logging/ -
       HINT: you have to upgrade your server adaptor script (e.g. moin.cgi)
-      and load a logging configuration that fits your needs from there.
+      and load a logging configuration that fits your needs from there, or
+      alternatively you can also set MOINLOGGINGCONF environment variable
+      to point at your logging config file.
       If you use some of our sample logging configs, make sure you have a
       look INTO them to fix e.g. the path of the logfile it will use.
     * Moin now logs the path from where it loaded its configuration(s)
--- a/wikiserverlogging.conf	Sun May 11 18:55:59 2008 +0200
+++ b/wikiserverlogging.conf	Sun May 11 22:38:06 2008 +0200
@@ -20,7 +20,8 @@
 propagate=0
 
 [handlers]
-keys=logfile,stderr
+#keys=logfile,stderr
+keys=stderr
 
 [handler_logfile]
 class=FileHandler
@@ -38,7 +39,7 @@
 keys=default
 
 [formatter_default]
-format=%(asctime)s %(name)s %(levelname)s %(message)s
+format=%(asctime)s %(levelname)s %(name)s:%(lineno)d %(message)s
 datefmt=
 class=logging.Formatter