2

I'm loading my logging configuration from a file. The log file is given below:

[loggers]
keys=root

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stderr,)

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('../output.log','w')

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

And I create a logger using:

_logger = logging.getLogger(__name__)
logging.config.fileConfig('../logging.conf')

However, I do not see any logging output when I run my program. If I add a separate logger for my main module, then logging works as expected. But set up like this it doesn't work. What am I doing wrong?

2
  • What's the name of your log file? Commented May 11, 2013 at 0:38
  • @HennyH added statement to show how I'm loading the config file. Commented May 11, 2013 at 12:49

2 Answers 2

1

You need to place the logger creation after configuration. The logging.fileConfig call will disable the pre-existing loggers which is why your logger isn't working.

Sign up to request clarification or add additional context in comments.

1 Comment

Or alternatively set the keyword argument disable_existing_loggers=False.
1

By creating the log file like so:

_logger = logging.getLogger(__name__)

It will look for a file with the name with the value of __main__, which your main module will have equal to "__main__". However the other modules you import will have __name__ equal to the module name of the module.

2 Comments

That's not how I'm creating the log file. That's just how I'm creating the logger. And I've edited my post to show how I'm loading the lagging config file.
Regardless, shouldn't messages me forwarded to the root logger by the main logger? Why aren't they being output?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.