python 日誌輸出模塊--兩種方法
阿新 • • 發佈:2018-03-25
bug nco format file dha fig bytes 兩種 lena
第一種方法:(推薦)
import logging.handlers LOG_FILE = r‘tst.log‘ handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1024 * 1024, backupCount=5, encoding=‘utf-8‘) # 實例化handler fmt = ‘%(asctime)s - %(levelname)s - %(message)s‘ formatter = logging.Formatter(fmt) # 實例化formatter handler.setFormatter(formatter) # 為handler添加formatter logger = logging.getLogger(‘tst‘) # 獲取名為tst的logger logger.addHandler(handler) # 為logger添加handler logger.setLevel(logging.DEBUG) logger.info(u‘輸出中文試一試‘) logger.debug(‘first debug message‘)
第二種方法:
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s" DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p" logging.basicConfig(filename=‘my.log‘, level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT) logging.debug("This is a debug log.") logging.info("This is a info log.") logging.warning("This is a warning log.") logging.error("This is a error log.") logging.critical("This is a critical log.")
python 日誌輸出模塊--兩種方法