简体   繁体   中英

trouble setting up python logging

I am using python's standard logging system to log my application. I want to print all types of messages (debug through critical) to the console, but I also want to send an email if the message level is error or higher. I've been reading about the logging documentation, but it was a bit confusing. I set up the following test, but it doesn't seem to work correctly:

 import logging

 log = logging.getLogger('my_test_log')
 sublog = logging.getLogger('my_test_log.sublog')

 log.setLevel(logging.ERROR)
 log.addHandler(logging.StreamHandler())

 sublog.addHandler(logging.StreamHandler())
 sublog.setLevel(logging.DEBUG)     

 sublog.debug('This is a debug message')
 sublog.info('This is an info message')
 sublog.warn('This is a warn message')
 sublog.error('This is an error message')
 sublog.critical('This is a critical message')

NOTE: I set up both logs to StreamHandler right now because I don't want to spam email yet, but it should technically just print the error and critical message twice instead of sending it to email in this situation. I will change this to SMTP after this works to email it off

This is my output when I run this code:

This is a debug message
This is a debug message
This is an info message
This is an info message
This is a warn message
This is a warn message
This is an error message
This is an error message
This is a critical message
This is a critical message

Basically everything gets printed twice rather than just the error and critical messages. What am I doing wrong here? Thanks!

After some quick research, it seems that Handler objects don't automatically use their parent Logger's log level. You'll have to set the level yourself .

import logging

log = logging.getLogger('my_test_log')
sublog = logging.getLogger('my_test_log.sublog')

log.setLevel(logging.ERROR)
handler = logging.StreamHandler()
handler.setLevel(logging.ERROR)
log.addHandler(handler)

...

Your problem is that you have the level set to DEBUG on the sublog. So, you will get all of the messages (just change to ERROR). Also, there is a problem with logger.propagate being True.

This should fix it:

sublog.propagate = False

This will stop the duplicate messages.

Review the documentation about logging here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM