From 329432b58ee9b9fbf695df474cd3b90766b0497e Mon Sep 17 00:00:00 2001 From: Will Woods Date: Wed, 13 Jun 2012 18:22:57 -0400 Subject: [PATCH] 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(). --- src/pylorax/__init__.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/pylorax/__init__.py b/src/pylorax/__init__.py index 72a08f85..79d477d3 100644 --- a/src/pylorax/__init__.py +++ b/src/pylorax/__init__.py @@ -23,12 +23,7 @@ # set up logging import logging logger = logging.getLogger("pylorax") -logger.setLevel(logging.DEBUG) - -sh = logging.StreamHandler() -sh.setLevel(logging.INFO) -logger.addHandler(sh) - +logger.addHandler(logging.NullHandler()) import sys import os @@ -125,6 +120,11 @@ class Lorax(BaseLoraxClass): 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"): fh = logging.FileHandler(filename=joinpaths(logdir, logname), mode="w") fh.setLevel(logging.DEBUG) @@ -153,6 +153,7 @@ class Lorax(BaseLoraxClass): if not os.path.isdir(logdir): os.makedirs(logdir) + self.init_stream_logging() self.init_file_logging(logdir) logger.debug("using work directory {0.workdir}".format(self)) logger.debug("using log directory {0}".format(logdir))