Anton
Anton

Reputation: 4815

Why is Python's Logger Returning Errors When There Isn't An Error?

I think I am not understanding how Python's logging library works. I have a simple function:

 def all_caps(_string):

    logging.debug("debug") 
    logging.info("info") 
    logging.warning("warning") 
    logging.error("error")
    logging.critical("critical")

    return _string.upper()

From what I understand from tutorials, if I run this function with an input that produces no errors (e.g. all_caps("hello")), I should see the output:

DEBUG:root:debug
INFO:root:info

However, I see the exact opposite:

WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical
HELLO

Am I crazy? Am I using logging fundamentally wrong?

Upvotes: 0

Views: 286

Answers (2)

user554538
user554538

Reputation:

Each logging function you call from the module will have the root logger send the requested message at the corresponding level, so with the default configuration, you should see messages for at least everything WARN and higher.

Upvotes: 2

user9074332
user9074332

Reputation: 2636

Your default logging level appears to be set to WARNING. Reset it using logging.basicConfig to get the desired results.

>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> def all_caps(_string):
...     logging.debug("debug")
...     logging.info("info")
...     logging.warning("warning")
...     logging.error("error")
...     logging.critical("critical")
...
...     return _string.upper()
...
>>> all_caps("hello")
DEBUG:root:debug
INFO:root:info
WARNING:root:warning
ERROR:root:error
CRITICAL:root:critical
'HELLO'

Upvotes: 0

Related Questions