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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:44:51+00:00 2026-05-26T00:44:51+00:00

I hate to give the question this heading but I actually don’t know whats

  • 0

I hate to give the question this heading but I actually don’t know whats happening so here it goes.

I was doing another project in which I wanted to use logging module. The code is distributed among few files & instead of creating separate logger objects for seperate files, I thought of creating a logs.py with contents

import sys, logging

class Logger:
    def __init__(self):
        formatter = logging.Formatter('%(filename)s:%(lineno)s %(levelname)s:%(message)s')
        stdout_handler = logging.StreamHandler(sys.stdout)
        stdout_handler.setFormatter(formatter)
        self.logger=logging.getLogger('')
        self.logger.addHandler(stdout_handler)
        self.logger.setLevel(logging.DEBUG)

    def debug(self, message):
        self.logger.debug(message)

and use this class like (in different files.)

import logs
b = logs.Logger()
b.debug("Hi from a.py")
  1. I stripped down the whole problem to ask the question here. Now, I have 3 files, a.py, b.py & main.py. All 3 files instantiate the logs.Logger class and prints a debug message.
  2. a.py & b.py imports “logs” and prints their debug message.
  3. main.py imports logs, a & b; and prints it own debug message.

The file contents are like this: https://i.stack.imgur.com/a4ssr.png

Why is debug message from b.py printed 2 times & from main.py 3 times?

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

    Specify a name for the logger, otherwise you always use root logger.

    import sys, logging
    
    class Logger:
        def __init__(self, name):
            formatter = logging.Formatter('%(filename)s:%(lineno)s %(levelname)s:%(message)s')
            stdout_handler = logging.StreamHandler(sys.stdout)
            stdout_handler.setFormatter(formatter)
            self.logger=logging.getLogger(name)
            self.logger.addHandler(stdout_handler)
            self.logger.setLevel(logging.DEBUG)
    
        def debug(self, message):
            self.logger.debug(message)
    

    http://docs.python.org/howto/logging.html#advanced-logging-tutorial :

    A good convention to use when naming loggers is to use a module-level
    logger, in each module which uses logging, named as follows:

    logger = logging.getLogger(__name__)

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

Sidebar

Related Questions

I hate that I'm doing this but I've just started working with Javascript classes
I hate to do this, but I just can't identify what I am doing
I know you are gonna hate me for that kind of question. But could
I hate to be the third person to ask this, but the previous two
I hate to ask such a general question, and this is not a write
I hate to ask such a general question, but the following code is a
I hate starting a post with this but I'm new to Java... I've followed
query = Files .Where(file => file.Fileinfo.Name.ToUpper().Contains(textBox1.Text.ToUpper())) .Take(7).ToList(); I hate asking this question, but I
I hate to ask another how do you do this with regex question on
I hate to submit a new question, but everyone else has some slight thing

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.