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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:36:57+00:00 2026-05-30T17:36:57+00:00

I have a python test in which I want to test if the logging

  • 0

I have a python test in which I want to test if the logging works properly. For example I have a function that creates a user and at the end the logging writes to log file the response.

logger = logging.getLogger('mylogger')
logger.setLevel(logging.DEBUG)
handler = logging.handlers.WatchedFileHandler('mylogfile.log')
formatter = logging.Formatter('%(asctime)s: %(message)s',
                              '%d/%b/%Y:%H:%M:%S %z')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('Some log text')

In my test case I want to send the log output to the StringIO.

class MyTest(unittest.TestCase):
    def setUp(self):
        stream = StringIO()
        self.handler = logging.StreamHandler(stream)
        log = logging.getLogger('mylogger')
        log.removeHandler(log.handlers[0])
        log.addHandler(self.handler)

    def tearDown(self):
        log = logging.getLogger('mylogger')
        log.removeHandler(self.handler)
        self.handler.close()

The problem is that I’m not sure how should I test wheter or not my logger is working.

  • 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-30T17:36:59+00:00Added an answer on May 30, 2026 at 5:36 pm

    Here is an example that works, make sure you set the level of your log, and flush the buffer.

    class MyTest(unittest.TestCase):
        def setUp(self):
            self.stream = StringIO()
            self.handler = logging.StreamHandler(self.stream)
            self.log = logging.getLogger('mylogger')
            self.log.setLevel(logging.INFO)
            for handler in self.log.handlers: 
                self.log.removeHandler(handler)
            self.log.addHandler(self.handler)
        def testLog(self):
            self.log.info("test")
            self.handler.flush()
            print '[', self.stream.getvalue(), ']'
            self.assertEqual(self.stream.getvalue(), 'test')
    
        def tearDown(self):
            self.log.removeHandler(self.handler)
            self.handler.close()
    
    if __name__=='__main__':
        unittest.main()
    

    Further reading, here is an example Temporily Capturing Python Logging to a string buffer that show how you could also do formatting.

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

Sidebar

Related Questions

I have a fairly complex system that I want to test using python. My
I have the following simple python test script that uses Suds to call a
I have a function (neural network model) which produces figures. I wish to test
I have an application that reads test scripts in python and sends them across
I have a test which randomly fails and I want to let it retry
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
I am setting up a simple test page in Python. I only have two
I have Python 2.6 and I want to install easy _ install module. The
I'm trying to install virtual-python on linux server which I don't have root access.
I have a currently running PHP application that I want to add real-time feed

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.