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 6617391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:41:55+00:00 2026-05-25T20:41:55+00:00

I really miss something basic about python’s logging module. In the following code, I

  • 0

I really miss something basic about python’s logging module.

In the following code, I create a logger object (log) and add to it two handlers. One with
‘INFO’ level and one with ‘WARNING’ level. Both of them are supposed to print to stdout. I expect that calling to log.info(msg)
will result in one copy of msg in my stdout and calling to log.warn(msg) sould result
in two copies of msg printed to my stdout. Here is the code:

import logging
import sys


logging.basicConfig()
log = logging.getLogger('myLogger')
log.handlers = []
h1 = logging.StreamHandler(sys.stdout)
h1.level = logging.INFO
h1.formatter = logging.Formatter('H1 H1 %(message)s ')
h2 = logging.StreamHandler(sys.stdout) 
h2.level = logging.WARNING
h2.formatter = logging.Formatter('H2 H2 %(message)s')
log.addHandler(h1)
log.addHandler(h2)

print 'log.level == %s'%logging.getLevelName(log.level)
print 'log.info'
log.info('this is some info')
print 'done'
print 'log.warn'
log.warn('this is a warning')
print 'done'

The output is really very strange to me. The .info call results in no visual effect.
However, calling to warn results in two copies of msg printed to stdout (which is OK), but also one copy printed to stderr (why?). This is the output of the above code. Note the formatting of the last line in this output. This line is printed to stderr.

log.level == NOTSET
log.info
done
log.warn
H1 H1 this is a warning 
H2 H2 this is a warning
done
WARNING:myLogger:this is a warning

So my questions are:

  1. why does my call to info result in no output, despite the fact that h1‘s level is set to INFO?
  2. why does my call to warn results in additional output to stderr?
  • 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-05-25T20:41:56+00:00Added an answer on May 25, 2026 at 8:41 pm

    There are two things you need to know:

    1. The root logger is initialized with a level of WARNING.

      Any log message that reaches a logger is discarded if its level is below the logger’s level. If a logger’s level is not set, it will take its “effective level” from its parent logger. So if the root logger has a level of WARNING, all loggers have a default effective level of WARNING. If you don’t configure it otherwise, all log messages with a level below that will be discarded.

    2. When you call basicConfig(), the system automatically sets up a StreamHandler on the root logger that prints to the standard error stream.

      When your program prints out its log messages, there are actually three handlers: the two that you’ve added, which have their own levels, and one from the system that will print out any message not rejected by its logger. That’s why you get the line

      WARNING:myLogger:this is a warning
      

      It comes from the system logger. It doesn’t do this for the INFO level message, because as previously discussed, the root logger is configured to reject those messages by default.

      If you don’t want this output, don’t call basicConfig().

    Further reading: http://docs.python.org/howto/logging.html

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

Sidebar

Related Questions

One thing I really miss about Java is the tool support. FindBugs, Checkstyle and
Do I miss something or there is really no (ready / built-in) way to
I am using Rails 2.3.2 and I am really miss something important in here.
The one thing I really miss in vim is a tiered intellisense support, just
A feature in Eclipse that I really miss is how you can auto-complete method
I've been using VS2010RC and I really miss the old help system. Is it
The Eclipse IDE has a neat little feature that I really miss in Visual
Accepting the possibility of extreme ridicule, I must admit that I really miss sun
Really stuck on trying to write code to unzip a file or directory on
I often find I want to write code something like this in C#, but

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.