I'm using the below module to log events in my modules. I call it as follows:
module 1
from tools.debug_logger import debug_logger
self.logger = debug_logger().start_logger('module1')
self.logger.debug("Top left corner found")
module2:
from tools.debug_logger import debug_logger
self.logger = debug_logger().start_logger('module2')
self.logger.debug("Top left corner found")
and here the file /tools/debug_logger.py
import logging, logging.handlers
import sys
class debug_logger(object):
def start_logger(self,name):
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
if not len(logger.handlers):
fh = logging.handlers.RotatingFileHandler('log/pokerprogram.log', maxBytes=1000000, backupCount=10)
fh.setLevel(logging.DEBUG)
fh2 = logging.handlers.RotatingFileHandler('log/pokerprogram_info_only.log', maxBytes=1000000, backupCount=5)
fh2.setLevel(logging.INFO)
er = logging.handlers.RotatingFileHandler('log/errors.log', maxBytes=2000000, backupCount=2)
er.setLevel(logging.WARNING)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(1)
fh.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
fh2.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
er.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
ch.setFormatter(logging.Formatter('%(name)s - %(levelname)s - %(message)s'))
logger.addHandler(fh)
logger.addHandler(fh2)
logger.addHandler(ch)
logger.addHandler(er)
return logger
Everything works fine and I get log files for the respective levels, but when the RotatingFileHandler is called I sometimes get an error. It is as if various instances want to do the rotation at the same time, even though I'm quite sure this shouldn't happen as I make sure there's only 1 handler with if not len(logger.handlers)
as recommended here: Duplicate log output when using Python logging module.
Any suggestion what could be causing this file access violation during the rotation would be appreciated.
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\Nicolas\\Dropbox\\PythonProjects\\Poker\\log\\pokerprogram.log' -> 'C:\\Users\\Nicolas\\Dropbox\\PythonProjects\\Poker\\log\\pokerprogram.log.1'
I believe the permission error occur because when the rotation occurs other modules are still writing to the file.
What would be the best way to do logging from multiple modules when I write to a file and use this RotatingFileHandler? Is there any best practice?
if not len(logger.handlers):
I ensure there is only 1 handler. What's a better way to do this? – EdytheeegetLogger
are different and hence you are adding one handler for module1 and one for module2. So, sure a single logger does not have more than one handler, but you do have two handlers using the same file and this causes the error you see. – Jerrelljerrimodule1
andmodule2
should only import thelogging
module and get their loggers vialogging.getLogger(__name__)
. Everything else regarding configuration of the logging output is handled from the executable. This makes sense because it's the guy that launches the application that should be able to decide where to log something and at which level, not the module itself! – Jerrelljerri