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

The Archive Base Latest Questions

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

When I first learned Python, I got used to doing this: print text, lineNumber,

  • 0

When I first learned Python, I got used to doing this:

  print "text", lineNumber, "some dictionary", my_dict

When I wrote my own logging facility, I naturally wanted to be able to hand it an arbitrarily-sized list of items, so I did this:

def error(*args):
   print ERR_PREFIX,
   for _x in args:
      print _x,
   print "\r\n",

error("text", lineNumber, "some dictionary", my_dict)

Now I want to start using the logging package because it has a lot more goodies and I don’t feel like replicating their effort. Overall it looks like a clean design that can do a lot. But I’m stymied by the fact that you can no longer present it with the same list of items for it to print. Instead, I must change all my calls to something more like this:

error("text %d some dictionary %s" % (lineNumber, my_dict))

Or, I could do something really silly like this:

error(' '.join(map, str(("text", lineNumber, "some dictionary", my_dict))))

The question is, why omit such an obvious usage case? If you want to go from the typical ‘print’ statement straight to the new-fangled logging facility, shouldn’t this be easier?

As a follow-up question, can you think of a way to override the Logger class to perform 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-11T16:49:39+00:00Added an answer on May 11, 2026 at 4:49 pm

    I would suggest that it would be better to update the existing logging messages to the style that the logging module expects as it will be easier for other people looking at your code as the logging module will not longer function as they expect.

    That out of the way, the following code will make the logging module behave as you desire.

    import logging
    import types
    
    class ExtendedLogRecord(logging.LogRecord):
    
        def getMessage(self):
            """
            Return the message for this LogRecord.
    
            Return the message for this LogRecord after merging any user-supplied
            arguments with the message.
            """
            if not hasattr(types, "UnicodeType"): #if no unicode support...
                msg = str(self.msg)
            else:
                try:
                    msg = str(self.msg)
                except UnicodeError:
                    msg = self.msg      #Defer encoding till later
            if self.args:
                msg +=' '+' '.join(map(str,self.args))
            return msg
    
    #Patch the logging default logging class
    logging.RootLogger.makeRecord=lambda self,*args: ExtendedLogRecord(*args)
    
    some_dict={'foo':14,'bar':15}
    logging.error('text',15,'some dictionary',some_dict)
    

    Output:

    ERROR:root:text 15 some dictionary {'foo': 14, 'bar': 15}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • 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 No you don't need to revert, it will be quite… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer An anonymous namespace is logically equivalent to namespace _TU_specific_unique_generated_name {… May 11, 2026 at 11:31 pm
  • Editorial Team
    Editorial Team added an answer Database should always be designed and initially implemented in 3NF.… May 11, 2026 at 11:31 pm

Related Questions

I recently started experimenting with Python decorators (and higher-order functions) because it looked like
First, let me say that I'm a complete beginner at Python. I've never learned
The function code: # Connect to the DB try: dbi = MySQLdb.connect(host='localhost', \ user='user',
When I first started programming, I wrote everything in main. But as I learned,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.