Python——logging模块(日志模块)

2021-01-21 21:17

阅读:426

标签:nbsp   xtu   microsoft   relative   名称   级别   and   报错信息   asc   

按照格式形成相应的日志条目,写入到制定的目录中去。

特点:

  • 配置一次,后续调用即可,(放到项目目录中的conf目录)
  • 设置level级别时,要和最小写入级别相等。触发告警只能高或持平。

源代码书写方法:

import logging

file_handler = logging.FileHandler(‘a.txt‘,‘a‘,encoding=‘utf-8‘)   #日志存放路径
fmtl = logging.Formatter(fmt="%(asctime)s-%(name)s-%(levelname)s")  #存放格式
file_handler.setFormatter(fmtl)  #进行关联

logger = logging.Logger(‘日志对象名称,对应的name值‘,level=logging.ERROR) #设置日志接收等级

logger.addHandler(file_handler)  #logger与file_handler对应
logger.error(‘错误信息‘)

源码:报错信息等级:

CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0

源码:可以使用的fmt信息:

    %(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 loggingcall 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)  #以time.time形式出现
    %(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)      #线程ID
    %(threadName)s      Thread name (if available)    #线程名称
    %(process)d         Process ID (if available)      #内存ID
    %(message)s         The result of record.getMessage(), computed just asthe record is emitted  #接受的信息

常用版本:

import logging

file_handler = logging.FileHandler(filename=‘a.txt‘,mode=‘a‘,encoding=‘utf-8‘)
logging.basicConfig(
        format = "————————%(asctime)s-%(levelname)s————————",  #指定格式记录,下面指定了全部保存信息,所以这里不用将很多信息都收集。
        datefmt= ‘%Y-%m-%d %H-%M-%S‘,   #指定日期的格式,将毫秒去掉
        handlers=[file_handler,],
        level= logging.ERROR)   #设置接受的报错等级。

try:
    int(‘abc‘)
except Exception as e:
    logging.error(e,exc_info = True)    #这里的exc_info指保存全部保存信息,而不只是保存部分。

 

日志分割:

每隔一段时间生成一个日志文件,而不会是全部都在一个日志文件中。

import logging
from logging import handlers
file_handler = handlers.TimedRotatingFileHandler(filename=‘a.txt‘,when=‘h‘,interval=1,encoding=‘utf-8‘)  #将报错文件每1小时创建一个新的文件。格式是:a.txt时间
logging.basicConfig(
        format = "————————%(asctime)s-%(levelname)s————————",
        datefmt= ‘%Y-%m-%d %H-%M-%S‘,
        handlers=[file_handler,],
        level= logging.ERROR)

时间间隔书写源码:

S - Seconds
M - Minutes
H - Hours
D - Days
midnight - roll over at midnight
W{0-6} - roll over on a certain day; 0 - Monday

  

Python——logging模块(日志模块)

标签:nbsp   xtu   microsoft   relative   名称   级别   and   报错信息   asc   

原文地址:https://www.cnblogs.com/xinbing/p/12893712.html


评论


亲,登录后才可以留言!