Python 之迴圈日誌
參考:
http://docs.python.org/2/library/logging.html
RotatingFileHandler 用於處理迴圈日誌, 可以指定記錄檔層級,記錄檔最大容量(位元組),記錄檔
#!/usr/bin/python# filename: printlog.py# author: # date: 2012-12-19# version: 0.1import loggingfrom logging.handlers import RotatingFileHandler## log settingsLOG_PATH_FILE = "./print.log"LOG_MODE = 'a'LOG_MAX_SIZE = 2*1024*1024 # 2MLOG_MAX_FILES = 4 # 4 Files: print.log.1, print.log.2, print.log.3, print.log.4LOG_LEVEL = logging.DEBUGLOG_FORMAT = "%(asctime)s %(levelname)-10s[%(filename)s:%(lineno)d(%(funcName)s)] %(message)s"handler = RotatingFileHandler(LOG_PATH_FILE, LOG_MODE, LOG_MAX_SIZE, LOG_MAX_FILES)formatter = logging.Formatter(LOG_FORMAT)handler.setFormatter(formatter)Logger = logging.getLogger()Logger.setLevel(LOG_LEVEL)Logger.addHandler(handler)i=0while i < 10000: i = i+1 Logger.log(logging.DEBUG, "Logs a message with integer level lvl on this logger.") Logger.info("Logs a message with level INFO on this logger.") Logger.warning("Logs a message with level WARNING on this logger.") Logger.error("Logs a message with level ERROR on this logger.") Logger.critical("Logs a message with level CRITICAL on this logger.")# Logger.exception("This method should only be called from an exception handler.")