Posted By

silviud on 09/08/10


Tagged

python logger


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

eglyph


Python log setup


 / Published in: Python
 

  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  

You need to login to post a comment.