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

  • Home
  • SEARCH
  • 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 8072351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:59:30+00:00 2026-06-05T13:59:30+00:00

I have a server application for handling XMLRPC requests. Each connection creates its own

  • 0

I have a server application for handling XMLRPC requests. Each connection creates its own thread. I would like to setup logging so that some thread/connection specific information gets in the log. I could do something like:

import logging

@add_unique_request_id
def thread_top(request_id):
    logging.info('thread %d says: hello'%request_id)
    logging.error('thread %d says: darn!!'%request_id)

and setup the global root logger as I wish, but I do not like this solution. I would like the following instead:

import logging

@setup_logger
def thread_top():
    logging.info('hello')
    logging.error('darn!!')

But I have no idea how the setup_logger deco should look like. I came up with a workaround to use separate process for each request, then setting the root logger in each process would do exactly what I want. Is there some way to make this work without being forced to use multiprocessing?

Thank you!

  • 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-05T13:59:31+00:00Added an answer on June 5, 2026 at 1:59 pm

    I’m doing this with a custom formatter and threadlocal storage:

    from collections import defaultdict
    import logging
    import threading
    
    class ContextAwareFormatter(logging.Formatter):
        """
        Makes use of get_context() to populate the record attributes.
        """
    
        def format(self, record):
            # Using defaultdict to avoid KeyErrorS when a key is not in the context.
            def factory():
                return ""
            record.__dict__ = defaultdict(factory, record.__dict__)
    
            for k, v in get_context().iteritems():
                if not hasattr(record, k):
                    setattr(record, k, v)
            return logging.Formatter.format(self, record)
    
    THREADLOCAL_ATTR = "logging_context"
    _threadlocal = threading.local()
    
    def get_context():
        result = getattr(_threadlocal, THREADLOCAL_ATTR, None)
        if result is None:
            result = {}
            setattr(_threadlocal, THREADLOCAL_ATTR, result)
        return result
    
    def set_context(**context):
        c = get_context()
        c.clear()
        c.update(**context)
        return c
    
    def update_context(**context):
        c = get_context()
        c.update(**context)
        return c
    

    Then in logger configuration:

    "formatters": {
        "default": {
            "()": "log.ContextAwareFormatter",
            "format": "%(asctime)s %(levelname)s [%(request_id)s] %(message)s (%(module)s:%(lineno)d)",
        },
    }
    

    Before logging the context is populated with:

    update_context(request_id=request_id)
    

    You may want to use different formatters for different parts of application where you might not need request_id in the log records.

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

Sidebar

Related Questions

I have a client/server type application setup, similar to a bittorrent downloading program. However,
I have a server application that is compiled in 32 bit, and I want
I have a Client/Server application, where the Client and Server have some common tables
We have a server application that we want to restrict non-users from triggerring it
I have a server application that consists of multiple OSGi bundles, some mine, some
I have a server application that needs to find and exchange small amounts of
I have a server application receiving messages from a JMS queue. And client applications
I have a server application that writes to a popen(myCommand, w) file descriptor in
I have a server application that, in rare occasions, can allocate large chunks of
I have a server application with such structure: There is one object, call him

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.