Logging question
Diez B. Roggisch
deets at nospam.web.de
Wed Sep 23 03:02:46 EDT 2009
Gabor Urban schrieb:
> Hi guys,
>
> I have embarassing problem using the logging module. I would like to
> encapsulate the creation and setting up of the logger in a class, but
> it does not seem working.
>
> Here are my relevant parts of the code:
>
> --
> import sys
> import logging
>
> class LogClass:
> def __init__(self, fileName, loggerName = 'classLog'):
> self.Logger = logging.getLogger(loggerName)
> self.traceName = fileName
> handler = logging.FileHandler(self.traceName,'a')
> formatter = logging.Formatter("%(name)s %(asctime)s
> %(filename)s %(lineno)d %(levelname)s %(message)s")
> handler.setFormatter(formatter)
> self.Logger.addHandler(handler)
> self.Handler = handler
>
> def closeLog(self):
> self.Handler.flush()
> self.Handler.close()
>
> def fetchLogger(self):
> return self.Logger
>
> if __name__ == "__main__":
> name = 'testlog.trc'
> classLog = LogClass(name)
> logger = classLog.fetchLogger()
> logger.info("Created")
> logger.debug("Test")
> logger.info("Created .. ")
> logger.debug("Test data")
> classLog.closeLog()
>
> --
>
> The trace file is created properly but contains no lines at all. If I
> put the code directly in __main__, it works fine.
>
> What did I miss? Any ideas are wellcome.
That the default level is less than INFO - if you set that to e.g.
logging.DEBUG.
However, I think there are a few problems here beside that. For once,
reading PEP8 might be worth considering.
And the logging-module is written so that setting it up & using it are
de-coupled. Which you foil here somewhat. What is the above supposed to do?
Diez
More information about the Python-list
mailing list