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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:09:55+00:00 2026-05-27T09:09:55+00:00

In a class with few no static functions, I typically do logging like this:

  • 0

In a class with few no static functions, I typically do logging like this:

class ClassA:

    def __init__(self):
        self._logger = logging.getLogger(self.__class__.__name__)

    def do_something(self):
        self._logger.info("Doing something")

    def do_something_else(self):
        self._logger.info("Doing something else.")

In a class with static methods I’ve been doing this:

class ClassB:

    _logger = logging.getLogger("ClassB")

    @staticmethod
    def do_something():
        ClassB._logger.info("Doing something")

    @staticmethod
    def do_something_else():
        ClassB._logger.info("Doing something else")

You could do this but it seems lame:

class ClassB:

    @staticmethod
    def do_something():
        logger = logging.getLogger("ClassB")
        logger.info("Doing something")

    @staticmethod
    def do_something_else():
        logger = logging.getLogger("ClassB")
        logger.info("Doing something else")

Is there a better pattern for logging from static methods?

  • 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-27T09:09:55+00:00Added an answer on May 27, 2026 at 9:09 am

    You could turn them into class methods instead.

    class ClassB(object):
        _logger = logging.getLogger("ClassB")
    
        @classmethod
        def do_something(cls):
             cls._logger.info("Doing something")
    

    But note that when you derive from ClassB and call do_something, it will get a different logger since cls denotes the derived class rather than ClassB.

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

Sidebar

Related Questions

I want to call a few static methods of a CPP class defined in
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
There are a few ways to get class-like behavior in javascript, the most common
Say I had a library called libfoo which contained a class, a few static
I understand that a class that consists of nothing but public static member functions
I have an IAddress class with a few properties. I then have a concrete
I have a class that offers up a few events. That class is declared
Here are a few properties of my class. There is also a public object
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have a few labels on my page with a class of 'error', the

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.