pylorax: set up logging as recommended by logging module

Generally it's not a good idea for python libraries to set up loggers in
the body of the library.

Set up a NullHandler by default (as the logging module suggests), and
add a function to do the current logging setup during run().
This commit is contained in:
Will Woods 2012-06-13 18:22:57 -04:00
parent 00272dec94
commit 329432b58e

View File

@ -23,12 +23,7 @@
# set up logging # set up logging
import logging import logging
logger = logging.getLogger("pylorax") logger = logging.getLogger("pylorax")
logger.setLevel(logging.DEBUG) logger.addHandler(logging.NullHandler())
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
logger.addHandler(sh)
import sys import sys
import os import os
@ -125,6 +120,11 @@ class Lorax(BaseLoraxClass):
self._configured = True self._configured = True
def init_stream_logging(self):
sh = logging.StreamHandler()
sh.setLevel(logging.INFO)
logger.addHandler(sh)
def init_file_logging(self, logdir, logname="pylorax.log"): def init_file_logging(self, logdir, logname="pylorax.log"):
fh = logging.FileHandler(filename=joinpaths(logdir, logname), mode="w") fh = logging.FileHandler(filename=joinpaths(logdir, logname), mode="w")
fh.setLevel(logging.DEBUG) fh.setLevel(logging.DEBUG)
@ -153,6 +153,7 @@ class Lorax(BaseLoraxClass):
if not os.path.isdir(logdir): if not os.path.isdir(logdir):
os.makedirs(logdir) os.makedirs(logdir)
self.init_stream_logging()
self.init_file_logging(logdir) self.init_file_logging(logdir)
logger.debug("using work directory {0.workdir}".format(self)) logger.debug("using work directory {0.workdir}".format(self))
logger.debug("using log directory {0}".format(logdir)) logger.debug("using log directory {0}".format(logdir))