0

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?

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

"by default" should be emphasized or emphasized
I've reworded it hopefully to make it clearer. I could emphasize it too if it's not enough. Thanks!
The answer was clear enough already. However, I think it is important to highlight the that "WARN and higher" is only by default. However, your answer clarifies the confusion that @Anton I believe had: logging works from a set level up and not down.
Oh! So each call I make sends the requested message, and then these messages are filtered by the level parameter?
I agree about defaults. Thanks again!
|
0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.