Python Flask Logging Log Reports An error "Another program is using this file, the process cannot be accessed"

tags: Python  flask  logging  

Problem

Python Flask Logging Log Reports An error "Another program is using this file, the process cannot be accessed"

2. Code example

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author:   craftsman

import os
import datetime
import decimal
import logging
from logging.handlers import TimedRotatingFileHandler
import redis
import json
from flask import Flask, request, make_response, current_app, g
from flask import render_template
from flask import jsonify
from flask import send_from_directory
from flask_sqlalchemy import SQLAlchemy
import flask.json
from config import sql_db_config

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
LOG_PATH = os.path.join(BASE_DIR, 'logs')
app = Flask(__name__)
# Configure log information
# Set the log level of the log
logging.basicConfig(level=logging.INFO)
# Create a logging device, specify the path saved by the log, the maximum size of each log file, saved the number of log files
file_log_handler = TimedRotatingFileHandler(filename=os.path.join(LOG_PATH, 'info.log'),
                                            when='midnight', backupCount=31, interval=1, encoding='utf-8')
# Create a logging format log level Enter the file name row log information for log information
formatter = logging.Formatter('[%(levelname)s %(filename)s:%(lineno)d]: %(message)s')
# Set the logging format for the log recorder you just created
file_log_handler.setFormatter(formatter)
# Add a day recorder for the global log tool object (used by Flask App)
logging.getLogger().addHandler(file_log_handler)
app.logger.addHandler(file_log_handler)

3. Reason

When using RotatingFileHandler or TimerotatingFileHandler in Flask, this is the case when the log file is backed up, and it is written to the same log file, and write a conflict.

4. Solve

pip install concurrent-log-handler
After installation, the code above will not report any more, and the log will automatically back up.

appendix:
For details, please refer to: https://github.com/preston-landers/concurrent-log-handler


Plunder
Point of attention
Thank you

Intelligent Recommendation

IIS deployment local website prompts that the website cannot be started, another program is using this file, and the process cannot be accessed

Today, the website deployed with IIS cannot be started locally. The default port of https is 443. In order to avoid the port starting with 4 on the local computer, I tried to use port 555, and the pro...

Kafka 0.11x automatically stops after 30 seconds of startup, reported [another program is using this file, the process cannot be accessed]...

Environment: kafka_2.11-1.1.0, win7_64, java8 Phenomenon: automatically stops after 30 seconds of startup, reported [another program is using this file, the process cannot be accessed] It can be seen ...

Tips when compiling idea: another program is using this file, the process cannot be accessed; the problem is fixed

Regarding the idea when compiling idea: Another program is using this file and the process cannot be accessed. Problem repair record First report the original text: Error:Maven Resources Compiler: Fai...

[Winerror 32] Another program is using this file, the process cannot be accessed, # image processing

When making image processing, I used three modules OS, PIL, CV2. I encountered a bug who plagued one morning, I investigated several resource monitor ~~~, bug as follows: Description of Requirement: 1...

Permissionerror: [Winerror 32] Another program is using this file, and the process cannot be accessed. Unable to delete files

Question: After posting the file to the remote interface, open the excel file for classification and processing, and the excel file cannot be deleted. Reason: Requests.post is not closed. Solution: Ma...

More Recommendation

Virtual machine startup error: Another program has locked part of the file and cannot be accessed by the process

The virtual machine suddenly reported an error after a certain system as follows: Seeing this interface, although I don’t know the specific reason for the error, I can roughly guess the reason f...

2021-05-05 Using Exifread to find a picture to shoot the date, rename the file, another program is using this file, and the process cannot be accessed.

Use Exifread to find a picture date, rename the file, and another program is using this file, the process is unacceptable. Error prompt appears: Traceback (most recent call last):   File "C:...

An error was reported when starting kafka: java.nio.file.FileSystemException, another program is using this file, and the process cannot access it.

Paste the error message first Cause Analysis: Failed to clean up the consumer_offset record in the D:\tmp\kafka-logs directory consumer_offset is the topic that Kafka automatically saves the offset, a...

VMware startup error: Another program has locked part of the file and cannot be accessed by the process (delete the most recent .lck folder)

I can't shut down the virtual machine, so I restarted the computer directly, and it turned out like this after restarting. reason: .lck file is a disk lock file of VMWare software. Since the virtual d...

【Solution】Moving Logs/AssetImportWorker 1.log to Logs/AssetImportWorker 1-prev.log: Another program is using this file and the process cannot access it.

Game development platform: Unity Programming platform: Visual Studio 2017 or above Languages ​​spoken: C#   Problem description Moving Logs/AssetImportWorker 1.log to Logs/AssetImportWorker 1-pre...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top