SoFunction
Updated on 2025-05-12

Detailed explanation of the principle and solution of Flask to solve duplicate log printing

introduction

In Flask application development, log management is an easily overlooked but extremely important link. Many developers will experience duplicate log printing issues, especially in multi-threaded, multi-process or modular projects. This article will analyze the root cause of log duplication in detail and provide a complete set of solutions to help developers solve this problem thoroughly.

Problem background

When developing a phone number matching service, we found that each message in the log was repeatedly printed twice, for example:

2025-05-11 15:38:46,291 - app - INFO - File upload request - National Match: No, Receive email: ffffffhemo@
2025-05-11 15:38:46,291 - app - INFO - File upload request - National Match: No, Receive email: ffffffhemo@

This duplicate log not only interferes with debugging, but also occupies additional storage resources. After troubleshooting, we found that the root cause of the problem is that the log processor has been added and mixed with logging and .

The root cause of log duplication

1. Repeated addition of log processor

Flask's logging system will add a processor (such as console output) by default, while developers may manually add additional processors (such as file logs), resulting in each log being processed by multiple processors.

Error example:

def create_app():
    app = Flask(__name__)
    
    # Add a file processor    file_handler = TimedRotatingFileHandler('')
    (file_handler)
    
    # There is already one processor by default, and there are two processors in total    return app

2. Mix logging and

In Flask, it is an encapsulation of the Python standard library logging. If both are used at the same time, the log will be logged repeatedly.

Error example:

import logging
from flask import current_app

def some_function():
    ("Using the standard library logging")  # Record once    current_app.("Using Flask logger")  # Record the second time

3. Multithreaded or multi-process initialization

Multithreading: Background threads may repeatedly initialize logs.

Multi-process: When using gunicorn --workers=2, each process will initialize the log independently.

Solution

1. Unified use

Completely remove direct calls to logging and use or current_app.logger instead.

Fixed code:

from flask import current_app

def process_data():
    current_app.("Processing data")  # ✅Use Flask logger in unison

2. Make sure the log is only initialized once

In create_app, prevent repeated initialization by tagging:

def create_flask_app_with_configs() -> Flask:
    phone_app = PhoneApp(__name__)

    if hasattr(phone_app, "_logger_initialized"):
        return phone_app
    phone_app._logger_initialized = True  # Tags are initialized
    # Clear the default processor    phone_app.()

    # Add a custom processor    file_handler = TimedRotatingFileHandler("")
    phone_app.(file_handler)

    return phone_app

3. Fix the logs of background threads

In the background thread, the application context must be bound to use the current_app.logger:

def background_task():
    from flask import current_app
    with current_app.app_context():
        current_app.("Background task execution")  # ✅ The correct way

4. Disable Flask default logs (optional)

Disable Werkzeug's default access log to reduce interference:

# Disable Werkzeug logswerkzeug_logger = ('werkzeug')
werkzeug_logger.()
werkzeug_logger.setLevel()

Completely fixed code

Here are the core parts that have been completely repaired:

import os
import threading
from flask import Flask, current_app
from  import TimedRotatingFileHandler

class PhoneApp(Flask):
    pass

def create_flask_app_with_configs() -> Flask:
    phone_app = PhoneApp(__name__)

    if hasattr(phone_app, "_logger_initialized"):
        return phone_app
    phone_app._logger_initialized = True

    # Clear the default processor    phone_app.()

    # File log processor    file_handler = TimedRotatingFileHandler(
        "", when="midnight", backupCount=7
    )
    phone_app.(file_handler)

    return phone_app

def create_app() -> Flask:
    app = create_flask_app_with_configs()
    ("Application initialization completed")  # ✅Use in a unified way    return app

if __name__ == "__main__":
    app = create_app()
    (use_reloader=False)  # Close the debug reloader

Verify that the log is repaired

Check log files: Confirm that each log only appears once.

Test multi-threading: start the background task and observe whether the log is normal.

Production environment testing: Use gunicorn multi-worker testing to ensure no duplication.

Summarize

question reason Solution
Repeat log printing The processor has been added several times Clear the default processor and make sure it is initialized only once
Mixed useloggingand Logs are recorded in two ways Unified use
Multithreaded/multi-process issues Each thread/process is initialized independently Mark initialization status, bind context

Through the above methods, you can completely solve the problem of Flask log duplication, making the log system clear and efficient!

This is the article about the principles and solutions for repeated printing of Flask's logs. For more related contents for repeated printing of Flask's logs, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!