学一门新技术或者新语言,我们都要首先学会如何去适应这们新技术,其中在适应过程中,我们必须得学习如何调试程序并打出相应的log信息来,正所谓“只要log打的好,没有bug解不了@PhantomAssassin2011”,在我们熟知的一些信息技术中,log4xxx系列以及开发Android app时的android.util.Log包等等都是为了开发者更好的得到log信息服务的。在Python这门语言中,我们同样可以根据自己的程序需要打出log。

        log信息不同于使用打桩法打印一定的标记信息,log可以根据程序需要而分出不同的log级别,比如info、debug、warn等等级别的信息,只要实时控制log级别开关就可以为开发人员提供更好的log信息,与log4xx类似,logger,handler和日志消息的调用可以有具体的日志级别(Level),只有在日志消息的级别大于logger和handler的设定的级别,才会显示。下面我就来谈谈我在Python中使用的logging模块一些方法。


logging模块介绍


         Python的logging模块提供了通用的日志系统,熟练使用longgong模块可以方便开发者开发第三方模块或者是自己的Python应用。同样这个模块提供不同的日志级别,并可以采用不同的方式记录日志,比如文件,HTTP、GET/POST,SMTP,Socket等,甚至可以自己实现具体的日志记录方式。下文我将主要介绍如何使用文件方式记录log。

        logging模块包括logger,handler,filter,formatter这四个基本概念。

        logger:提供日志接口,供应用代码使用。logger最长用的操作有两类:配置和发送日志消息。可以通过logging.getLogger(name)获取logger对象,如果不指定name则返回root对象,多次使用相同的name调用getLogger方法返回同一个logger对象。

        handler:将日志记录(log record)发送到合适的目的地(destination),比如文件,socket等。一个logger对象可以通过addHandler方法添加0到多个handler,每个handler又可以定义不同日志级别,以实现日志分级过滤显示。

        filter:提供一种优雅的方式决定一个日志记录是否发送到handler。

        formatter:指定日志记录输出的具体格式。formatter的构造方法需要两个参数:消息的格式字符串和日期字符串,这两个参数都是可选的。


基本方法


basicConfig函数即可,代码如下:


Python代码


1. '''''
2. Created on 2012-8-12
3.  
4. @author: walfred
5. @module: loggingmodule.BasicLogger
6. '''  
7. import logging  
8.   
9. log_file = "./basic_logger.log"  
10.   
11. logging.basicConfig(filename = log_file, level = logging.DEBUG)  
12.   
13. logging.debug("this is a debugmsg!")  
14. logging.info("this is a infomsg!")  
15. logging.warn("this is a warn msg!")  
16. logging.error("this is a error msg!")  
17. logging.critical("this is a critical msg!")

 


basic_logger.log文件,查看basic_logger.log内容如下:

INFO:root:this is a info msg!
DEBUG:root:this is a debug msg!
WARNING:root:this is a warn msg!
ERROR:root:this is a error msg!
CRITICAL:root:this is a critical msg!

level设定为DEBUG级别,所以log日志中只显示了包含该级别及该级别以上的log信息。信息级别依次是:notset、debug、info、warn、error、critical。如果在多个模块中使用这个配置的话,只需在主模块中配置即可,其他模块会有相同的使用效果。


较高的设置


        上述的基础配置比较简单,没有显示出logging模块的厉害,适合小程序用,现在我介绍一个较高版本的代码,我们需要依次设置logger、handler、formatter等配置。


Python代码

1. '''''
2. Created on 2012-8-12
3.  
4. @author: walfred
5. @module: loggingmodule.NomalLogger
6. '''  
7. import logging  
8.   
9. log_file = "./nomal_logger.log"  
10. log_level = logging.DEBUG  
11.   
12. logger = logging.getLogger("loggingmodule.NomalLogger")  
13. handler = logging.FileHandler(log_file)  
14. formatter = logging.Formatter("[%(levelname)s][%(funcName)s][%(asctime)s]%(message)s")   
15.   
16. handler.setFormatter(formatter)  
17. logger.addHandler(handler)  
18. logger.setLevel(log_level)  
19.   
20. #test  
21. logger.debug("this is a debug msg!")  
22. logger.info("this is a info msg!")  
23. logger.warn("this is a warn msg!")  
24. logger.error("this is a error msg!")  
25. logger.critical("this is a critical msg!")


nomal_logger.log日志文件,如下:

[DEBUG][<module>][2012-08-12 17:43:59,295]this is a debug msg!
 [INFO][<module>][2012-08-12 17:43:59,295]this is a info msg!
 [WARNING][<module>][2012-08-12 17:43:59,295]this is a warn msg!
 [ERROR][<module>][2012-08-12 17:43:59,295]this is a error msg!
 [CRITICAL][<module>][2012-08-12 17:43:59,295]this is a critical msg!

       这个对照前面介绍的logging模块,不难理解,下面的最终版本将会更加完整。


最终版本


        这个最终版本我们用singleton设计模式来写一个|Logger类,代码如下:


Python代码


1. '''''
2. Created on 2012-8-12
3.  
4. @author: walfred
5. @module: loggingmodule.FinalLogger
6. '''  
7.   
8. import logging.handlers  
9.   
10. class FinalLogger:  
11.       
12. None  
13.       
14. "n" : logging.NOTSET,  
15. "d" : logging.DEBUG,  
16. "i" : logging.INFO,  
17. "w" : logging.WARN,  
18. "e" : logging.ERROR,  
19. "c" : logging.CRITICAL}  
20.       
21. "d"  
22. "final_logger.log"  
23. 10 * 1024 * 1024;  
24. 5  
25.     
26.     @staticmethod  
27. def getLogger():  
28. if FinalLogger.logger is not None:  
29. return FinalLogger.logger  
30.           
31. "oggingmodule.FinalLogger")  
32.         log_handler = logging.handlers.RotatingFileHandler(filename = FinalLogger.log_file,\  
33.                                                            maxBytes = FinalLogger.log_max_byte,\  
34.                                                            backupCount = FinalLogger.log_backup_count)  
35. "[%(levelname)s][%(funcName)s][%(asctime)s]%(message)s")  
36.         log_handler.setFormatter(log_fmt)  
37.         FinalLogger.logger.addHandler(log_handler)  
38.         FinalLogger.logger.setLevel(FinalLogger.levels.get(FinalLogger.log_level))  
39. return FinalLogger.logger  
40.       
41. if __name__ == "__main__":  
42.     logger = FinalLogger.getLogger()  
43. "this is a debug msg!")  
44. "this is a info msg!")  
45. "this is a warn msg!")  
46. "this is a error msg!")  
47. "this is a critical msg!")


final_logger.log内容如下:

[DEBUG][<module>][2012-08-12 18:12:23,029]this is a debug msg!
 [INFO][<module>][2012-08-12 18:12:23,029]this is a info msg!
 [WARNING][<module>][2012-08-12 18:12:23,029]this is a warn msg!
 [ERROR][<module>][2012-08-12 18:12:23,029]this is a error msg!
 [CRITICAL][<module>][2012-08-12 18:12:23,029]this is a critical msg!

        这个final版本,也是我一直用的,读者朋友也可以再加上其他的一些Handler,比如StreamHandler等等来获取更多的log信息,当然也可以将你的log信息通过配置文件来完成。