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

  • SEARCH
  • Home
  • 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 9138765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:58+00:00 2026-06-17T09:17:58+00:00

I am trying to add a module to a project that uses the standard

  • 0

I am trying to add a module to a project that uses the standard Python (2.7) logging libraries. What I want to achieve is the following:

  • Anything equal or worse than WARNING goes to wherever the main program (outside of my control) has configured the logging to go (usually stderr)
  • Everything else (debug messages mainly) goes to a log file

Please note that the key issue here is that I do not want to modify the global logging settings, since those are controlled by the main app. Otherwise I would use the example in here:

http://docs.python.org/2/howto/logging-cookbook.html#logging-to-multiple-destinations

I’ve tried this:

logger = logging.getLogger('my_module')
logger.setLevel(logging.WARNING)
fh = logging.FileHandler('my_module.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

but then I only get warning messages in my file.

If I replace the above line with:

logger.setLevel(logging.DEBUG)

then I get debug messages in both stderr and my file.

Then I tried this:

logger = logging.getLogger('my_module')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.NullHandler())
console = logging.StreamHandler()
console.setLevel(logging.WARNING)
logger.addHandler(console)
fh = logging.FileHandler('my_module.log')
fh.setLevel(logging.DEBUG)
logger.addHandler(fh)

But again no luck.

So can I achieve this without having to call basicConfig() or anything else that affects the global logging infrastructure? I only want to modify my own module here if possible.

Thanks!

  • 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-17T09:17:59+00:00Added an answer on June 17, 2026 at 9:17 am
    • Anything equal or worse than WARNING goes to wherever the main program (outside of my control) has configured the logging to go
      (usually stderr)
    • Everything else (debug messages mainly) go to a log file

    You can’t do it with a single logger without setting WARNING level on ancestor’s handlers. From the docs:

    Logger.propagate
    

    If this evaluates to true, logging messages are passed by this logger and by its child loggers to the handlers of higher level
    (ancestor) loggers. Messages are passed directly to the ancestor
    loggers’ handlers – neither the level nor filters of the ancestor
    loggers in question are considered.


    what do you suggest a module can do in this situation?

    In general other than the setting of WARNING level for the logger and adding NullHandler(), library code shouldn’t configure logging at all.

    If you want an easy way for the main application to provide you with a debug info then define a function that configures DEBUG level logging for your library.


    I don’t want the library to be DEBUG except for the file, where I would like to see everything.

    If you can’t control the main program then you shouldn’t create debug files as a side-effect of using your_module unless explicitly asked to do so:

    import your_module # in the main application
    
    your_module.get_logger().log_to_file(filename) # without this line your module
                                                   # shouldn't create debug files
    ...
    your_module.some_function()
    

    I am not sure how this logger hierarchy works: the logger I created is a child of the root logger that the app configured?

    '' is a root logger. 'a.b' is a child of 'a' logger.

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

Sidebar

Related Questions

I am trying to create a shared library (really a Python module) that links
I am working on a project that has an EJB-module. There has been a
I am trying to setup the assembly plugin for a large multi-module project. The
I am trying to implement the TinyMCE Spellcheck plugin that uses GoogleSpell. The thing
I have a currently running PHP application that I want to add real-time feed
I am trying to add a WSDL module to my existing application, but I'm
Being a beginner of using Django, i am trying to add some module for
I've just started using maven for trying to build a multi module project. Everything
I have a trying to send SMS messages with ruby-smpp. Following the project examples
I'm trying add data triggers to the default combobox style so each text item

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.