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

The Archive Base Latest Questions

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

I am currently writing an API and an application which uses the API. I

  • 0

I am currently writing an API and an application which uses the API. I have gotten suggestions from people stating that I should perform logging using handlers in the application and use a "logger" object for logging from the API.

In light of the advice I received above, is the following implementation correct?

class test:
    def __init__(self, verbose):
        self.logger = logging.getLogger("test")
        self.logger.setLevel(verbose)
    
    def do_something(self):
        # do something
        self.logger.log("something")
        # by doing this i get the error message "No handlers could be found for logger "test"

The implementation i had in mind was as follows:

 #!/usr/bin/python

 """ 
 ....
 ....
 create a logger with a handler 
 ....
 ....

 """

 myobject = test()
 try:
     myobject.do_something()
 except SomeError:
     logger.log("cant do something")

I’d like to get my basics strong, I’d be grateful for any help and suggestions for code you might recommend I look up.

Thanks!

  • 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-14T05:13:44+00:00Added an answer on May 14, 2026 at 5:13 am

    It’s not very clear whether your question is about the specifics of how to use logging or about logging exceptions, but if the latter, I would agree with Adam Crossland that log-and-swallow is a pattern to be avoided.

    In terms of the mechanics of logging, I would make the following observations:

    1. You don’t need to have a logger as an instance member. It’s more natural to declare loggers at module level using logger = logging.getLogger(__name__), and this will also work as expected in sub-packages.
    2. Your call logger.log(“message”) would likely fail anyway because the log method has a level as the first argument, rather than a message.

    You should declare handlers, and if your usage scenario is fairly simple you can do this in your main method or if __name__ == '__main__': clause by adding for example

    logging.basicConfig(filename='/tmp/myapp.log', level=logging.DEBUG, 
                        format='%(asctime)s %(levelname)s %(name)s %(message)s')
    

    and then elsewhere in your code, just do for example

    import logging
    logger = logging.getLogger(__name__)
    

    once at the top of each module where you want to use logging, and then

    logger.debug('message with %s', 'arguments') # or .info, .warning, .error etc.
    

    in your code wherever needed.

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

Sidebar

Related Questions

I'm writing an API which is used to receive some data from another application.
I'm currently writing a MEX file in MATLAB that uses the CPLEX API in
I'm currently writing some intranet web application where people could submit to admins requests
I am writing a virtual desktop application which utilises the DeferWindowPos API functions. The
I'm writing a web application using the google maps api v3, which displays places
I am writing a WPF application that has an optional dependency on an API
I'm currently writing an application to PGP encrypt files using the Bouncy Castle API
I'm currently writing a little drawing application that needs to access pixel data for
Background: I'm writing a Java server application that needs to perform basic interactions with
I am currently writing the specification for an API that works as follows: Client

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.