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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:13:39+00:00 2026-06-08T19:13:39+00:00

as you might know the python logging module supports the definition of *kwargs to

  • 0

as you might know the python logging module supports the definition of *kwargs to customize log entries. This appears to be not supported for logging exceptions.

My Question: Is there an easy way to use custom kwargs with logging.exception?

A minimal example follows:

import logging
import logging.handlers

def create_logger(): 
    """ initialize logging using sys log

    """
    logger = logging.getLogger()

    form = '%(asctime)s [%(levelname)-8s] %(xyz)s %(message)s'
    local_log_handler = logging.handlers.SysLogHandler(address='/dev/log')
    formatter = logging.Formatter(form)
    local_log_handler.setFormatter(formatter)

    logger.addHandler(local_log_handler)
    return logger

log = create_logger()
try:
    raise AttributeError("whatever")
except:
    log.warn('Alarm! Fire!', extra={'xyz':'POLICE'})
    log.exception('')

Output:

Traceback (most recent call last):
  File "logexample.py", line 27, in <module>
    log.exception('')
  File "/usr/lib/python2.7/logging/__init__.py", line 1172, in exception
    self.error(msg, exc_info=1, *args)
  File "/usr/lib/python2.7/logging/__init__.py", line 1166, in error
    self._log(ERROR, msg, args, **kwargs)
  File "/usr/lib/python2.7/logging/__init__.py", line 1258, in _log
    self.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1268, in handle
    self.callHandlers(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 1308, in callHandlers
    hdlr.handle(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 748, in handle
    self.emit(record)
  File "/usr/lib/python2.7/logging/handlers.py", line 791, in emit
    msg = self.format(record) + '\000'
  File "/usr/lib/python2.7/logging/__init__.py", line 723, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 467, in format
    s = self._fmt % record.__dict__
KeyError: 'xyz'

Syslog:

Jul 30 14:56:27 localhost 2012-07-30 14:56:27,131 [WARNING ] POLICE Alarm! Fire!

PS: A little background, I am writing a process with threading and I want to log information in the following manner:

[app-thread1]
[app-thread2]
…

  • 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-06-08T19:13:41+00:00Added an answer on June 8, 2026 at 7:13 pm

    You can just use e.g.

    logger.error('Message with %s', 'args', exc_info=1, extra={...})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this might seem like a duplicate, but I'm not getting anywhere with
I know this might seem a controversial question but it really is not meant
I know perl and python is tested solution for this kind of log parsing
I know this might end up being language specific, so a Java or Python
I know Python (and a bunch of other languages) and I think it might
As you all might know that the MIPS instruction set supports clz (count leading
I know this might sound easy. I thought about using the first dot(.) which
I know this might be a stupid question, but here it goes. I always
I know this might be a long shot, but here it goes. I have
I know this might be a bit awkward but I am trying to modify

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.