I think this problem happens only on Windows as Windows doesn't let you rename file while it is open. Not tested on linux though.
I've tried to simplify code to show you issue clearly. The following code generates logfile defined in dictConfig and logs to that file sucessfully. After a minute (rotation interval), code reports an error saying PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: It seems rotate function fails to rename file as it is already opened, so rotation fails. For now, the code logs to one file without rotation, but this will generate a giant log file after a while. Any help to resolve it?
Test code:
from loggerConfig import LOGGING
import logging
from logging.config import dictConfig
from flask import Response
import flask
dictConfig(LOGGING)
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def home():
amLogger.debug("RX-GET")
return Response("<h1>Alert listener working.</h1> <p>Alert listener working.</p>", status=200)
amLogger = logging.getLogger('alerting')
app.run(host="0.0.0.0", port=5000, threaded=True)
loggerConfig.py:
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'default': {
'format': '%(asctime)s — %(name)s — %(levelname)s — %(message)s'
}
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.TimedRotatingFileHandler',
'formatter': 'default',
'filename': "path/to/log-file/alertListener.log",
'when': 'm',
'interval': 1,
'delay': True
}
},
'loggers': {
'': {
'handlers': ['file'],
'level': 'DEBUG',
'propagate': False
},
}
}