Overwriting a Python Logging File Instead of Appending

Python

To overwrite the log file each time rather than the default append then pass filemode='w' parameter to the basicConfig function.

 1|  logging.basicConfig(format='%(asctime)s[%(levelname)s] %(funcName)s: %(message)s',
 2|                          datefmt='%d/%m/%Y %I:%M:%S %p',
 3|                          filename='log_file.log',
 4|                          level=logging.INFO,
 5|                          filemode='w')
 6|  
 7|  LOG = logging.getLogger('feature_engineering')
 8|  
 9|  try:
10|  	feature_eng()
11|  except Exception as e:
12|          LOG.exception(e)
Did you find this snippet useful?

Sign up for free to to add this to your code library