python logging日誌輸出個文件中

 1 # -*- coding:utf-8 -*-
 2 import logging  # 引入logging模塊
 3 import os.path
 4 import time
 5 # 第一步,建立一個logger
 6 logger = logging.getLogger()
 7 logger.setLevel(logging.INFO)  # Log等級總開關
 8 # 第二步,建立一個handler,用於寫入日誌文件
 9 rq = time.strftime(u'%Y%m%d%H%M', time.localtime(time.time()))
10 log_path = u'C:/Users/feixu_yan/Desktop/Logs'
11 log_name = log_path + rq + u'.log'
12 logfile = log_name
13 fh = logging.FileHandler(logfile, mode=u'w')
14 fh.setLevel(logging.DEBUG)  # 輸出到file的log等級的開關
15 # 第三步,定義handler的輸出格式
16 formatter = logging.Formatter(u"%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
17 fh.setFormatter(formatter)
18 # 第四步,將logger添加到handler裏面
19 logger.addHandler(fh)
20 # 日誌
21 logger.debug(u'this is a logger debug message')
22 logger.info(u'this is a logger info message')
23 logger.warning(u'this is a logger warning message')
24 logger.error(u'this is a logger error message')
25 logger.critical(u'this is a logger critical message')

 

 

具體詳見:http://www.javashuo.com/article/p-xhfakruo-c.html

相關文章
相關標籤/搜索