Flask logging does not write to the log file in time

problem description

flask logging does not write to the log file in time

related codes

    handler = logging.FileHandler("flask.log", encoding="UTF-8")

    handler.setLevel(logging.DEBUG)

    logging_format = logging.Formatter(
        "%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s")

    handler.setFormatter(logging_format)

    app.logger.addHandler(handler)
        current_app.logger.debug("A value for debugging")
        current_app.logger.warning("A warning occurred (%d apples)", 42)
        current_app.logger.error("An error occurred")

what result do you expect? What is the error message actually seen?

write to the log file immediately to check for errors. Now it is time to close or restart to write to the log file

Apr.05,2021

In fact, this may not have much to do with flask , because at the level of python and system , there is a concept of buffering for file IO, in order to reduce waiting for IO.

because you are writing files, the buffering strategy is full buffering , so you will wait until the buffer is full or manual flush will be submitted to the system write queue, and then drop the disk. The size of the buffer varies with the system, but it is usually 4096 bytes.

related information:

I/OreadwriteI/OI/Omalloc:
    1) I/OI/O
    2) I/OI/Ostdinstdout
    3) readwritestderr
Menu