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 Write Better Python Buggy Logs Log Messages

Dan Badertscher
Dan Badertscher
5,327 Points

Logging at the WARNING level?

'Log "The French have the Grail" as a WARNING level message.'

import logging

logging.basicConfig(filename='cc.log', level=logging.DEBUG)

# Write your code below here

logging.debug("ello")

logging.debug("The French Have the Grail", level = logging.WARNING)

Where can I improve my code?

3 Answers

Travis Bailey
Travis Bailey
13,675 Points
logging.debug("ello")

The above logs an error as debug.

logging.error("this is an error")

The one above this logs the string "this is an error" as an error. Catch the difference? :)

Dan Badertscher
Dan Badertscher
5,327 Points

Thank you, after I did that (and corrected a capitalization error in the string) it passed haha

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Logging has built-in functions for the various levels:

# Set up logging
logging.basicConfig(filename='cc.log', level=logging.DEBUG)
# output messages
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 error message')

The "level" says what messages are exposed. Since logging.WARNING > logging.DEBUG (literally!) when level is set to DEBUG all warning messages produced by logging.warning() will be visible. If the level is changed to WARNING, then logging.debug() will not show up and neither would logging.info() mesages.

logging.WARNING, logging.DEBUG, etc. are have integer values so logging.WARNING (30) is literally greater than logging.INFO (20) and logging.DEBUG (10). You can define your own levels

See more in the docs

Sayan De
Sayan De
12,397 Points
import logging

logging.basicConfig(filename='cc.log', level=logging.DEBUG)
# Write your code below here
logging.debug("Hi")
logging.warning("The French have the Grail")

can you tell me what's the problem here?

[edit formatting –cf]

Travis Bailey
Travis Bailey
13,675 Points

Hi Sayan,

There's no need for the parenthesis on the 2nd line of your code. Also make sure to place each log call on it's own line.

PS You'll get faster feedback and better visibility on your post if you make your own post instead of replying to one that's pretty old like this one.

Also click the 'Markdown Cheatsheet' below your text window when typing your forum post. It will tell you how to make your code show up in a code window. Makes it much easier to diagnose issues with your code.