Why do all my loggers start auto-disabled?

J

jonfroehlich

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
 
P

Peter Otten

jonfroehlich said:
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
 
V

Vinay Sajip

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top