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)
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With