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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:19:43+00:00 2026-05-16T00:19:43+00:00

I would like to use the python logging module to log all of the

  • 0

I would like to use the python logging module to log all of the output from unittest so that I can incorporate it into a testing framework I am trying to write. The goal of this is to run the tests with 2 sets of output, one with simple output that tells the test case steps and a more debug level output so that when things go wrong we have as much info as possible. The output would be placed into two files, one that I could email out to people, and another kept in case of failures. I noticed that the TextTestRunner can use a stream, could this be used with the logging module? I’m planning on using some of the new features in python 2.7.

  • 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-16T00:19:44+00:00Added an answer on May 16, 2026 at 12:19 am

    You could, but I’m not sure it’s your best approach.

    For this approach, you would:

    1. Instantiate an in-memory stream that can be used by TextTestRunner. This is the sort of thing io.StringIO would be nearly perfect for, except that it works with Unicode input only, and I’m not sure that TextTestRunner writes Unicode properly to the stream. Your other option would be to code up your own in-memory stream that serves your purpose, perhaps wrapping StringIO with an encoder.

    2. Create your own TextTestRunner and initialize it with this in-memory stream.

    3. Create a class that reads from the stream and writes it to the log.

    This could be as simple as:

    class StreamLogger(object):
        def __init__(self, input_stream, output_logger):
            self.input_stream = input_stream
            self.output_logger
        def run(self):
            while True:
                line = input_stream.readline()
                if not line:
                    break
                output_logger.error(line)
    

    Problems with this approach:

    • You don’t have much if any flexibility in directing different parts of TextTestRunner output to different log levels.
    • TextTestRunner, if misconfigured, will write a bunch of stuff that you probably don’t want. The default verbosity is 1, which will write progress dots while you’re testing… which will probably only get in your way in the logging output.
    • If you do this naïvely, you’ll call stream_logger.run() only after you’ve written all your output to the stream. Thus, you won’t get your logging output in real time, and your logging timestamps will be useless. You could solve this by spawning a separate thread for reading, for example, but then you’d need to choose/roll a stream that can handle a reader and writer thread working simultaneously, or fork a process and ditch the in-memory stream, or something relatively complicated.

    The approach I’d suggest instead is to forego streams and simply roll your own test runner – called, say, LoggingTestRunner – that writes the test output to the logger in precisely the way you want it output. This will allow you to avoid all three of these problems.

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

Sidebar

Ask A Question

Stats

  • Questions 542k
  • Answers 542k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SELECT p.id as a, p.url as b, t.id as… May 17, 2026 at 3:22 am
  • Editorial Team
    Editorial Team added an answer There are two parts to the problem First Issue You… May 17, 2026 at 3:19 am
  • Editorial Team
    Editorial Team added an answer I thought I'd show the regex approach, too. It doesn't… May 17, 2026 at 3:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I would like to use Python to script an application that advertises itself as
I have some Python Scripts which I would like to use from my VB.NET
I would like to use my laptop as a web development (PHP, Python, etc.)
I would like to use a language that I am familiar with - Java,
I would like to use a component that exposes the datasource property, but instead
I would like to use the C implementation of a class method (generated from
I would like to use something like CLR Profiles on .Net 2.0 to see
I would like to use as and is as members of an enumeration. I
I would like to use client-side Javascript to perform a DNS lookup (hostname to
I would like to use javascript to develop general-purpose GUI applications. Initially these are

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.