Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8750305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:50:35+00:00 2026-06-13T12:50:35+00:00

Up to now, I’ve been peppering my code with ‘print debug message’ and even

  • 0

Up to now, I’ve been peppering my code with ‘print debug message’ and even ‘if condition: print debug message’. But a number of people have told me that’s not the best way to do it, and I really should learn how to use the logging module. After a quick read, it looks as though it does everything I could possibly want, and then some. It looks like a learning project in its own right, and I want to work on other projects now and simply use the minimum functionality to help me. If it makes any difference, I am on python 2.6 and will be for the forseeable future, due to library and legacy compatibilities.

All I want to do at the moment is pepper my code with messages that I can turn on and off section by section, as I manage to debug specific regions. As a ‘hello_log_world’, I tried this, and it doesn’t do what I expected

import logging

# logging.basicConfig(level=logging.DEBUG)

logging.error('first error')
logging.debug('first debug')

logging.basicConfig(level=logging.DEBUG)

logging.error('second error')
logging.debug('second debug')

You’ll notice I’m using the really basic config, using as many defaults as possible, to keep things simple. But appears that it’s too simple, or that I don’t understand the programming model behind logging.

I had expected that sys.stderr would end up with

ERROR:root:first error
ERROR:root:second error
DEBUG:root:second debug

… but only the two error messages appear. Setting level=DEBUG doesn’t make the second one appear. If I uncomment the basicConfig call at the start of the program, all four get output.

Am I trying to run it at too simple a level?

What’s the simplest thing I can add to what I’ve written there to get my expected behaviour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:50:36+00:00Added an answer on June 13, 2026 at 12:50 pm

    Logging actually follows a particular hierarchy (DEBUG -> INFO -> WARNING -> ERROR -> CRITICAL), and the default level is WARNING. Therefore the reason you see the two ERROR messages is because it is ahead of WARNING on the hierarchy chain.

    As for the odd commenting behavior, the explanation is found in the logging docs (which as you say are a task unto themselves 🙂 ):

    The call to basicConfig() should come before any calls to debug(),
    info() etc. As it’s intended as a one-off simple configuration
    facility, only the first call will actually do anything: subsequent
    calls are effectively no-ops.

    However you can use the setLevel parameter to get what you desire:

    import logging
    
    logging.getLogger().setLevel(logging.ERROR)
    
    logging.error('first error')
    logging.debug('first debug')
    
    logging.getLogger().setLevel(logging.DEBUG)
    
    logging.error('second error')
    logging.debug('second debug')
    

    The lack of an argument to getLogger() means that the root logger is modified. This is essentially one step before @del’s (good) answer, where you start getting into multiple loggers, each with their own specific properties/output levels/etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now i know there have been a lot of question about this, but i
I have a jquery bug and I've been looking for hours now, I can't
Now that viewDidUnload and shouldAutorotateToInterfaceOrientation have been deprecated with iOS 6, what should we
Now I know this basic question has been asked before, but I must be
Now facebook require to have these options when you want to use bulit-in actions
Now I am creating an address book in Java 1.6. Now how I have
Now I have a long query and I'd like to alternate with such a
Now I have a string just including uppercases and lowercases. I want to change
Now, this may be really stupid. But can we deploy a Java Swing Application
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.