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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:39:11+00:00 2026-05-16T08:39:11+00:00

When coding python, I use the logging module a lot. After some bad experiences

  • 0

When coding python, I use the logging module a lot.

After some bad experiences and reading articles like this one, I try to prevent import-time executed code wherever possible.

However, for the sake of simplicity, I tend to get my logging object right at the beginning of the module file:

# -*- coding: utf-8 -*-
import logging
logger = logging.getLogger('product.plugin.foo.bar')

This way, my logger is globally accessible and I can just write “logger.error()” anywhere. The alternative is to create it class-wide:

class Bar(object):
    logger = logging.getLogger('product.plugin.foo.bar')

However, now I have to type the Class name everytime. To prevent typing the class name, I am tempted to use “self” instead, which will fail in static methods.

    def my_method(self):
        Bar.logger.error('foo')

    def my_method_2(self):
        self.logger.error('foo') # ok...

    @staticmethod
    def my_method_2():
        self.logger.error('foo') # boom!

So, at first, it looks like creating the logger object module-wide seems like the right thing to do – still it feels like I could end up in import-related trouble when doing it like this…

  • 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-16T08:39:11+00:00Added an answer on May 16, 2026 at 8:39 am

    It’s fine. I even use the same variable name logger. Any logging is better than no logging, but I find it’s nice practise to only expose the logger variable, keep the module hidden away so your code only references the logger, and hence the namespace you’ve designated for the module.

    If you later need to refine the namespaces for code within the module, you can use self.logger within those classes, or shadow the global logger where necessary.

    Update0

    __all__ = [anything but logger]
    import logging
    logger = logging.getLogger("why.is.this.method.still.java.camel.case")
    del logging
    

    Taking note of S.Lott’s contribution below. Also note that generally you don’t want from x import * anyway.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The provider is definitely there as part of the download.… May 16, 2026 at 11:25 pm
  • Editorial Team
    Editorial Team added an answer Try opening a UTF8 connection: con<-file('filename',encoding="UTF-8") write.csv(...,file=con,...) May 16, 2026 at 11:25 pm
  • Editorial Team
    Editorial Team added an answer I finally found what I was looking for. Step 1)… May 16, 2026 at 11:25 pm

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 use Emacs primarily for coding Python but sometimes I use IDLE. Is there
I'm coding by python about Gtalk I use XMPPPY. But I can chat with
I'm coding in Python and I really like Notepad++. However, off late when I
I want to be able to use Python to open a .csv file like
I use TextMate to debug python script, as I like the feature of using
I'm trying to use python + ffmpeg + oggenc to convert any audiofile to
While coding python I'm using only 2 spaces to indent, sure PEP-8 really recommend
Let me preface this question by saying I use TextMate on Mac OSX for
I am coding a python (2.6) interface to a web service. I need to
I'm trying to use a JPEG image in a QImage object from a Python

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.