Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python logger not printing Info

Tags:

python

logging

Looking at the python docs, if I set my logger level to INFO, it should print out all logs at level INFO and above.

However, the code snipper below only prints "error"

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
logger.info("Info")
logger.error("error")
logger.info("info")

Output

error

What could be the reason for this?

like image 492
user3828311 Avatar asked Dec 03 '22 18:12

user3828311


1 Answers

Use logging.basicConfig to set a default level and a default handler:

import logging

logger = logging.getLogger()
logging.basicConfig(level=logging.INFO)

logger.info("Info")
logger.error("error")
logger.info("info")

prints:

INFO:root:Info
ERROR:root:error
INFO:root:info

The logging module is powerful yet confusing. Look into the HOWTO in the docs for a tutorial. I've made my own helper function that logs to stderr and a file that I've detailed on my blog. You might like to adapt it to your needs.

like image 149
Ben Avatar answered Dec 28 '22 11:12

Ben