I have a problem with my flask code. I want to Rotate-log my log files. If i write the configuration code for the logs and i run my flask application, it goes in error and tells me that my file is already accessing from another process. I tried to start the application before and then paste the configuration, save the project (with flask debug mode set as true) and this problem does not appear, the rotation works well. Also if i use the classic debugger and watch what happens line by line, with the configuration already in the code, it doesn't show me the problem.
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(message)s",
handlers=\[
RotatingFileHandler("./app.log", maxBytes=100, backupCount=5)
\],
)
logger = logging.getLogger("DemoLogger")
This is the configuration.
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
I run my flask application like this
(The error is too long for paste it, hope you understand the problem and can help me).
More a workaround, than an answer, but I removed
debug=Truefrom theapp.run()call, and it works as expected.I guess that's because if debug mode is activated, flask actually restarts the entire application. In your log somewhere, there should be a line stating
INFO:werkzeug: * Restarting with stat. I guess that somehow breaks file access.Unfortunately, I'm still stuck at this and can't provide anything better than this workaround for now.
Edit: Never calling the rollover (rotation) should also work, but that's not the point, I guess.
Edit2: found another “solution”
I dug far too deep into this, but I found something: flask uses the module werkzeuge for quite some stuff. The reloading of the server in debug mode is part of this, hence the log about restarting with stat. (fyi: stat is the reloading service. werkzeuge also provides watchdog, a more capable service, that will be used by default if installed. See the werkzeuge docs ) The problem is, that stat is reloading the application, thus running your logger configuration twice, without terminating the first one before. So you try to write to the same file from two instances of your application. That's the root of your problem. You can do at least two things to recover from this:
run app with
use_reloader=Falseif you don't need the hot reload feature of the flask debugger and restart your server manually every time you change something in the code, this will be enough to get your rotating logs running.use
werkzeug.serving.is_running_from_reloader()to check if you can access the log files for write I'm not sure about your setup and how to implement this. By mine was quite simple. I just wanted to rotate the logfiles on app start. So I checked on start if a logfile exists. If so, I rotate. Now, I additionally check, if the application is running inside the reloader. If so I do not rotate again, but keep the original logger run and log stuff as expected.