[Python3] 042 log

LOG

  • logging module logs module level function
  • It includes four components

1. Log concepts

1.1 log level level

  1. DEBUG
  2. INFO
  3. NOTICE
  4. WARNING
  5. ERROR
  6. CRITICAL
  7. ALERT
  8. EMERGENCY
  • Users can program the information they need attention

1.2 LOG role of

  • debugging
  • Understand the operation of the software
  • Analysis of Orientation

1.3 log

  • time
  • level
  • location
  • content

1.4 mature third-party log

  • logging
  • Log4j
  • log4php

1.5 Note

  • Do not operate frequently IO

2. Logging Module

2.1 log level

  • DEBUG
  • INFO
  • WARNING
  • ERROR
  • CRITICAL

2.2 Use

  1. Direct use of logging, logging because the other components of the package

  2. Four custom components directly with loging

2.3 Notes

  • Initialize or write log instances need to specify the level
  • Level can be customized
  • Only when the level is equal to or higher than the specified level was only recorded

2.4 logging module level log

2.4.1 logging of several functions

function DEFINITIONS
logging.debug(msg, *args, **kwargs) Create a severity level DEBUG logging
logging.info(msg, *args, **kwargs) Create a severity level of INFO logging
logging.warning(msg, *args, **kwargs) Create a severity level WARNING logging
logging.error(msg, *args, **kwargs) Create a severity level ERROR logging
logging.critical(msg, *args, **kwargs) Create a severity level CRITICAL logging
logging.log(level, *args, **kwargs) Create a severity level for the level of logging
logging.basicConfig(**kwargs) To root logger a one-time configuration
  • logging.basicConfig(**kwargs)
    • Only when the first call to act
    • Do not configure the default value logger
      • Output: sys.stderr
      • Level: WARNING
      • Format: level: log_name: content

2.4 Example 2

  • format parameters
parameter usage DEFINITIONS
asctime %(asctime)s Time log of events - human-readable time
such as: 2019-12-2417: 00: 12,765
created %(created)f Time log of events - the time stamp
and calling time.time value returned by the function () the same
relativeCreated %(relativeCreated)d Log event time with respect to
the relative number of milliseconds logging module load time
msecs %(msecs)d Log event millisecond section of the event
levelname %(levelname)s Log level text form of the log record
'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'
levelno %(levelno)s The digital form of the log record log level (10, 20, 30, 40, 50)
name %(name)s Log name used, the default is the 'root'
as the default rootLogger
message %(message)s Text logging, calculation obtained by msg% args
pathname %(pathname)s The full path to the source file is called logging function
filename %(filename)s Pathname of the file name, including file suffix
module %(module)s filename part of the name, does not contain a suffix
lineno %(lineno)d The line number to call log function of the source code resides
funcName %(funcName)s Call logging function name function
process %(process)d Process ID
processName %(processName)s Process name, Python 3.1 new
thread %(thread)d Thread ID
threadName %(thread)s Thread name

2.5 logging process flow module

  • Four components
Package effect
Logger Logger Generating a log of the interface
Processor Handler Sends the generated log to the corresponding destination
Filters Filter Those finer control log output
Formatter Formatter Formatting the output information

2.5.1 Logger

  • Produce a log
function DEFINITIONS
Logger.setLevel () Minimum level of severity set logger will be processed log messages
Logger.addHandler () 和 Logger.removeHandler () Logger objects for adding and removing a handler objects
Logger.addFilter () 和 Logger.removeFilter () Logger objects for adding and removing a filter objects
Logger.debug 产生一条 debug 级别的日志
info, error 等同理
Logger.exception() 创建类似于 Logger.error 的日志消息
Logger.log() 获取一个明确的日志 level 参数类创建一个日志记录
  • 如何得到一个 logger 对象
  1. 实例化
  2. logging.getLogger()

2.5.2 Handler

  • 把 log 发送到制定位置
  • 方法
    • setLevel
    • setFormat
    • addFilter, removeFilter
  • 不需要直接使用,Handler 是基类
方法 释义
logging.StreamHandler 将日志消息发送到输出到 Stream
如 std.out, std.err 或任何 file-like 对象
logging.FileHandler 将日志消息发送到磁盘文件
默认情况下文件大小会无限增长
logging.handlers.RotatingFileHandler 将日志消息发送到磁盘文件,并支持日志文件按大小切割
logging.hanlders.TimedRotatingFileHandler 将日志消息发送到磁盘文件,并支持日志文件按时间切割
logging.handlers.HTTPHandler 将日志消息以 GET 或 POST 的方式发送给一个 HTTP 服务器
logging.handlers.SMTPHandler 将日志消息发送给一个指定的 email 地址
logging.NullHandler 该 Handler 实例会忽略 error messages
通常被想使用 logging 的 library 开发者使用来避免
'No handlers could be found for logger XXX' 信息的出现

2.5.3 Filter 类

  • 可以被 Handler 和 Logger 使用
  • 控制传递过来的信息的具体内容

2.5.4 Format 类

  • 直接实例化
  • 可以继承 Format 添加特殊内容
  • 三个参数
参数 释义
fmt 指定消息格式化字符串,如果不指定该参数则默认使用 message 的原始值
datefmt 指定日期格式字符串,如果不指定该参数则默认使用 "%Y-%m-%d %H:%M:%S"
style Python 3.2 时新增的参数,可取值为 '%', '{' 和 '$',如果不指定该参数则默认使用 '%'

Guess you like

Origin www.cnblogs.com/yorkyu/p/12093785.html