Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

python - Permission error when initialising RotatingFileHandler logging in Flask app factory

There are a lot of similar questions on SO, but I couldn't one that exactly fit my situation.

I'm setting up logging in my app factory like so:

__init__.py

import os
from flask import Flask
from logging.config import dictConfig
LOG_FOLDER = f'{os.path.dirname(os.path.abspath(__file__))}/logs'

def create_app(test_config=None):
    # Setup logging
    # Make log folder if it doesn't exist
    try:
        os.makedirs(LOG_FOLDER)
        print("created logs folder")
    except OSError:
        print("log folder already exists")
        pass

    dictConfig({
    "version": 1,
    "handlers": {
        "fileHandler": {
            "class": "logging.handlers.RotatingFileHandler",
            "formatter": "myFormatter",
            "filename": f"{LOG_FOLDER}/flask.log",
            "maxBytes": 500,
            "backupCount": 5
        },
        "werkzeugFileHandler": {
            "class": "logging.handlers.RotatingFileHandler",
            "formatter": "myFormatter",
            "filename": f"{LOG_FOLDER}/werkzeug.log",
            "maxBytes": 500,
            "backupCount": 5
        },
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "myFormatter"
        }
    },
    "loggers": {
        APP_NAME: {
            "handlers": ["fileHandler", "console"],
            "level": "INFO",
        },
        "werkzeug": {
            "level": "INFO",
            "handlers": ["werkzeugFileHandler", "console"],
        }
    },

    "formatters": {
        "myFormatter": {
            "format": "[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s"
        }
    }
})

    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)

    <remainder omitted>

And accessing the logger in my other classes like so:

foo.py

from flask import Flask
from definitions import APP_NAME

app = Flask(APP_NAME)

app.logger.info("blah")

But when it comes time for RotatingFileHandler to rename flask.log to flask.log.1, I get this error I've seen in numerous SO posts:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\user\project_root\logs\flask.log' -> 'C:\Users\user\project_root\logs\flask.log.1'

I am running the flask server locally in development mode, using the flask run cli command.

Another thing to note is, when the flask server is running, I am unable to modify (i.e. delete or rename) the log files manually, so it seems the mere act of having the server running is locking the files from modification? Is it wrong to initialise the logging in __init__.py, or is there something I'm missing?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...