Log rotation is working fine when I log to the current working directory, but get a PermissionError when I am trying to log to a separate directory within the current directory.
Is there a way to handle this without a need for -
- additional function to be written
- log to the cwd and move the renamed file to the log directory post rotation?
Based on the docs, I have tried adding delay=True
without any luck.
I have also reviewed other posts that talk about the PermissionError but are slightly different to the issue here.
Any insights would be helpful. Thanks
log_file_name = os.path.join(os.getcwd()+'\\'+'logs'+'\\'+'sample.log')
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s] [%(name)s] [%(funcName)s()] [%(levelname)s] : %(message)s')
file_handler = handlers.TimedRotatingFileHandler(log_file_name, when='midnight', backupCount=100, delay=True)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: