0

I have following log configuration file in python:

[loggers]
keys=root

[logger_root]
handlers=screen,file

[formatters]
keys=simple,complex

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

[formatter_complex]
format=[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s

[handlers]
keys=file,screen

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=complex
level=DEBUG
args=('spartacus.log',)

[handler_screen]
class=StreamHandler
formatter=complex
level=DEBUG
args=(sys.stdout,)

Run following python program:

import logging.config

logging.config.fileConfig("logging.conf")
logging.debug("1")
logging.info("2")
logging.warn("3")
logging.error("4")
logging.critical("5")

The output is:

[2014-04-01 11:25:04,720] WARNING [root.<module>:11] 3
[2014-04-01 11:25:04,720] ERROR [root.<module>:12] 4
[2014-04-01 11:25:04,720] CRITICAL [root.<module>:13] 5

Where are my INFO and DEBUG level log entries?

2 Answers 2

1

Try setting your root logger level to DEBUG:

[logger_root]
level=DEBUG
handlers=screen,file

From the docs:

Note that the root logger is created with level WARNING

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

2 Comments

Can you explain this? Why doesn't the level on the file handler have any effect?
The root logger dictates the base logging level which is inherited by child loggers and may be overridden by handlers; but it only takes effect if that level is higher than the logger it is associated with. So, for example, if the root level was DEBUG, but your screen handler had the level set to WARN, you would only see WARN or higher messages on your console.
0

try this logging.conf file

[loggers]
keys=root

[logger_root]
handlers=screen,file
level=DEBUG

[formatters]
keys=simple,complex

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

[formatter_complex]
format=[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s

[handlers]
keys=file,screen

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=5
formatter=complex
args=('spartacus.log',)

[handler_screen]
class=StreamHandler
formatter=complex
args=(sys.stdout,)

You need to set the level for logger.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.