Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Python Logging to overwrite log file when using dictConfig?

Tags:

python

logging

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')
like image 315
T. Brian Jones Avatar asked Aug 18 '17 00:08

T. Brian Jones


People also ask

How do you overwrite a logging file in Python?

You can use basic config. If you configure the attribute filename , logs will be automatically saved to the file you specify. You can also configure the attribute filemode . Setting the value to w will overwrite the file after every entry.

Does logging in Python use log4j?

The inbuilt logging module in python requires some handful of lines of code to configure log4j-like features viz - file appender, file rotation based on both time & size. For one-liner implementation of the features in your code, you can use the package autopylogger .


1 Answers

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.

like image 188
Vinay Sajip Avatar answered Oct 10 '22 04:10

Vinay Sajip