Python log setup


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def setupLogger(self, log_dir):
  2. if not isdir(log_dir):
  3. os.mkdir(log_dir)
  4.  
  5. # create logger
  6. log_file = join(log_dir, time.strftime('%Y_%m_%d') + '.log')
  7. logger = logging.getLogger(log_file)
  8. logger.setLevel(logging.INFO)
  9.  
  10. # create console handler and set level to debug
  11. ch = logging.FileHandler(log_file)
  12. ch.setLevel(logging.INFO)
  13.  
  14. # create formatter
  15. formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
  16.  
  17. # add formatter to ch
  18. ch.setFormatter(formatter)
  19.  
  20. # add ch to logger
  21. logger.addHandler(ch)
  22.  
  23. return logger

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.