简体   繁体   中英

Set Python Logging to overwrite log file when using dictConfig?

I'm using dictConfig() to configure my logging. I want the process to overwrite the specified log file every time the process is run. How do I do this?

I see filemode as a setting in basicConfig() but I can't figure out where to put this in the dictConfig() configuration.

I've tried this but get an unexpected keyword argument 'filemode' error. I've tried it in a few other places too. Python logging docs are confusing as hell!

LOG_PATH                    = os.path.join(PROJECT_PATH,'logs')
LOG_FILE_NAME               = 'log.'+main.__file__+'.'+time.strftime("%Y-%m-%d")
LOG_FILE_PATH               = os.path.join(LOG_PATH,LOG_FILE_NAME)
LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': { 
        'standard': { 
            'format': '[%(levelname)s] %(message)s - [pid:%(process)d - %(asctime)s - %(name)s]',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'formatter': 'standard',
            'class': 'logging.StreamHandler',
        },
        'file': {
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.FileHandler',
            'filename': LOG_FILE_PATH,
            'filemode': 'w',
        },
    },
    'loggers': {
        '': { 
            'handlers': ['console','file'],
            'level': 'DEBUG',
            'propagate': True
        },
    },
}
logging.config.dictConfig(os.path.join(LOGGING_CONFIG))
logger = logging.getLogger(__name__)
logger.debug('logger configured')

ANSWER


Thanks to @Vinay Sajip for his selected answer below. Here is my updated configuration that now overwrites the specified log file every time the process is run. I simply added LOGGING_CONFIG['handlers']['file']['mode'] = 'w' .

LOG_PATH                    = os.path.join(PROJECT_PATH,'logs')
LOG_FILE_NAME               = 'log.'+main.__file__+'.'+time.strftime("%Y-%m-%d")
LOG_FILE_PATH               = os.path.join(LOG_PATH,LOG_FILE_NAME)
LOGGING_CONFIG = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': { 
        'standard': { 
            'format': '[%(levelname)s] %(message)s - [pid:%(process)d - %(asctime)s - %(name)s]',
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'formatter': 'standard',
            'class': 'logging.StreamHandler',
        },
        'file': {
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.FileHandler',
            'filename': LOG_FILE_PATH,
            'mode': 'w', # <=== HERE IS THE CHANGE
        },
    },
    'loggers': {
        '': { 
            'handlers': ['console','file'],
            'level': 'DEBUG',
            'propagate': True
        },
    },
}
logging.config.dictConfig(os.path.join(LOGGING_CONFIG))
logger = logging.getLogger(__name__)
logger.debug('logger configured')

You need to use mode rather than filemode . In general, you need to use the argument names specified in the documentation for the handler initialisation - see here for FileHandler .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM