Getting Started with Python's built-in module --logging module

Getting Started with Python's built-in module --logging module

1, logging - log

(1) log function:

<1> user information recorded

<2> records Personal water

Operating state <3> recording software

<4> recording instruction issued by the programmer

<5> programmers for debugging code

(2) Functional simple configuration log:

logging.debug () # debug debugging 10

logging.info () # info information 20

logging.warning() # warning 警告 30

logging.error () # error Error 40

logging.critical() # critical 危险 50

(3) information to be recorded in the log

Python logging module logs the default print to standard output, and only shows a greater than or equal WARNING level logging, logging level indicating that the default to WARNING

(Log level grade CRITICAL> ERROR> WARNING> INFO> DEBUG),

The default log format for the log level: Logger Name: User output messages.

From the beginning of recorded default warning

logging.basicConfig (level = 30) # adjust the default start level 30

(4) manual transmission

import logging
logging.basicConfig(
     level=logging.DEBUG,
     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                     datefmt='%Y-%m-%d %H:%M:%S',
                     filename="test.log",
                     filemode="a",
 )


 logging.debug("你是疯儿,我是傻") # debug 调试
 logging.info("疯疯癫癫去我家")   # info 信息
 logging.warning("缠缠绵绵到天涯")   # info 警告
 logging.error("我下不床")           # error 错误
 logging.critical("你回不了家")        # critical 危险

<1> basicConfig () function can be changed by the logging module specific parameters default behavior parameters are available:

  • filename: Creating FiledHandler, such logs are stored in the specified file with the specified file name.
  • filemode: File Open, this parameter is specified in the filename, the default value "a" may also be designated as "w".
  • format: log handler specified display format.
  • datefmt: specify the date and time format.
  • Set logging levels: level
  • stream: Creating StreamHandler with the specified stream. You can specify output to
  • sys.stderr, sys.stdout or file (f = open ( 'test.log', 'w')), default sys.stderr. If both lists the filename and stream two parameters, the stream parameter is ignored.

<2> format parameter string format may be used :

  • % (Name) s Logger name
  • % (Levelno) s log level digital form
  • % (Levelname) s log level text form
  • % (Pathname) s call to the full path name of the log output function module may not
  • % (Filename) s call log output function module filename
  • % (Module) s call log output function module name
  • Function names% (funcName) s call log output function
  • OK code statements% (lineno) d log output function call where
  • % (Created) f the current time, represented by a standard floating point representation UNIX Time
  • Since the number of milliseconds Logger created when the d output log information% (relativeCreated)
  • % (Asctime) of the current time string s. The default format is "2003-07-0816: 49: 45,896." Milliseconds after the comma
  • % (Thread) d thread ID. Maybe not
  • % (ThreadName) s thread name. Maybe not
  • % (Process) d process ID. Maybe not
  • Message% (message) s user output

(5) Automatic

import logging
# 初始化一个空日志
logger = logging.getLogger()   # -- 创建了一个对象
# 创建一个文件,用于记录日志信息
fh = logging.FileHandler('test.log',encoding='utf-8')
# 创建一个文件,用于记录日志信息
fh1 = logging.FileHandler('test1.log',encoding='utf-8')
# 创建一个可以在屏幕输出的东西
ch = logging.StreamHandler()
# 对要记录的信息定义格式
msg = logging.Formatter('%(asctime)s - [line:%(lineno)d] - %(filename)s - %(levelname)s - %(message)s')
# 对要记录的信息定义格式
msg1 = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# 设置记录等级
logger.setLevel(10) or logger.setLevel(logging.DEBUG)
# 等级对应表
'''
DEBUG - 10
INFO - 20
WARNING - 30
ERROR - 40
CRITICAL - 50
'''
# 将咱们设置好的格式绑定到文件上
fh.setFormatter(msg)
fh1.setFormatter(msg)
# 将咱们设置好的格式绑定到屏幕上
ch.setFormatter(msg1)
# 将设置存储日志信息的文件绑定到logger日志上
logger.addHandler(fh) #logger对象可以添加多个fh和ch对象
logger.addHandler(fh1)
logger.addHandler(ch)
# 记录日志
logger.debug([1,2,3,4,])
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')

Guess you like

Origin www.cnblogs.com/caiyongliang/p/11546748.html