1

I configure logging in all modules in a project like this:

import logging
import logging.config
logging.config.fileConfig('../logging.ini',defaults={'name':__name__})
logging.getLogger("cassandra").setLevel("WARNING") #move this line into the logging.ini

Now I would like to move the last line into the following config file. What is the best way of doing this? Right now I copy/pasted this line into each module, although I have a shared config-file. :S

Configs I found give only examples for self-created loggers, but not overwriting properties of imported loggers.

logging.ini

[loggers]
keys=root

[handlers]
keys=consoleHandler,fileHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=consoleHandler,fileHandler

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

[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormatter
args=('../logs/%(name)s.log',)


[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=
Rick
  • 1,891
  • 13
  • 26

1 Answers1

1

Try this in your logging.ini:

[loggers]
keys=root,cassandra

[logger_cassandra]
level=WARNING
handlers=consoleHandler,fileHandler
qualname=cassandra

With this config you can write your modules like

import logging.config

logging.config.fileConfig('logging.ini', defaults={'name': __name__})
logging.warning("this is my logging message")

Note: I recommend the usage of logging.dictConfig. It appears to be more straight forward to me and offers a many options for your configuration. You maybe want to checkout this example dict config or even this with colored console logging.

Fabs
  • 161
  • 1
  • 9
  • I tried this and get: NoOptionError: No option 'qualname' in section: 'logger_cassandra' – Rick Apr 03 '18 at 14:07
  • I want to do this, because I want a central place to configure logging for my project. There is roughly 15 modules and it is cumbersome to configure logging for all of them. – Rick Apr 03 '18 at 14:09
  • Can you explain why I need to add qualname and handlers? ideally this is inherited from root. If I configure my logger with python code, I do not need to specify these. – Rick Apr 03 '18 at 14:52