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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:14:35+00:00 2026-05-23T15:14:35+00:00

I have a logging setup in python application that logs into file and MongoDB.

  • 0

I have a logging setup in python application that logs into file and MongoDB. The setup looks like this:

[logger_root]
handlers=myHandler,mongoHandler
level=DEBUG
qualname=myapp

[handler_myHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=myFormatter
args=('myapp.log', 'a',20000000,10)

[handler_mongoHandler]
class=myapp.MongoLogger.MongoLogger
level=INFO
args=('log',)

[formatter_myFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s

And the MongoLogger has emit() function like this:

def emit(self, record):
    logdata = record.__dict__
    try:
        if(self.data == None):
            self.initDb()
        self.logtable.insert(logdata)
    except:
       self.handleError(record)

The logging is done like this then:

 logger.info("Processing account %s..." % account)

It works reasonably well, but now I have an additional requirement. I want it to have some context – i.e., to be able to define custom value – say, account name – so every log done inside the account processing would have the account name as part of the record passed to emit above and also available for the formatter in myFormatter string.

Is it possible to do this with logging module? Is there maybe another, better way to do the same?

  • 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-23T15:14:36+00:00Added an answer on May 23, 2026 at 3:14 pm

    You can define a function inside the account processing code, after you get the account name, like this:

    # account_name should already be defined
    log = lambda msg: logger.info(msg, extra={'account': account_name})
    
    ###
    
    log('Processing account...')
    

    Note the extra keyword argument. It is used to add additional context to log records—in this case, the account name.

    You can use context passed via extra in the formatter:

    format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s - %(account)s'
    

    Note that if you set up formatter like this and forget to pass account, you’ll get a string formatting exception.

    • More information on extra keyword argument
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an appender setup like this <appender name=Scheduler_Appender type=log4net.Appender.RollingFileAppender> <file value=c:\temp\ApplicationLog.txt/> <rollingStyle value=Date/>
I have a Java web application that currently uses Log4J for logging. I'd like
I've got a project that I'm working Enterprise Library logging into, and that application
We have a PHP/MySQL application and I want to setup a logging mechanism to
I have successfully configured an application that uses log4j for it's logging to log
I have this setup in my symfony application: # settings.yml prod: .settings: no_script_name: true
I have a console application for which I am trying to setup logging with
I have setup an Application that uses @Anywhere from twitter to Authenticate the users.
I have a python logger set up, using python's logging module. I want to
I have a problem wih a logging setup in a apring webapp deployed under

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.