Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Sasan Shadpour
Sasan Shadpour
2,601 Points

Logging.info Vs. logging.warn?!

In the video, we have seen both 'Logging.info' and 'logging.warn' have been briefly touched. We can see the warning but we do not see the info message. Then, why and where do we need 'Logging.info'? what I mean is first we had both of them without 'logging.basicConfig'. and I am referring to video for logging by Kenneth Love.

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Sorry, I did not answer your question before. Got a little distracted with a work matter.

You really can't omit the logging.basicConfig if you want full-featured output.

As it stands in Python 3.6+ if you use logging without basicConfig, all you are going to get is screen output for WARNING, ERROR, and CRITICAL.

For example:

# log_example.py
import logging

logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
logging.error('This is an error message')
logging.critical('This is a critical message')

Is only going to output--

$ python log_example.py
WARNING:root:This is a warning message
ERROR:root:This is an error message
CRITICAL:root:This is a critical message

So where did my output go? Since logging.debug() and logging.info() are missing from the output, I think you would agree that this is not very informative!

Sasan Shadpour
Sasan Shadpour
2,601 Points

Thanks a lot for breaking down this for me and well-explained! I can understand it now better.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

Logging info is for "normal" (timestamped) status messages and warning messages are for abnormal operations or potential threats.

I can't speak to Kenneth's specific example but these logging messages can be sent to separate streams or suppressed altogether depending on the company's needs. By keeping the messages separate, we can assign handling and piping separately (by auxiliary monitor programs on the server).

For example, I may not be especially interested in normal messages but I want these available, and when I am "on-call" on the weekends, I will want to have the warning messages sent to me via email notification or even SMS texting. Hopefully, the server won't call with warnings when I am enjoying downtime from work, but if they do, I can go into action!

I hope this helps. Good luck with your Python journey!

Sasan Shadpour
Sasan Shadpour
2,601 Points

Thanks for the explanation, Jeff. Let me rephrase my question: if we do not have 'logging.basicConfig' in which we can create the log file, then logging.info would not give us anything, would it?