python logging标准模块转载2

来源:互联网 发布:mac os x 10.12升级包 编辑:程序博客网 时间:2024/05/18 10:06

Formatters

Formatter objects configure the final order, structure, and contents of the log message. Unlike the base logging.Handler class, application code may instantiate formatter classes, although you could likely subclass the formatter if your application needs special behavior. The constructor takes two optional arguments: a message format string and a date format string. If there is no message format string, the default is to use the raw message. If there is no date format string, the default date format is:

Formatter对象定义了最终log信息的顺序,结构和内容.于基本的logging.Handler类不同,应用可以直接实例化formatter,当然,如果需要你也可以子例化formatter以便定制它的一些行为.构造函数接受两个可选参数:一个信息格式字符串和一个日期格式字符串.如果没有信息格式字符串,直接输出log信息.如果没有日期格式字符串,默认的格式是:

%Y-%m-%d %H:%M:%S

with the milliseconds tacked on at the end.

毫秒加载最后面.

The message format string uses %(<dictionary key>)s styled string substitution. Here is the Formatter class's docstring, which describes the valid substitution strings and what they mean:

信息格式字符串用%(<dictionary key>)s风格的字符串做替换.下面是Formatter类的docstring,说明了现有的替换字符串和它们所代表的意思.

%(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)

%(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)

%(process)d        Process ID (if available)

%(message)s        The result of record.getMessage(), computed just as

                   the record is emitted

The following message format string will log the time in a human-readable format, the severity of the message, and the contents of the message, in that order:

下面的信息格式字符串会输出一个可读格式的串,严重级别和log信息内容以下面的格式给出:

"%(asctime)s - %(levelname)s - %(message)s"

0 0
原创粉丝点击