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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:39:47+00:00 2026-05-20T20:39:47+00:00

I am using python logging in my django application. A class that connects to

  • 0

I am using python logging in my django application. A class that connects to a backend api initialises this logger with a filehandler if needed. The class gets instantiated everytime an api call is made. I have tried making sure additional handlers are not added every time, but

lsof | grep my.log 

shows an increasing amount of handlers on my log file and after a while my server fails due to this open file limit.

 self.logger = logging.getLogger("FPA")

        try:
            if self.logger.handlers[0].__class__.__name__=="FileHandler":
                pass
        except Exception, e:
            print 'new filehandler added'+str(e)
            ch = logging.FileHandler(FPA_LOG_TARGET)
            formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s - %(pathname)s @ line %(lineno)d")
            ch.setFormatter(formatter)
            self.logger.setLevel(logging.DEBUG)
            self.logger.addHandler(ch)

I realise this may not be the best way to do this, but I have not found the error in my implementation so far.

  • 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-20T20:39:49+00:00Added an answer on May 20, 2026 at 8:39 pm

    I did not analysed if for a long time, but it looks like a concurrency problem.

    Each process/thread is keeping it’s own list of the file handles to the opened log files.

    How to fix it? For the multithreaded code, make sure that there is a global dictionary where all handles are kept. For the multiprocess – I’m afraid I do not have an answer… each process is keeping it’s own file-handles, maybe mapping it to the memory (memory mapped files could be an option), but I’m not sure that this is good solution – see this remark.

    But the main question is why do you need to do such a thing.

    First of all, you can use logging.conf file to initialize all your loggers/handlers/formatters and when needed (e.g. specific loger is extensive and you want to log it to the separate file) add another logger with different filename. Which is quite sensible if you will add one logger per django app, by adding in the main __init__.py of the app:

    import logging
    log = logging.getLogger(__name__)
    

    and then import log in the rest of the app code (views, models, etc.)

    To use logging.conf add following lines to your settings.py:

    import os
    import logging
    DIRNAME = os.path.abspath(os.path.dirname(__file__))
    logging.config.fileConfig(os.path.join(DIRNAME, 'logging.conf'))
    

    Yes, it is manual, but you do not need to change code, but simply a config file.

    Another approach (if you really want to have one file per logger type) is to have a separate process which will keep files open, accept connections from the application. Logging module documentation has a nice example of this method.

    Last, but not least there are already some nice solutions which may be helpful. One, quite good, is to use django-sentry. This module can log all your exceptions, 404 (with extra middleware – included) and capture all logging (via included logging handler).

    Provided UI will gave you a possibility to search all the logged messages, filter them by the severity and logging source. But this is not limited to those – you can simply add your own modules.

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

Sidebar

Related Questions

I am writing a Python/Django web application that uses OAuth (for the TwitterAPI, not
I'm trying to configure logging for a Django app using the Python logging module.
I am using python's logging module in a django project. I am performing the
I am using the basic python logger in django and it seems to be
I want to start using Python for small projects but the fact that a
I'm using python and CherryPy to create a simple internal website that about 2
I have a Django project where I have been logging to a file using
I am using Python 2.7 as this seems to be only Python MSI downloadable
Im trying to create a logging output using python 2.6. The data come a
Using Python I want to be able to draw text at different angles using

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.