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?"
Use the following to get the root logger: Logger system = Logger. getLogger(""); You can now access the root logger as any other logger.
Python provides a logging system as a part of its standard library, so you can quickly add logging to your application.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With