467,161 Members | 889 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

Why do all my loggers start auto-disabled?

I am a relative newbie to Python and its logging infrastructure;
however, I have programmed extensively with Java/C# and log4j and
log4net. That, I suppose, could be the root of my problem :)

I am trying to setup one logger per module (as this is roughly
analogous to how I've used log4j/log4net). However, I've noticed that
my loggers have the disabled flag set to true (more accurately, 1)
when they are initialized. For each logger, do I have to force
disabled = False? To make this more concrete, here is an example.

In example.py:

import logging
import logging.config

_log = logging.getLogger("prediction.predict")

def main():
logging.config.fileConfig('logconfig.conf')
_log.warn("test warning!")

if __name__ == "__main__":
main()

Unfortunately, this does not print out "test warning!" and if I put a
breakpoint at the _log.warn("test warning!") line, _log.disabled
equals 1. If I change main to:

def main():
logging.config.fileConfig('predict_logconfig.conf' )
_log.disabled = False
_log.warn("test warning!")

It works! So, am I doing something wrong or do I always have to force
my loggers to be enabled? Should I update my configuration file so
that loggers start as enabled? Here is my config file
(logconfig.conf):

[formatters]
keys: detailed,simple

[handlers]
keys: console,filelog

[loggers]
keys: root

[formatter_simple]
format: %(levelname)s : %(module)s.%(funcName)s (%(lineno)d): %
(message)s

[formatter_detailed]
format: %(asctime)s %(levelname)s : %(module)s.%(funcName)s (%
(lineno)d): %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: simple

[handler_filelog]
class: FileHandler
args: ["predictionLog.txt"]
formatter: detailed

[logger_root]
level: INFO
handlers: filelog, console

Aug 25 '08 #1
  • viewed: 3190
Share:
2 Replies
jonfroehlich wrote:
I am a relative newbie to Python and its logging infrastructure;
however, I have programmed extensively with Java/C# and log4j and
log4net. That, I suppose, could be the root of my problem :)

I am trying to setup one logger per module (as this is roughly
analogous to how I've used log4j/log4net). However, I've noticed that
my loggers have the disabled flag set to true (more accurately, 1)
when they are initialized. For each logger, do I have to force
disabled = False? To make this more concrete, here is an example.

In example.py:

import logging
import logging.config

_log = logging.getLogger("prediction.predict")

def main():
logging.config.fileConfig('logconfig.conf')
_log.warn("test warning!")

if __name__ == "__main__":
main()

Unfortunately, this does not print out "test warning!" and if I put a
breakpoint at the _log.warn("test warning!") line, _log.disabled
equals 1. If I change main to:

def main():
logging.config.fileConfig('predict_logconfig.conf' )
_log.disabled = False
_log.warn("test warning!")

It works! So, am I doing something wrong or do I always have to force
my loggers to be enabled? Should I update my configuration file so
that loggers start as enabled? Here is my config file
I think you have to put the logger into your config file to avoid that it
will be disabled by fileConfig():
(logconfig.conf):

[formatters]
keys: detailed,simple

[handlers]
keys: console,filelog
[loggers]
keys: root, predict

# ...

[logger_predict]
qualname: prediction.predict
handlers:

Peter
Aug 25 '08 #2
Calling fileConfig() disables any loggers existing at the time of the
call. Make sure you call fileConfig() before instantiating any
loggers; after that you can instantiate as many as you like, and they
will all be enabled. Make sure you don't call fileConfig() again, as
in that call all loggers which are not named in the configuration will
be disabled.

The reason why fileConfig() disables existing loggers is that it was
designed as a one-off configuration (which completely replaces any
existing configuration with that specified in the config file) - not
as an incremental configurator.

Best regards,

Vinay Sajip
Aug 26 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Kerry Neilson | last post: by
3 posts views Thread by Jeff Shannon | last post: by
2 posts views Thread by Frank Py | last post: by
19 posts views Thread by Oliver Neumann | last post: by
10 posts views Thread by Stephany Young | last post: by
1 post views Thread by belred | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.