python爬虫学习之日志记录模块

这次的代码就是一个日志记录模块,代码很容易懂,注释很详细,也不需要安装什么库。提供的功能是日志可以显示在屏幕上并且保存在日志文件中。
调用的方式也很简单,测试代码里面有。

源代码:

#encoding=utf-8

import logging
import getpass
import sys


    # 定义MyLog类
class MyLog(object):
    # 类MyLog的构造函数
    def __init__(self):
        self.user = getpass.getuser()
        self.logger = logging.getLogger(self.user)
        self.logger.setLevel(logging.DEBUG)
        # 日志文件名
        self.logFile = sys.argv[0][0:-3] + '.log'   #print(sys.argv[0])   代表文件名 输出 mylog.py
        self.formatter = logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s\r\n')

        # 日志显示到屏幕上并输出到日志文件内
        # 输出到日志文件
        self.logHand = logging.FileHandler(self.logFile, encoding='utf8')
        self.logHand.setFormatter(self.formatter)
        self.logHand.setLevel(logging.DEBUG)

        # 输出到屏幕
        self.logHandSt = logging.StreamHandler()
        self.logHandSt.setFormatter(self.formatter)
        self.logHandSt.setLevel(logging.DEBUG)

        # 添加两个Handler
        self.logger.addHandler(self.logHand)
        self.logger.addHandler(self.logHandSt)

    # 日志的5个级别对应以下的5个函数
    def debug(self,msg):
        self.logger.debug(msg)

    def info(self,msg):
        self.logger.info(msg)

    def warning(self,msg):
        self.logger.warning(msg)

    def error(self,msg):
        self.logger.error(msg)

    def critical(self,msg):
        self.logger.critical(msg)

if __name__ == '__main__':
    mylog = MyLog()
    mylog.debug(u"I'm debug 测试中文")
    mylog.info("I'm info")
    mylog.warning("I'm warn")
    mylog.error(u"I'm error 测试中文")
    mylog.critical("I'm critical")

测试结果:

  

  

猜你喜欢

转载自www.cnblogs.com/xiaoyh/p/10146016.html