参考链接:
https://www.cnblogs.com/CJOKER/p/8295272.html
https://www.cnblogs.com/deeper/p/7404190.html
https://www.jianshu.com/p/feb86c06c4f4
https://zhuanlan.zhihu.com/p/42638567(推荐看这个知乎的。)
参考书籍:Python3标准库,Python编程快速上手。
首先介绍一下,为什么要用logging,根据Python编程快速上手的书中解释。虽然每次使用logging需要设置logging.basicConfig,
但好处也是显而易见的,当你不想输出logging输出的时候,只需要把logging,disable同等级调整到最高就可以了。
首相我先上一个format常用格式说明
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
英文版本:
可选的格式参数如下(Python 3.5):
%(asctime)s: Human-readable time when theLogRecord
was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).
%(created)f: Time when theLogRecord
was created (as returned bytime.time()
).
%(filename)s: Filename portion ofpathname
.
%(funcName)s: Name of function containing the logging call.
%(levelname)s: Text logging level for the message (‘DEBUG‘
,‘INFO‘
,‘WARNING‘
,‘ERROR‘
,‘CRITICAL‘
).
%(levelno)s: Numeric logging level for the message (DEBUG
,INFO
,WARNING
,ERROR
,CRITICAL
).
%(lineno)d: Source line number where the logging call was issued (if available).
%(module)s: Module (name portion offilename
).
%(msecs)d: Millisecond portion of the time when theLogRecord
was created.
%(message)s: The logged message, computed asmsg%args
. This is set whenFormatter.format()
is invoked.
%(name)s: Name of the logger used to log the call.
%(pathname)s: Full pathname of the source file where the logging call was issued (if available).
%(process)d: Process ID (if available).
%(processName)s: Process name (if available).
%(relativeCreated)d: Time in milliseconds when the LogRecord was created, relative to the time the logging module was loaded.
%(thread)d: Thread ID (if available).
%(threadName)s: Thread name (if available).
这个以后在写basicConfig里面要用,写在开头便于记忆。
先上简单代码:
import logging logging.basicConfig(level=logging.DEBUG) # logging.disable(logging.CRITICAL) # 这个可以设置等级,设置为该等级,则该等级与该等级以下的内容不会被执行。 logging.debug(‘hello‘) logging.info(‘hello‘) logging.warning(‘hello‘) logging.error(‘hello‘) logging.critical(‘hello‘)
/usr/local/bin/python3.7 /Users/shijianzhong/study/t_logging/t1.py DEBUG:root:hello INFO:root:hello WARNING:root:hello ERROR:root:hello CRITICAL:root:hello
从输出可以看出,默认的信息输出为等级,第二个%(name)s,,第三个信息。
先简单记录一下,logging其实还是非常复杂的,等后续用到的时候,再来添加相关内容。
原文:https://www.cnblogs.com/sidianok/p/12020130.html