Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Logging always print message on console without reason

I have a code that work but do things that I don’t understand.
This code use logging python module. The problem is that when I run this script there is always (in additional of the normal Stream Log) a line on the console that I don’t want (the second line in STDOUT at the bottom of this post).

I don’t understand why this line exist and how solve this problem.

I think that problem is due to the usage of logging.basicConfig(level=logging.INFO) but I need to use it because if I don’t use it logger.info('This is a INFO-01') don’t work just the level WARNING, etc … I tried several things but that just produced errors and don’t find answer on Internet.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import logging

logging.basicConfig(level=logging.INFO)

logger = logging.getLogger(__name__)

c_handler = logging.StreamHandler()
f_handler = logging.FileHandler('file07.log')
c_handler.setLevel(logging.INFO)
f_handler.setLevel(logging.INFO)

c_format = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
f_format = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
f_handler.setFormatter(f_format)

logger.addHandler(c_handler)
logger.addHandler(f_handler)


logger.info('This is a INFO-01')

STDOUT:

2021-12-11 17:54:30,203 - INFO - This is a INFO-01
INFO:__main__:This is a INFO-01

If someone can help me, I will be really grateful.
Cordially

>Solution :

You guessed it right. Take a look at this image from the document.

By default the propagation is True. So your logger object has a parent logger named "root".

basicConfig essentially does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger.

The ‘root’ is the parent of all loggers. So after your logger logger logs the message, it will hand it to the parent logger and because it has a handler now, it logs that message for the second time with the default format.

You can remove that because you don’t need it. Don’t forget to specify LEVEL for your logger: (I removed the f_handler handler in the below code)

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)  # <----- here

c_handler = logging.StreamHandler()
c_handler.setLevel(logging.INFO)

c_format = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)

logger.addHandler(c_handler)

logger.info('This is a INFO-01')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading