Set Python Logging to overwrite log file when using dictConfig? -


i'm using dictconfig() configure logging. want process overwrite specified log file every time process run. how do this?

i see filemode setting in basicconfig() can't figure out put in dictconfig() configuration.

i've tried unexpected keyword argument 'filemode' error. i've tried in few other places too. python logging docs confusing 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 @vinay sajip selected answer below. here updated configuration overwrites specified log file every time process run. 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 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 use mode rather filemode. in general, need use argument names specified in documentation handler initialisation - see here filehandler.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -