Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have logging.ini file without root logger?

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?"

like image 740
Tejas Avatar asked Feb 26 '15 08:02

Tejas


People also ask

How do I get root logger?

Use the following to get the root logger: Logger system = Logger. getLogger(""); You can now access the root logger as any other logger.

Is logging a default Python library?

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.

Should I use logging Python?

In Python, the logging module is used to log such events and errors. An event can be described by a message and can optionally contain data specific to the event. Events also have a level or severity assigned by the developer. Logging is very useful for debugging and for tracking any required information.


2 Answers

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.

like image 168
Wayne Werner Avatar answered Sep 30 '22 02:09

Wayne Werner


Just in case it happens to someone else, check that you have commas separating all the logger entries, as you may be missing one, having both fields' names merged.

like image 37
txomon Avatar answered Sep 30 '22 04:09

txomon