【python学习】python日志的使用操作-26

日志:记录了执行过程,逻辑执行流程

           记录了报错

定义日志收集器:要从代码当中按照要求 收集对应的日志,并输出到渠道当中

日志级别(Level):    
         debug—— info—— warning —— error—— critical(FATA)
          调试            基本          警告             报错             严重错误

日志级别排序:critical>error>warning>info>debug


日志显示的格式(Formatter):
        时间、日志级别、代码文件、第几行:信息
日志输出渠道(Handle):
        文件(FileHandle)、控制台(StreamHandle) 

logging模块:有一个默认的日志收集器,root。
    a、收集的是warning及warning以上级别的日志。
    b、日志格式: 日志级别:收集器的名字:输出的内容
    c、输出渠道: 控制台

导入logging包

import logging

logging模块:有一个默认的日志收集器,root。
    a、收集的是warning及warning以上级别的日志。
    b、日志格式: 日志级别:收集器的名字:输出的内容
    c、输出渠道: 控制台

import logging  

logging.debug("当前是debug")
logging.info("当前是info")
logging.warning("当前是warning")
logging.error("当前是error")
logging.critical("当前是critical")


------------------------打印结果------------------------

WARNING:root:当前是warning
ERROR:root:当前是error
CRITICAL:root:当前是critical

自定义日志收集:
1、调用logger = logging.getLogger(日志名字)来生成一个日志收集器对象
2、设置你的日志收集级别。logger.setLevel(日志级别),一般为INFO
3、使用logging.Formatter类来定制要输出到控制台/文件当中的日志格式
4、使用handle1 = logging.StreamHandle()来创建一个控制台渠道对象,
   并将控制台要输出的日志格式设置为3当中的formatter. 设置:handle1.setformatter(Formatter对象)
5、将4当中的handle1添加到logger当中,那么日志就可以输出到控制台。
6、使用handle2 = logging.FileHandle(日志文件路径)来创建一个控制台渠道对象,
   并将控制台要输出的日志格式设置为3当中的formatter. 设置:handle2.setformatter(Formatter对象)
7、将6当中的handle2添加到logger当中,那么日志就可以输出到文件当中。
6、使用handle3 = logging.FileHandle(日志文件路径)来创建一个控制台渠道对象,
   并将控制台要输出的日志格式设置为3当中的formatter. 设置:handle3.setformatter(Formatter对象)   
   指定handle3的日志级别为ERROR
   handle3.setLevel(logging.ERROR)   
7、将6当中的handle3添加到logger当中,那么日志就可以输出到文件当中。
import logging

#创建一个日志收集器
logger=logging.getLogger("tetslog")

#自定义级别、自定义日志格式、自定义日志格式、自定义输出渠道
#logging.{} 设置 DEBUG、INFO、WARNING、ERROR、CRITICAL
logger.setLevel(logging.DEBUG)

#自定义日志格式(Formatter)
fmt_test=" %(name)s %(levelname)s %(filename)s %(lineno)d %(message)s"
"""
    %(name)s            Name of the logger (logging channel)
    %(levelno)s         Numeric logging level for the message (DEBUG, INFO,
                        WARNING, ERROR, CRITICAL)
    %(levelname)s       Text logging level for the message ("DEBUG", "INFO",
                        "WARNING", "ERROR", "CRITICAL")
    %(pathname)s        Full pathname of the source file where the logging
                        call was issued (if available)
    %(filename)s        Filename portion of pathname
    %(module)s          Module (name portion of filename)
    %(lineno)d          Source line number where the logging call was issued
                        (if available)
    %(funcName)s        Function name
    %(created)f         Time when the LogRecord was created (time.time()
                        return value)
    %(asctime)s         Textual time when the LogRecord was created
    %(msecs)d           Millisecond portion of the creation time
    %(relativeCreated)d Time in milliseconds when the LogRecord was created,
                        relative to the time the logging module was loaded
                        (typically at application startup time)
    %(thread)d          Thread ID (if available)
    %(threadName)s      Thread name (if available)
    %(process)d         Process ID (if available)
    %(message)s         The result of record.getMessage(), computed just as
                        the record is emitted
"""
#实例化一个日志格式类
formatter=logging.Formatter(fmt_test)

#实例化渠道(Handle),设置渠道当中的日志显示格式
#控制台(StreamHandler)
handle=logging.StreamHandler()

#设置渠道当中的日志显示格式
handle.setFormatter(formatter)

#将渠道与日志收集器绑定起来
logger.addHandler(handle)


#日志情况
logger.debug("当前是debug")
logger.info("当前是info")
logger.warning("当前是warning")
logger.error("当前是error")
logger.critical("当前是critical")


--------------------------打印结果--------------------------

 tetslog DEBUG 日志.py 61 当前是debug
 tetslog INFO 日志.py 62 当前是info
 tetslog WARNING 日志.py 63 当前是warning
 tetslog ERROR 日志.py 64 当前是error
 tetslog CRITICAL 日志.py 65 当前是critical
import logging

#创建一个日志收集器
logger=logging.getLogger("tetslog")

#自定义级别、自定义日志格式、自定义日志格式、自定义输出渠道
#logging.{} 设置 DEBUG、INFO、WARNING、ERROR、CRITICAL
logger.setLevel(logging.DEBUG)

#自定义日志格式(Formatter)
fmt_test=" %(name)s %(levelname)s %(filename)s %(lineno)d %(message)s"

#实例化一个日志格式类
formatter=logging.Formatter(fmt_test)

#实例化渠道(Handle),设置渠道当中的日志显示格式
#控制台(StreamHandler)
handle=logging.StreamHandler()

#设置渠道当中的日志显示格式
handle.setFormatter(formatter)

#将渠道与日志收集器绑定起来
logger.addHandler(handle)

#文件渠道FileHandler,输出在本地目录中,文件名称logtest.log
dd=logging.FileHandler("logtest.log",encoding="utf-8")

#设置渠道当中的日志显示格式
dd.setFormatter(formatter)

#将渠道与日志收集器绑定起来
logger.addHandler(dd)

#日志情况
logger.debug("当前是debug")
logger.info("当前是info")
logger.warning("当前是warning")
logger.error("当前是error")
logger.critical("当前是critical")

猜你喜欢

转载自blog.csdn.net/admins_/article/details/121291264
今日推荐