python logging: multiple loggers error
Question: I have objects called Job which has it's own logger (each Job need to have a
log file which is represented by logging.getLogger())

The problem is I create thousands of Jobs (~4000) and they all want to create
a logger.

    
    
    Traceback (most recent call last):
      File "/u/lib/btool/Job.py", line 151, in __init__
      File "/usr/lib/python2.7/logging/__init__.py", line 911, in __init__
      File "/usr/lib/python2.7/logging/__init__.py", line 936, in _open
    IOError: [Errno 24] Too many open files: '/x/zooland/20160710-032802.log'
    

Is there way to deal with multiple loggers?


Answer: Here's a custom file handler that stores the log message and then closes the
file.

    
    
    import logging
    
    
    class MyFileHandler(logging.Handler):
        def __init__(self, filename):
            self.filename = filename
            super().__init__()
    
        def emit(self, record):
            log_text = self.format(record)
            try:
                fh = open(self.filename, "a")
                fh.write(log_text)
                fh.close()
    
                return True
            except:
                return False
    
    
    logger = logging.getLogger("job")
    
    handler = MyFileHandler("file-1")
    logger.addHandler(handler)
    
    logger.error("hola")
    

