random

- collection of un-sorted bollocks
git clone git://git.acid.vegas/random.git
Log | Files | Refs | Archive

logga.py (1105B)

      1 #!/usr/bin/env python
      2 # logging example - developed by acidvegas in python (https://acid.vegas/random)
      3 import logging
      4 import logging.handlers
      5 import os
      6 
      7 log_file=True # Set to False for console logging only
      8 
      9 # Set up logging
     10 def setup_logger():
     11 	sh = logging.StreamHandler()
     12 	sh.setFormatter(logging.Formatter('%(asctime)s | %(levelname)9s | %(message)s', '%I:%M %p'))
     13 	if log_file:
     14 		if not os.path.exists('logs'):
     15 			os.makedirs('logs')
     16 		fh = logging.handlers.RotatingFileHandler('logs/debug.log', maxBytes=250000, backupCount=7, encoding='utf-8')
     17 		fh.setFormatter(logging.Formatter('%(asctime)s | %(levelname)9s | %(filename)s.%(funcName)s.%(lineno)d | %(message)s', '%Y-%m-%d %I:%M %p'))
     18 		logging.basicConfig(level=logging.NOTSET, handlers=(sh,fh))
     19 		del fh
     20 	else:
     21 		logging.basicConfig(level=logging.NOTSET, handlers=(sh,))
     22 	del sh
     23 
     24 # Logging examples
     25 setup_logger()
     26 logging.debug('This message should go to the log file')
     27 logging.info('So should this')
     28 logging.critical('ok')
     29 logging.warning('And this, too')
     30 logging.error('And non-ASCII stuff, too, like Øresund and Malmö')
     31 logging.shutdown()