使用TimedRotatingFileHandler时权限被拒绝

时间:2019-01-15 07:44:12

标签: python logging flask

为此我使用Flask-RESTFUL创建了REST API,我需要为此生成日志文件

必须重新运行API时出现错误。登录文件时会发生这种情况。

 PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:

为避免此错误,我不得不删除该文件,但是当另一台计算机尝试访问API时也会发生相同的错误。

我有config.py代码,用于初始化日志记录模块的应用程序配置。

        import os
        from datetime import date
        import logging
        import logging.handlers as handlers

        class BaseConfig(object):
            DEBUG = False
            TESTING = False
            LOGGING_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
            #LOGGING_LOCATION = 'logs/bookshelf.log'


        class DevelopmentConfig(BaseConfig):
            DEBUG = True
            TESTING = True



        class TestingConfig(BaseConfig):
            DEBUG = False
            TESTING = True

        class ProductionConfig(BaseConfig):
            DEBUG = False
            TESTING = False



        config = {
            "development": "config.DevelopmentConfig",
            "testing": "config.TestingConfig",
            "default": "config.DevelopmentConfig",
            "production": "config.ProductionConfig"
        }


        def configure_app(app):

            #ENVRIONMENT CONFIGURATION
            config_name = os.getenv('FLASK_CONFIGURATION', 'default')
            #config_name = os.getenv('FLASK_CONFIGURATION', 'production') #UNCOMMENT IF PROD
            app.config.from_object(config[config_name]) # object-based default configuration
            #handler = logging.FileHandler(app.config['LOGGING_LOCATION'])
            handler = handlers.TimedRotatingFileHandler('logs/'+str(date.today())+'-mipay-error.log', when='M', interval=1)
            formatter = logging.Formatter(app.config['LOGGING_FORMAT'])
            #handler.setLevel(app.config['LOGGING_LEVEL'])
            handler.setFormatter(formatter)
            app.logger.addHandler(handler)
            #LOGGING CONFIGURATION

我已阅读到需要关闭文件,但是我不知道何时以及如何关闭日志文件。

0 个答案:

没有答案