Registro: registra el proceso de ejecución, el flujo de ejecución lógica
error registrado
Defina un recopilador de registros: recopile los registros correspondientes del código según sea necesario y envíelos al canal
Nivel de registro (Nivel):
depuración—— información—— advertencia—— error—— depuración crítica (FATA)
advertencia básica error informe de error grave
Clasificación del nivel de registro: crítico>error>advertencia>información>depuración
Formato de visualización del registro (Formatter):
hora, nivel de registro, archivo de código, línea: información
Canal de salida del registro (Handle):
archivo (FileHandle), consola (StreamHandle)
logging模块:有一个默认的日志收集器,root。
a、收集的是warning及warning以上级别的日志。
b、日志格式: 日志级别:收集器的名字:输出的内容
c、输出渠道: 控制台
paquete de registro de importación
import logging
módulo de registro: tiene un recopilador de registros predeterminado, root.
a. Se recopilan los registros en el nivel de advertencia y superiores.
b. Formato de registro: Nivel de registro: Nombre del recopilador: Contenido de salida
c. Canal de salida: Consola
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")