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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:01:50+00:00 2026-05-24T06:01:50+00:00

I am using the logger module of python (python version 3.x, but it should

  • 0

I am using the logger module of python (python version 3.x, but it should not matter) and I noticed that an error in a format string is reported as such:

Traceback (most recent call last):
  File "/usr/lib/python3.1/logging/__init__.py", line 770, in emit
    msg = self.format(record)
  File "/usr/lib/python3.1/logging/__init__.py", line 650, in format
    return fmt.format(record)
  File "/usr/lib/python3.1/logging/__init__.py", line 438, in format
    record.message = record.getMessage()
  File "/usr/lib/python3.1/logging/__init__.py", line 308, in getMessage
    msg = msg % self.args
TypeError: %d format: a number is required, not str

As you can see, there is no mention of where the actual error (in my code) was. By the way, here’s what was wrong in my code:

logging.debug('This is a string %d', str(foo))

Changing the %d in %s solved the problem.

My question is: how do I get some slightly more useful information from the logging module output? Do I have to write my own logger? Where do I tweak the logger module?

  • 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-24T06:01:51+00:00Added an answer on May 24, 2026 at 6:01 am

    Was just about to post this when Unutbu beat me to it. But here it is anyway:

    You could try subclassing whichever handler you’re using (example below using StreamHandler) and overiding the format method with a function that wraps it in a try: block

    import traceback, logging
    
    class MyStreamHandler(logging.StreamHandler):
        def format(self, record):
            try:
                return logging.StreamHandler.format(self, record)
            except TypeError:
                # Print a stack trace that includes the original log call
                traceback.print_stack() 
    
    
    if __name__ == "__main__":
        log = logging.getLogger("testlogger")
        handler = MyStreamHandler()
    
        log.addHandler(handler)
    
        log.error("Try interpolating an int correctly: %i", 1)
    
        log.error("Now try passing a string to an int: %d", 'abc')
    
        log.error("And then a string to a string %s", 'abc')
    

    gives me:

    Try interpolating an int correctly: 1
      File "logtest2.py", line 19, in ?
        log.error("Now try passing a string to an int: %d", 'abc')
      File "/usr/lib64/python2.4/logging/__init__.py", line 999, in error
        apply(self._log, (ERROR, msg, args), kwargs)
      File "/usr/lib64/python2.4/logging/__init__.py", line 1079, in _log
        self.handle(record)
      File "/usr/lib64/python2.4/logging/__init__.py", line 1089, in handle
        self.callHandlers(record)
      File "/usr/lib64/python2.4/logging/__init__.py", line 1126, in callHandlers
        hdlr.handle(record)
      File "/usr/lib64/python2.4/logging/__init__.py", line 642, in handle
        self.emit(record)
      File "/usr/lib64/python2.4/logging/__init__.py", line 731, in emit
        msg = self.format(record)
      File "logtest2.py", line 8, in format
        traceback.print_stack()
    None
    And then a string to a string abc
    

    I wouldn’t leave this in any production code, but it should help you find things like

    log.error("%d", 'a string')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python logger set up, using python's logging module. I want to
I am using the logging module in Python to write debug and error messages.
I am using python's logging module in a django project. I am performing the
I have a custom HTTP Module. I would like to inject the logger using
I am using logging module of python. How can I access the handlers defined
I create a couple of worker processes using Python's Multiprocessing module 2.6. In each
In Python 2.5 I stored data using this code: def GLWriter(file_name, string): import cPickle
I've written the following in python using the mechanize module in order to print
Due to the flooding examples of implementing logger using Singleton pattern, I have just
When using log4j, the Logger.log(Priority p, Object message) method is available and can be

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.