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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:04:56+00:00 2026-05-24T23:04:56+00:00

In many cases unit-tests are significantly slowed down by the use of python’s logging

  • 0

In many cases unit-tests are significantly slowed down by the use of python’s logging package. Assuming logging isn’t essential to the test, how would you cleanly override logging per-test, so that log commands would be effectively skipped.

Assume the use of multiple loggers such as in:

logger1 = logging.getLogger('logger1')
logger2 = logging.getLogger('logger2')
  • 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-24T23:04:56+00:00Added an answer on May 24, 2026 at 11:04 pm

    Option 1:

    Logging can be disabled by calling

    logging.disable(logging.CRITICAL)
    

    and turned back on with

    logging.disable(logging.NOTSET)
    

    However, even after disabling logging, a logging statement such as logger.info would still cause Python to do a few attribute lookups and function calls before reaching the isEnabledFor method. Still, this might be good enough.

    Option 2:

    Use mocking:

    class MockLogger(object):
        def debug(msg, *args, **kwargs): pass
        def info(msg, *args, **kwargs): pass
        def warn(msg, *args, **kwargs): pass
        def error(msg, *args, **kwargs): pass
        def critical(msg, *args, **kwargs): pass
    
    class Test(unittest.TestCase):
        def test_func(self):
            _logger1=testmodule.logger1
            _logger2=testmodule.logger2
            testmodule.logger1=MockLogger()
            testmodule.logger2=MockLogger()
            # perform test
            testmodule.logger1=_logger1
            testmodule.logger2=_logger2
    

    This will reduce the time consumed by logging statements to the time it takes to do one attribute lookup and one (noop) function call. If that’s not satisfactory, I think the only option left is removing the logging statements themselves.

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

Sidebar

Related Questions

I know there are many cases which are good cases to use multi-thread in
In Python scripts, there are many cases where a keyboard interrupt (Ctrl-C) fails to
I'd like to use properties for my instance variables, but in many cases, I
I have a class that I want to use in many test cases: require
First of all, there are many cases where Sleep() is misused , for example
I'm using c#. In many cases I'm writing code that can benefit from a
In many prototype scripting cases, it's easier to just stick every form item (such
I'm writing some unit tests for one of my rails models. It's a large
When I run my Unit Tests, all tests pass, but instead of Test run
I have read many posts about setting up unit testing in Zend Framework and

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.