3

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!

2 Answers 2

2

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)

...
Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok, that would make sense why it wouldn't work then. I just tried this and it works! Thank you!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.