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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:08:14+00:00 2026-05-13T10:08:14+00:00

I’ve read through the logging module documentation and whilst I may have missed something

  • 0

I’ve read through the logging module documentation and whilst I may have missed something obvious, the code I’ve got doesn’t appear to be working as intended. I’m using Python 2.6.4.

My program consists of several different python files, from which I want to send logging messages to a text file and, potentially, the screen. I imagine this is a common thing to do so I’m messing this up somewhere.

What my code is doing at the minute is logging to the text file correctly, kinda. But logging to the screen is being duplicated, one with the specified formatting, and one without. Also, when I turn off the screen output, I’m still getting the text printed once, which I don’t want – I just want it to be logged to the file.

Anyway, some code:

#logger.py
import logging
from logging.handlers import RotatingFileHandler
import os

def setup_logging(logdir=None, scrnlog=True, txtlog=True, loglevel=logging.DEBUG):        
    logdir = os.path.abspath(logdir)

    if not os.path.exists(logdir):
        os.mkdir(logdir)

    log = logging.getLogger('stumbler')
    log.setLevel(loglevel)

    log_formatter = logging.Formatter("%(asctime)s - %(levelname)s :: %(message)s")

    if txtlog:
        txt_handler = RotatingFileHandler(os.path.join(logdir, "Stumbler.log"), backupCount=5)
        txt_handler.doRollover()
        txt_handler.setFormatter(log_formatter)
        log.addHandler(txt_handler)
        log.info("Logger initialised.")

    if scrnlog:
        console_handler = logging.StreamHandler()
        console_handler.setFormatter(log_formatter)
        log.addHandler(console_handler)

Nothing unusual there.

#core.py
import logging
corelog = logging.getLogger('stumbler.core')  # From what I understand of the docs, this should work :/

class Stumbler:
    [...]

    corelog.debug("Messages and rainbows...")

The screen output shows how this is being duplicated:

2010-01-08 22:57:07,587 - DEBUG :: SCANZIP: Checking zip contents, file: testscandir/testdir1/music.mp3
DEBUG:stumbler.core:SCANZIP: Checking zip contents, file: testscandir/testdir1/music.mp3
2010-01-08 22:57:07,587 - DEBUG :: SCANZIP: Checking zip contents, file: testscandir/testdir2/subdir/executable.exe
DEBUG:stumbler.core:SCANZIP: Checking zip contents, file: testscandir/testdir2/subdir/executable.exe

Although the textfile is getting the correctly formatted output, turning the screen logging off in logger.py still has the incorrectly formatted output displayed.

From what I understand of the docs, calling corelog.debug(), seeing as corelog is a child of the “stumbler” logger, it should use that formatting and output the logs as such.

Apologies for the essay over such a trivial issue.

TL;DR: How do I do logging from multiple files?

  • 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-13T10:08:14+00:00Added an answer on May 13, 2026 at 10:08 am

    Are you sure no other logging setup is being done in anything you import.

    The incorrect output in your console logs look like the default configuration for a logger, so something else may be setting that up.

    Running this quick test script:

    import logging
    from logging.handlers import RotatingFileHandler
    import os
    
    def setup_logging(logdir=None, scrnlog=True, txtlog=True, loglevel=logging.DEBUG):
        logdir = os.path.abspath(logdir)
    
        if not os.path.exists(logdir):
            os.mkdir(logdir)
    
        log = logging.getLogger('stumbler')
        log.setLevel(loglevel)
    
        log_formatter = logging.Formatter("%(asctime)s - %(levelname)s :: %(message)s")
    
        if txtlog:
            txt_handler = RotatingFileHandler(os.path.join(logdir, "Stumbler.log"), backupCount=5)
            txt_handler.doRollover()
            txt_handler.setFormatter(log_formatter)
            log.addHandler(txt_handler)
            log.info("Logger initialised.")
    
        if scrnlog:
            console_handler = logging.StreamHandler()
            console_handler.setFormatter(log_formatter)
            log.addHandler(console_handler)
    
    
    
    setup_logging('/tmp/logs')
    corelog = logging.getLogger('stumbler.core')
    corelog.debug("Messages and rainbows...")
    

    yields this result:

    2010-01-08 15:39:25,335 – DEBUG ::
    Messages and rainbows…

    and in my /tmp/logs/Stumbler.log

    2010-01-08 15:39:25,335 – INFO ::
    Logger initialised. 2010-01-08
    15:39:25,335 – DEBUG :: Messages and
    rainbows…

    This worked as expected when I ran it in python 2.4, 2.5, and 2.6.4

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

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • 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 You'll need to pass a MethodInfo. May 13, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer From the Java interface design FAQ by Philip Shaw: Interface… May 13, 2026 at 11:02 pm
  • Editorial Team
    Editorial Team added an answer In order to set post values on the page with… May 13, 2026 at 11:02 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

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

Top Members

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.