DarkTrick
DarkTrick

Reputation: 3447

log info doesn't show up on console but warn and error do

I'm trying to use the logging facility of python, but it does not work as expected.

Below is my code. It prints warning and critical messages properly, but it does not print info and debug. getEffectiveLevel() shows the correct level.

import logging

logger = logging.getLogger("base")

# set level to debug
logger1.setLevel(logging.DEBUG) 

# check, if level is really DEBUG
print("effective level: " + str(logger1.getEffectiveLevel()))

logger1.debug   ("logger1: debug    msg")
logger1.info    ("logger1: info     msg")  
logger1.warning ("logger1: warning  msg")  
logger1.critical("logger1: critical msg")

Upvotes: 1

Views: 221

Answers (1)

DarkTrick
DarkTrick

Reputation: 3447

Explanation

The missing information is a little bit hidden in the docs. You need to call

logging.basicConfig()

To initialize the handlers.

Complete Code

The full code would look like this:

import logging

logger = logging.getLogger("base")

# setup handlers
logging.basicConfig()

# set level to debug
logger1.setLevel(logging.DEBUG) 

# check, if level is really DEBUG
print("efffective level: " + str(logger1.getEffectiveLevel()))

logger1.debug   ("logger1: debug    msg")
logger1.info    ("logger1: info     msg")  
logger1.warning ("logger1: warning  msg")  
logger1.critical("logger1: critical msg")

Also see how the printed messages changed.

Reference:

Reference: https://docs.python.org/3/library/logging.html , search for logging.log(level, msg, *args, **kwargs) and read the Note

Upvotes: 2

Related Questions