dabo Commit
Revision 5975
Date: 2010-09-05 10:23:48 -0700 (Sun, 05 Sep 2010)
Author: Ed
Trac: http://trac.dabodev.com/changeset/5975

Changed:
U   trunk/dabo/__init__.py
U   trunk/dabo/logging.conf.sample
U   trunk/dabo/settings.py

Log:
Updated the logging changes to not attempt to create any file-based logs. This 
should fix some of the problems reported on Windows.

Changed the dbActivityLog to mirror the mainLog: it has provisions for both a 
console and a file-based handler. This will allow you to better control db 
activity logging.

Renamed the log handlers to include 'Handler' in their name. I think that it is 
much more clear that 'consoleLogHandler' is a handler; the old name of 
'consoleLog' would lead one to believe it is the log itself.


Diff:
Modified: trunk/dabo/__init__.py
===================================================================
--- trunk/dabo/__init__.py      2010-09-01 12:58:01 UTC (rev 5974)
+++ trunk/dabo/__init__.py      2010-09-05 17:23:48 UTC (rev 5975)
@@ -127,6 +127,9 @@
 # we want to make them part of the dabo namespace.
 from settings import *
 
+# These are the various standard log handlers.
+consoleLogHandler = fileLogHandler = None
+dbConsoleLogHandler = dbFileLogHandler = None
 # See if a logging.conf file exists in either the current directory or
 # the base directory for the dabo module. If such a file is found, use
 # it to configure logging. Otherwise, use the values gotten from
@@ -138,57 +141,120 @@
        _daboloc = os.path.dirname(__file__)
        _logConfFile = os.path.join(_daboloc, _logConfFileName)
 if os.path.exists(_logConfFile):
+       # If a 'logging.conf' file exists, use it instead of dabo.settings.
        import logging.config
        logging.config.fileConfig(_logConfFile)
        # Populate the module namespace with the appropriate loggers
        log = logging.getLogger(mainLogQualName)
        dbActivityLog = logging.getLogger(dbLogQualName)
-       consoleLog = fileLog = dbLog = None
        for _handler in log.handlers:
                try:
                        _handler.baseFilename
-                       fileLog = _handler
+                       fileLogHandler = _handler
                except AttributeError:
-                       consoleLog = _handler
+                       consoleLogHandler = _handler
        for _handler in dbActivityLog.handlers:
                try:
                        _handler.baseFilename
-                       dbLog = _handler
+                       dbFileLogHandler = _handler
                        break
                except AttributeError:
-                       pass
+                       dbConsoleLogHandler = _handler
 else:
        # Use dabo.settings values to configure the logs
-       consoleLog = logging.StreamHandler()
-       consoleLog.setLevel(mainLogConsoleLevel)
-       fileLog = logging.handlers.RotatingFileHandler(filename=mainLogFile, 
maxBytes=maxLogFileSize,
-                       encoding=defaultEncoding)
-       fileLog.setLevel(mainLogFileLevel)
+       consoleLogHandler = logging.StreamHandler()
+       consoleLogHandler.setLevel(mainLogConsoleLevel)
        consoleFormatter = logging.Formatter(consoleFormat)
        consoleFormatter.datefmt = mainLogDateFormat
-       fileFormatter = logging.Formatter(fileFormat)
-       fileFormatter.datefmt = mainLogDateFormat
-       consoleLog.setFormatter(consoleFormatter)
-       fileLog.setFormatter(fileFormatter)
+       consoleLogHandler.setFormatter(consoleFormatter)
        log = logging.getLogger(mainLogQualName)
        log.setLevel(logging.DEBUG)
-       log.addHandler(consoleLog)
-       log.addHandler(fileLog)
+       log.addHandler(consoleLogHandler)
+       if mainLogFile is not None:
+               fileLogHandler = 
logging.handlers.RotatingFileHandler(filename=mainLogFile,
+                               maxBytes=maxLogFileSize, 
encoding=defaultEncoding)
+               fileLogHandler.setLevel(mainLogFileLevel)
+               fileFormatter = logging.Formatter(fileFormat)
+               fileFormatter.datefmt = mainLogDateFormat
+               fileLogHandler.setFormatter(fileFormatter)
+               log.addHandler(fileLogHandler)
        
+       dbConsoleLogHandler = logging.StreamHandler()
+       dbConsoleLogHandler.setLevel(dbLogConsoleLevel)
+       dbConsoleFormatter = logging.Formatter(dbConsoleFormat)
+       dbConsoleFormatter.datefmt = dbLogDateFormat
+       dbConsoleLogHandler.setFormatter(dbConsoleFormatter)
        dbActivityLog = logging.getLogger(dbLogQualName)
-       dbLog = logging.handlers.RotatingFileHandler(filename=dbLogFile, 
maxBytes=maxLogFileSize,
-                       encoding=defaultEncoding)
-       dbActivityLog.addHandler(dbLog)
-       dbActivityLog.setLevel(dbLogFileLevel)
-       dbLog.setLevel(dbLogFileLevel)
+       dbActivityLog.setLevel(dbLogLevel)
+       dbActivityLog.addHandler(dbConsoleLogHandler)
+       if dbLogFile is not None:
+               dbFileLogHandler = 
logging.handlers.RotatingFileHandler(filename=dbLogFile,
+                               maxBytes=maxLogFileSize, 
encoding=defaultEncoding)
+               dbFileLogHandler.setLevel(dbLogFileLevel)
+               dbFileFormatter = logging.Formatter(dbFileFormat)
+               dbFileFormatter.datefmt = dbLogDateFormat
+               dbFileLogHandler.setFormatter(dbFileFormatter)
+               dbActivityLog.addHandler(dbFileLogHandler)
 
 
-########################################################
-#### The commented out code was a first attempt at using Python logging, but 
with
-#### the dabo.settings file being used to configure instead of logging.conf
-########################################################
-########################################################
+def setMainLogFile(fname, level=None):
+       """Create the main file-based logger for the framework, and optionally
+       set the log level. If the passed 'fname' is None, any existing 
file-based
+       logger will be closed.
+       """
+       if fname is None:
+               if dabo.fileLogHandler:
+                       # Remove the existing handler
+                       dabo.log.removeHandler(dabo.fileLogHandler)
+                       dabo.fileLogHandler.close()
+                       dabo.fileLogHandler = None
+       else:
+               if dabo.fileLogHandler:
+                       # Close the existing handler first
+                       dabo.log.removeHandler(dabo.fileLogHandler)
+                       dabo.fileLogHandler.close()
+                       dabo.fileLogHandler = None
+               dabo.fileLogHandler = 
logging.handlers.RotatingFileHandler(filename=fname,
+                               maxBytes=dabo.maxLogFileSize, 
encoding=dabo.defaultEncoding)
+               if level:
+                       dabo.fileLogHandler.setLevel(level)
+               else:
+                       dabo.fileLogHandler.setLevel(dabo.mainLogFileLevel)
+               dabo.fileFormatter = logging.Formatter(dabo.fileFormat)
+               dabo.fileFormatter.datefmt = dabo.mainLogDateFormat
+               dabo.fileLogHandler.setFormatter(dabo.fileFormatter)
+               dabo.log.addHandler(dabo.fileLogHandler)
 
+
+def setDbLogFile(fname, level=None):
+       """Create the dbActivity file-based logger for the framework, and 
optionally
+       set the log level. If the passed 'fname' is None, any existing 
file-based
+       logger will be closed.
+       """
+       if fname is None:
+               if dabo.dbFileLogHandler:
+                       # Remove the existing handler
+                       dabo.dbActivityLog.removeHandler(dabo.dbFileLogHandler)
+                       dabo.dbFileLogHandler.close()
+                       dabo.dbFileLogHandler = None
+       else:
+               if dabo.dbFileLogHandler:
+                       # Close the existing handler first
+                       dabo.dbActivityLog.removeHandler(dabo.dbFileLogHandler)
+                       dabo.dbFileLogHandler.close()
+                       dabo.dbFileLogHandler = None
+               dabo.dbFileLogHandler = 
logging.handlers.RotatingFileHandler(filename=fname,
+                               maxBytes=dabo.maxLogFileSize, 
encoding=dabo.defaultEncoding)
+               if level:
+                       dabo.dbFileLogHandler.setLevel(level)
+               else:
+                       dabo.dbFileLogHandler.setLevel(dabo.mainLogFileLevel)
+               dabo.dbFileFormatter = logging.Formatter(dabo.dbFileFormat)
+               dabo.dbFileFormatter.datefmt = dabo.dbLogDateFormat
+               dabo.dbFileLogHandler.setFormatter(dabo.dbFileFormatter)
+               dabo.dbActivityLog.addHandler(dabo.dbFileLogHandler)
+
+
 # Install localization service for dabo. dApp will install localization service
 # for the user application separately.
 import dLocalize

Modified: trunk/dabo/logging.conf.sample
===================================================================
--- trunk/dabo/logging.conf.sample      2010-09-01 12:58:01 UTC (rev 5974)
+++ trunk/dabo/logging.conf.sample      2010-09-05 17:23:48 UTC (rev 5975)
@@ -1,3 +1,4 @@
+
 [loggers]
 keys=root,daboLog,dbActivity
 
@@ -2,3 +3,3 @@
 [handlers]
-keys=consoleHandler,fileHandler,dbHandler
+keys=consoleHandler,fileHandler,dbConsoleHandler,dbFileHandler
 
@@ -19,7 +20,7 @@
 
 [logger_dbActivity]
 level=DEBUG
-handlers=dbHandler
+handlers=dbConsoleHandler,dbFileHandler
 qualname=dabo.dbActivityLog
 propagate=0
 
@@ -35,7 +36,13 @@
 formatter=basicFormatter
 args=("dabo.log", "a", 5242880, 7, "utf8")
 
-[handler_dbHandler]
+[handler_dbConsoleHandler]
+class=StreamHandler
+level=ERROR
+formatter=basicFormatter
+args=(sys.stdout, )
+
+[handler_dbFileHandler]
 class=handlers.RotatingFileHandler
 level=DEBUG
 formatter=basicFormatter

Modified: trunk/dabo/settings.py
===================================================================
--- trunk/dabo/settings.py      2010-09-01 12:58:01 UTC (rev 5974)
+++ trunk/dabo/settings.py      2010-09-05 17:23:48 UTC (rev 5975)
@@ -183,18 +183,23 @@
 # Logging settings
 mainLogLevel = logging.DEBUG
 mainLogQualName = "dabo.mainLog"
-# Set the main log file to the null device initially
-mainLogFile = os.devnull
+# Set the main log file to None initially
+mainLogFile = None
 mainLogConsoleLevel = logging.ERROR
 mainLogFileLevel = logging.ERROR
 mainLogDateFormat = "%Y-%m-%d %H:%M:%S"
-consoleFormat = "%(asctime)s - %(levelname)s - %(message)s"
-fileFormat = "%(asctime)s - %(levelname)s - %(message)s"
-# Set the db file to the null device initially
-dbLogFile = os.devnull
+consoleFormat = fileFormat = "%(asctime)s - %(levelname)s - %(message)s"
+maxLogFileSize = 5242880               # 5 MB
+
+dbLogLevel = logging.DEBUG
 dbLogQualName = "dabo.dbActivityLog"
+# Set the db file to None initially
+dbLogFile = None
+dbLogConsoleLevel = logging.ERROR
 dbLogFileLevel = logging.DEBUG
-maxLogFileSize = 5242880               # 5 MB
+dbLogDateFormat = "%Y-%m-%d %H:%M:%S"
+dbConsoleFormat = dbFileFormat = "%(asctime)s - %(levelname)s - %(message)s"
+dbMaxLogFileSize = 5242880             # 5 MB
 
 
 ### Settings - end



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to