Can I have logging.ini file without root logger?

configuration, ini, logging, python

Solution

If you use the source, you'll see that you must configure a root logger:

# configure the root first
llist = cp["loggers"]["keys"]
llist = llist.split(",")
llist = list(map(lambda x: x.strip(), llist))
llist.remove("root")
section = cp["logger_root"]
root = logging.root
log = root

(where `cp` is the `configparser` that loads the `.ini` file you passed in)

The only reason I can think of is that explicit is better than implicit, so it forces you to declare exactly what you want to do with the root logger, in case you thought it would do some magic. Though I don't thinkg that's a particularly good reason. It was probably just the way someone thought to do it at the time. If you do some further reading:

The fileConfig() API is older than the dictConfig() API and does not provide functionality to cover certain aspects of logging [... N]ote that future enhancements to configuration functionality will be added to dictConfig(), so it’s worth considering transitioning to this newer API when it’s convenient to do so.

And if you consider the `dictConfig` docs, it appears that you don't have to provide a `root` logger.

So it appears that you are required to specify a root handler, with no really good reason besides backwards compatibility. If you want to get around that, you'll have to either specify your settings in a Python file or import a JSON file and use the `dictConfig` method.

Problem

Here is how my logging.ini file looks like: ``` [loggers] keys=teja [handlers] keys=fileHandler [formatters] keys=simpleFormatter [logger_teja] level=DEBUG handlers=fileHandler qualname=tejaLogger [handler_fileHandler] class=logging.FileHandler level=DEBUG formatter=simpleFormatter args=("error.log", "w") [formatter_simpleFormatter] format=%(asctime)s - %(name)s - %(levelname)s - %(message)s ``` I am getting the follwing error: ``` File "test.py", line 22, in <module> logging.config.fileConfig('logging.ini') File "/usr/lib/python2.7/logging/config.py", line 79, in fileConfig _install_loggers(cp, handlers, disable_existing_loggers) File "/usr/lib/python2.7/logging/config.py", line 183, in _install_loggers llist.remove("root") ValueError: list.remove(x): x not in list ``` Please help me to figure the problem. Or Please explain me "Why there is always a need to include root logger at all?"

Original source