python日志的使用

1、日志设置

 1 import logging 
 2 
 3 global logger  # 设置全局变量,涉及动态修改
 4 g_logDir = "D:/checkerPath/logs/"
 5 
 6 def tearUp():  
 7    # 创建一个logger
 8     global root_logger
 9     global fh
10     global ch
11     
12     root_logger = logging.getLogger("clkLogger")
13     root_logger.setLevel(logging.DEBUG)
14     
15     # 创建一个handler,用于写入日志文件
16     fh = logging.FileHandler(logDir)
17     fh.setLevel(logging.DEBUG)
18     
19     # 再创建一个handler,用于输出到控制台
20     ch = logging.StreamHandler()
21     ch.setLevel(logging.DEBUG)
22     
23     # 定义handler的输出格式
24     formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(pathname)s - %(lineno)s - %(message)s')
25     fh.setFormatter(formatter)
26     ch.setFormatter(formatter)
27     
28     # 给logger添加handler
29     root_logger.addHandler(fh)
30     root_logger.addHandler(ch)    
31     logger = root_logger; # 修改全局日志变量
32     
33     logger.info(explain(configItem[1]))
34 
35 
36 def tear_down():    
37     logger.debug("close the log handler")
38     root_logger.removeHandler(fh)
39     root_logger.removeHandler(ch)
40     logger.removeHandler(fh)
41     logger.removeHandler(ch)

使用方法

import logging

logger = logging.getLogger("clkLogger.libClk")

logger.debug("获取的参数信息为:"+ str(g_clock_sourceA["5"]))

 

posted @ 2019-06-06 11:37  Vtianhu  阅读(389)  评论(0编辑  收藏  举报