Permission error during rotation logging with flask

Edoardo

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).

coldcod

Based on the error description, it seems like your log file might be locked or opened by another process, causing the RotatingFileHandler to fail when it tries to write to the file. This could potentially be due to how the Flask debug mode reloader works.

When you run a Flask application with debug=True, Flask uses a reloader that restarts your application whenever a change is detected. This essentially means your Flask application is running in two separate processes - one for the reloader and one for the application itself. It's likely both processes are trying to access the log file at the same time, causing the error you're seeing.

To work around this issue, you can add a check to ensure the logger configuration is only set up in the main application process and not in the reloader process. This can be done by checking the WERKZEUG_RUN_MAIN environment variable, which is set to 'true' in the main application process.

Here's an example of how you could modify your code:

from flask import Flask
import logging
from logging.handlers import RotatingFileHandler
import os

app = Flask(__name__)

# Only set up logging in main process, not in reloader
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s - %(message)s",
        handlers=[RotatingFileHandler("./app.log", maxBytes=100, backupCount=5)],
    )

logger = logging.getLogger("DemoLogger")

# rest of your code

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

This will ensure the logger is only set up in the main process, and should prevent the file access error you're seeing.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

django error logging : [Errno 13] Permission denied

Permission error when configuring credential rotation in AWS Secret Manager

Flask SocketIO: Error during WebSocket handshake

The caller doesnot have permission error while logging in to google api console

Permission denied error when logging to /var/log/<file-name>

Select which rows are written during SSIS error logging tasks

Error stat of log failed: Permission denied during LogRotation

Kivy rotation during movement

Python Flask Error Logging wont work with HTTP Handler

auditd not compressing logs during rotation

MvvmCross ViewModel lifecycle during rotation

Disabling of sprite rotation during motion

Logging in flask with gunicorn

Logging inside a Flask microservice

Flask logging not working at all

Logging format for Flask on PythonAnywhere

flask logging duplicate output

conda and flask not logging anything

Flask-login not logging in

webstart application logging permission denied

Logging and expected Exceptions during testing

How to handle exceptions during logging?

disable python logging during unittest

Apache Spark upgrade from 1.5.2 to 1.6.0 using homebrew leading to permission denied error during execution

Google Cloud Storage permission error while setting file visibility during upload

Execution of the command buffer was aborted due to an error during execution. Insufficient Permission (to submit GPU work from background)

Python, want logging with log rotation and compression

forcing a log rotation in python logging module

Docker Permission Denied During Setup