• logging 日志


    logging模块

    Python中有一个模块logging,可以直接记录日志

    #   日志级别
    # CRITICAL 50
    # ERROR    40
    # WARNING  30
    # INFO     20
    # DEBUG    10
    

      

    logging.basicConfig()函数中的具体参数:
    filename:   指定的文件名创建FiledHandler,这样日志会被存储在指定的文件中;
    filemode:   文件打开方式,在指定了filename时使用这个参数,默认值为“w”还可指定为“a”;
    format:      指定handler使用的日志显示格式;
    datefmt:    指定日期时间格式。,格式参考strftime时间格式化(下文)
    level:        设置rootlogger的日志级别
    stream:     用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。
                      若同时列出了filename和stream两个参数,则stream参数会被忽略。
     
    format参数中可能用到的格式化信息:

    %(name)s

    Logger的名字

    %(levelno)s

    数字形式的日志级别

    %(levelname)s

    文本形式的日志级别

    %(pathname)s

    调用日志输出函数的模块的完整路径名,可能没有

    %(filename)s

    调用日志输出函数的模块的文件名

    %(module)s

    调用日志输出函数的模块名

    %(funcName)s

    调用日志输出函数的函数名

    %(lineno)d

    调用日志输出函数的语句所在的代码行

    %(created)f

    当前时间,用UNIX标准的表示时间的浮 点数表示

    %(relativeCreated)d

    输出日志信息时的,自Logger创建以 来的毫秒数

    %(asctime)s

    字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒

    %(thread)d

    线程ID。可能没有

    %(threadName)s

    线程名。可能没有

    %(process)d

    进程ID。可能没有

    %(message)s

    用户输出的消息

    二、logging模块测试

    1、打印日志到标准输出中

    import logging
    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')

    输出结果

    C:UsersAdministratorAppDataLocalProgramsPythonPython36python.exe D:/pyworkpeace/tupian.py 'https://www.tianyancha.com/login'
    WARNING:root:warning message
    
    Process finished with exit code 0

    可以看出默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志。默认的日志的格式为:

    日志级别:Logger名称:用户输出消息

    2、将日志文件输入到文件中

    import os
    logging.basicConfig(filename=os.path.join(os.getcwd(),'log.txt'),level=logging.DEBUG)
    logging.debug('this is a message')

    运行这三行代码后会在安装Python的目录中出现一个log.txt文件,文件内容

    DEBUG:root:this is a message
    DEBUG:root:debug message

    3、自定义格式,输出日志文件

    复制代码
    # -*-coding:utf-8-*-
    
    import logging
    
    
    def console_out(logFilename):
        ''''' Output log to file and console '''
        # Define a Handler and set a format which output to file
        logging.basicConfig(
            level=logging.DEBUG,  # 定义输出到文件的log级别,大于此级别的都被输出
            format='%(asctime)s  %(filename)s : %(levelname)s  %(message)s',  # 定义输出log的格式
            datefmt='%Y-%m-%d %A %H:%M:%S',  # 时间
            filename=logFilename,  # log文件名
            filemode='w')  # 写入模式“w”或“a”
        # Define a Handler and set a format which output to console
        console = logging.StreamHandler()  # 定义console handler
        console.setLevel(logging.INFO)  # 定义该handler级别
        formatter = logging.Formatter('%(asctime)s  %(filename)s : %(levelname)s  %(message)s')  # 定义该handler格式
        console.setFormatter(formatter)
        # Create an instance
        logging.getLogger().addHandler(console)  # 实例化添加handler
    
        # Print information              # 输出日志级别
        logging.debug('logger debug message')
        logging.info('logger info message')
        logging.warning('logger warning message')
        logging.error('logger error message')
        logging.critical('logger critical message')
    
    
    if __name__ == "__main__":
        console_out('logging.log')
    复制代码

    输出结果:

    此时也会自动生成一个日志文件,日志文件和运行文件在同一个文件夹中,文件名logging.log

    2017-10-23 Monday 11:37:59 hgghf : DEBUG logger debug message
    2017-10-23 Monday 11:37:59 hgghf : INFO logger info message
    2017-10-23 Monday 11:37:59 hgghf : WARNING logger warning message
    2017-10-23 Monday 11:37:59 hgghf : ERROR logger error message
    2017-10-23 Monday 11:37:59 hgghf : CRITICAL logger critical message

    修改输出路径:

    filename='/tmp/test1.log',  # log文件名
    当将脚本中这行代码换一下,那么我们输出日志的路径地址就换成了D: mp
    下面的方式同样可以达到上述结果

     4、自定义输出位置

    复制代码
    import logging  
    logging.basicConfig(level=logging.DEBUG,  
                        format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  
                        datefmt='%a, %d %b %Y %H:%M:%S',  
                        filename='/tmp/test.log',  
                        filemode='w')  
      
    logging.debug('debug message')  
    logging.info('info message')  
    logging.warning('warning message')  
    logging.error('error message')  
    logging.critical('critical message') 
    复制代码

    由于运行脚本放在D:pyworkpeace下,输出文件在D盘tmp文件夹下test.log,内容如下:

    Mon, 23 Oct 2017 15:00:05 tupian.py[line:11] DEBUG debug message
    Mon, 23 Oct 2017 15:00:05 tupian.py[line:12] INFO info message
    Mon, 23 Oct 2017 15:00:05 tupian.py[line:13] WARNING warning message
    Mon, 23 Oct 2017 15:00:05 tupian.py[line:14] ERROR error message
    Mon, 23 Oct 2017 15:00:05 tupian.py[line:15] CRITICAL critical message

    5.logging.basicConfig常用设置

    # 添加日志
    
    logging.basicConfig(
        level=logging.INFO,  # 定义输出到文件的log级别,大于此级别的都被输出
        format='%(asctime)s  %(filename)s  %(levelname)s : %(message)s',  # 定义输出log的格式
        datefmt='%Y-%m-%d %H:%M:%S',  # 时间
        filename='baidu.log',  # log文件名
        filemode='a', )  # 写入模式“w”或“a”
    
    
    
    # 输入日志信息
    import logging
    import time
    
    logging.basicConfig(
        level=logging.INFO,  # 定义输出到文件的log级别,大于此级别的都被输出
        format='%(asctime)s  %(filename)s  %(levelname)s : %(message)s',  # 定义输出log的格式
        datefmt='%Y-%m-%d %H:%M:%S',  # 时间
        filename="{}info.log".format(time.strftime('%Y%m%d')),  # log文件名
        filemode='a',  # 写入模式“w”或“a”
    )
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    # 设置格式
    formatter = logging.Formatter('%(asctime)s  %(filename)s  %(levelname)s : %(message)s')
    # 告诉handler使用这个格式
    console.setFormatter(formatter)
    # 为root logger添加handler
    logging.getLogger('').addHandler(console)

    6.logging.basicConfig中文输出乱码

    # 1.进入logging日志 __init__.py文件
    
    # 2.找到
    class FileHandler(StreamHandler):
        """
        A handler class which writes formatted logging records to disk files.
        """
        def __init__(self, filename, mode='a', encoding=None, delay=False):
            """
            Open the specified file and use it as the stream for logging.
            """
            # Issue #27493: add support for Path objects to be passed in
            filename = os.fspath(filename)
            #keep the absolute path, otherwise derived classes which use this
            #may come a cropper when the current directory changes
            self.baseFilename = os.path.abspath(filename)
            self.mode = mode
            self.encoding = encoding
            self.delay = delay
            if delay:
                #We don't open the stream, but we still need to call the
                #Handler constructor to set level, formatter, lock etc.
                Handler.__init__(self)
                self.stream = None
            else:
                StreamHandler.__init__(self, self._open())
    
    # 3.将encoding=None改成  encoding='utf-8'
    class FileHandler(StreamHandler):
        """
        A handler class which writes formatted logging records to disk files.
        """
        def __init__(self, filename, mode='a', encoding='utf-8', delay=False):
            """
            Open the specified file and use it as the stream for logging.
            """
            # Issue #27493: add support for Path objects to be passed in
            filename = os.fspath(filename)
            #keep the absolute path, otherwise derived classes which use this
            #may come a cropper when the current directory changes
            self.baseFilename = os.path.abspath(filename)
            self.mode = mode
            self.encoding = encoding
            self.delay = delay
            if delay:
                #We don't open the stream, but we still need to call the
                #Handler constructor to set level, formatter, lock etc.
                Handler.__init__(self)
                self.stream = None
            else:
                StreamHandler.__init__(self, self._open())
    

      

  • 相关阅读:
    spring scope 属性的取值
    DefaultTransactionStatus源码
    Spring事务管理接口PlatformTransactionManager的实现类DataSourceTransactionManager
    Spring 框架简介
    PL/SQL游标
    [BC冠军赛(online)]小结
    [hdu5164]ac自动机
    [hdu2222]ac自动机(模板)
    上浮法或漂浮法
    [hdu5213]容斥原理+莫队算法
  • 原文地址:https://www.cnblogs.com/yoyo1216/p/10119800.html
Copyright © 2020-2023  润新知