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 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.
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 .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With