Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change level of python fileConfig logger

Tags:

python

logging

I have a logger configured from a file and would like to change the level of my logging without having to change the .conf file, but instead using inline code;

import logging.config

logging.config.fileConfig('..\\LoggingConfig\\loggingfile.conf')

logging.StreamHandler.setLevel(logging.info)

logging.debug("Debug")
logging.info("Info")

This should only print the "Info" log line to the screen. I don't know on which object to call the setLevel()! logging.StreamHandler.setLevel(logging.info) is just a stab in the dark after 30 mins searching...

The loggingfile.conf file;

[loggers]
keys=root

[logger_root]
handlers=screen
level=NOTSET

[formatter_modfunc]
format=%(module)-20s  %(funcName)-25s %(levelno)-3s: %(message)s

[handlers]
keys=screen

[handler_screen]
class=StreamHandler
formatter=modfunc
level=DEBUG
args=(sys.stdout,)
qualname=screen
like image 243
Marcus Jones Avatar asked Oct 13 '25 04:10

Marcus Jones


1 Answers

You need to call setLevel on your Logger instance.

LOGGER = logging.getLogger('your.module.file.name')
LOGGER.setLevel(_level)
LOGGER.info('foo')

If you are only using the basic logger, you can do it like this

logging.basicConfig(level=_level)
logging.info('foo')

See http://docs.python.org/howto/logging.html

like image 170
cfedermann Avatar answered Oct 14 '25 18:10

cfedermann