mode = 'w' is only working with logging.FileHandler but not with logging.handlers.RotatingFileHandler

Viewed 622

I'm trying to create logs using following logging config. But in handler 'info_file_handler', mode: 'w' does not overwrite the file with class:logging.handlers.RotatingFileHandler. should I replace the class with logging.FileHandler, output log file gets overwrite. does logging.handlers.RotatingFileHandler need some extra code to be added here?

# https://gist.github.com/kingspp/9451566a5555fb022215ca2b7b802f19
version: 1
disable_existing_loggers: true
formatters:
    standard:
        # format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        format: "%(levelname)s: %(message)s"
    error:
        format: "%(levelname)s <PID %(process)d:%(processName)s> %(name)s.%(funcName)s(): %(message)s"

handlers:
    console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: standard
        stream: ext://sys.stdout

    info_file_handler:
        class: logging.handlers.RotatingFileHandler
        level: INFO
        formatter: standard
        filename: info.log
        mode: 'w'
        maxBytes: 10485760 # 10MB
        backupCount: 20
        encoding: utf8

root:
    level: NOTSET
    handlers: [console]
    propogate: no

loggers:
    my_module:
        level: INFO
        handlers: [info_file_handler]
        propogate: no
1 Answers

I have the same problem. How to rotate file at each logging starts with specified file size rotation. From logging.handlers :

        If maxBytes is zero, rollover never occurs.
        """
        # If rotation/rollover is wanted, it doesn't make sense to use another
        # mode. If for example 'w' were specified, then if there were multiple
        # runs of the calling application, the logs from previous runs would be
        # lost if the 'w' is respected, because the log file would be truncated
        # on each run.
        if maxBytes > 0:
            mode = 'a'

IMO. It seems another mode than 'a' is useless, so 'mode' could be removed.(?)

Related