Python native registration with file machines

I am using python logging in my django application. The class that connects to the backend api initializes this registrar, if necessary, using a file handler. A class is created every time api is called. I tried to make sure that additional handlers are not added every time, but

lsof | grep my.log 

shows an increase in the number of handlers in my log file, and after a while my server crashes 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 understand that this is not the best way to do this, but I have not yet found an error in my implementation.

+5
source share
1 answer

, , concurrency.

/ .

? , , . - , ... , , ( ), , - . .

: ?

, logging.conf // , (, ), . , django, __init__.py :

import logging
log = logging.getLogger(__name__)

log (, ..).

logging.conf, settings.py:

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

, , , .

( ) , , . .

, , , . , , -. , 404 ( - ) ( ).

, . : .

+1

All Articles