logging --- Python 的日誌記錄工具¶
這個模組定義了函式與類別 (class),為應用程式和函式庫實作彈性的日誌管理系統。
由標準函式庫模組提供的日誌記錄 API 的主要好處是,所有的 Python 模組都能參與日誌記錄,因此你的應用程式日誌可以包含你自己的訊息,並與來自第三方模組的訊息整合在一起。
Here's a simple example of idiomatic usage:
# myapp.py
import logging
import mylib
logger = logging.getLogger(__name__)
def main():
logging.basicConfig(filename='myapp.log', level=logging.INFO)
logger.info('Started')
mylib.do_something()
logger.info('Finished')
if __name__ == '__main__':
main()
# mylib.py
import logging
logger = logging.getLogger(__name__)
def do_something():
logger.info('Doing something')
If you run myapp.py, you should see this in myapp.log:
INFO:__main__:Started
INFO:mylib:Doing something
INFO:__main__:Finished
The key feature of this idiomatic usage is that the majority of code is simply
creating a module level logger with getLogger(__name__), and using that
logger to do any needed logging. This is concise, while allowing downstream
code fine-grained control if needed. Logged messages to the module-level logger
get forwarded to handlers of loggers in higher-level modules, all the way up to
the highest-level logger known as the root logger; this approach is known as
hierarchical logging.
For logging to be useful, it needs to be configured: setting the levels and
destinations for each logger, potentially changing how specific modules log,
often based on command-line arguments or application configuration. In most
cases, like the one above, only the root logger needs to be so configured, since
all the lower level loggers at module level eventually forward their messages to
its handlers. basicConfig() provides a quick way to configure
the root logger that handles many use cases.
這個模組提供了很多的功能性以及彈性。如果你對於 logging 不熟悉,熟悉它最好的方法就是去看教學(請看右上方的連結)。
The basic classes defined by the module, together with their attributes and methods, are listed in the sections below.
Loggers expose the interface that application code directly uses.
Handlers send the log records (created by loggers) to the appropriate destination.
Filters provide a finer grained facility for determining which log records to output.
格式器指定日誌記錄在最終輸出中的佈局。
Logger 物件¶
Loggers have the following attributes and methods. Note that Loggers should
NEVER be instantiated directly, but always through the module-level function
logging.getLogger(name). Multiple calls to getLogger() with the same
name will always return a reference to the same Logger object.
The name is potentially a period-separated hierarchical value, like
foo.bar.baz (though it could also be just plain foo, for example).
Loggers that are further down in the hierarchical list are children of loggers
higher up in the list. For example, given a logger with a name of foo,
loggers with names of foo.bar, foo.bar.baz, and foo.bam are all
descendants of foo. In addition, all loggers are descendants of the root
logger. The logger name hierarchy is analogous to the Python package hierarchy,
and identical to it if you organise your loggers on a per-module basis using
the recommended construction logging.getLogger(__name__). That's because
in a module, __name__ is the module's name in the Python package namespace.
- class logging.Logger¶
- name¶
This is the logger's name, and is the value that was passed to
getLogger()to obtain the logger.備註
此屬性應被視為唯讀。
- level¶
The threshold of this logger, as set by the
setLevel()method.備註
Do not set this attribute directly - always use
setLevel(), which has checks for the level passed to it.
- parent¶
The parent logger of this logger. It may change based on later instantiation of loggers which are higher up in the namespace hierarchy.
備註
This value should be treated as read-only.
- propagate¶
如果此屬性評估為 true,則在此日誌紀錄器被記錄的事件會被傳到更高階(上代)日誌記錄器 的處理函式和所有附加在此日誌記錄器的任何處理器。訊息會直接傳到上代 loggers 的處理器 - 在問題中上代日誌記錄器的層級或是篩選器都不會被考慮。
If this evaluates to false, logging messages are not passed to the handlers of ancestor loggers.
Spelling it out with an example: If the propagate attribute of the logger named
A.B.Cevaluates to true, any event logged toA.B.Cvia a method call such aslogging.getLogger('A.B.C').error(...)will [subject to passing that logger's level and filter settings] be passed in turn to any handlers attached to loggers namedA.B,Aand the root logger, after first being passed to any handlers attached toA.B.C. If any logger in the chainA.B.C,A.B,Ahas itspropagateattribute set to false, then that is the last logger whose handlers are offered the event to handle, and propagation stops at that point.此建構函式將該屬性設為
True。備註
If you attach a handler to a logger and one or more of its ancestors, it may emit the same record multiple times. In general, you should not need to attach a handler to more than one logger - if you just attach it to the appropriate logger which is highest in the logger hierarchy, then it will see all events logged by all descendant loggers, provided that their propagate setting is left set to
True. A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.
- handlers¶
The list of handlers directly attached to this logger instance.
備註
This attribute should be treated as read-only; it is normally changed via the
addHandler()andremoveHandler()methods, which use locks to ensure thread-safe operation.
- disabled¶
This attribute disables handling of any events. It is set to
Falsein the initializer, and only changed by logging configuration code.備註
此屬性應被視為唯讀。
- setLevel(level)¶
Sets the threshold for this logger to level. Logging messages which are less severe than level will be ignored; logging messages which have severity level or higher will be emitted by whichever handler or handlers service this logger, unless a handler's level has been set to a higher severity level than level.
當一個日誌記錄器被建立時,記錄層級會被設定成
NOTSET(當此日誌記錄器是根日誌記錄器,或是代表父日誌記錄器的非根日誌記錄器時,會使所有訊息被處理)。請注意根日誌記錄器會以記錄等級WARNING被建立。The term 'delegation to the parent' means that if a logger has a level of NOTSET, its chain of ancestor loggers is traversed until either an ancestor with a level other than NOTSET is found, or the root is reached.
If an ancestor is found with a level other than NOTSET, then that ancestor's level is treated as the effective level of the logger where the ancestor search began, and is used to determine how a logging event is handled.
If the root is reached, and it has a level of NOTSET, then all messages will be processed. Otherwise, the root's level will be used as the effective level.
層級清單請見 Logging Levels。
在 3.2 版的變更: The level parameter now accepts a string representation of the level such as 'INFO' as an alternative to the integer constants such as
INFO. Note, however, that levels are internally stored as integers, and methods such as e.g.getEffectiveLevel()andisEnabledFor()will return/expect to be passed integers.
- isEnabledFor(level)¶
Indicates if a message of severity level would be processed by this logger. This method checks first the module-level level set by
logging.disable(level)and then the logger's effective level as determined bygetEffectiveLevel().
- getEffectiveLevel()¶
Indicates the effective level for this logger. If a value other than
NOTSEThas been set usingsetLevel(), it is returned. Otherwise, the hierarchy is traversed towards the root until a value other thanNOTSETis found, and that value is returned. The value returned is an integer, typically one oflogging.DEBUG,logging.INFOetc.
- getChild(suffix)¶
Returns a logger which is a descendant to this logger, as determined by the suffix. Thus,
logging.getLogger('abc').getChild('def.ghi')would return the same logger as would be returned bylogging.getLogger('abc.def.ghi'). This is a convenience method, useful when the parent logger is named using e.g.__name__rather than a literal string.在 3.2 版被加入.
- getChildren()¶
Returns a set of loggers which are immediate children of this logger. So for example
logging.getLogger().getChildren()might return a set containing loggers namedfooandbar, but a logger namedfoo.barwouldn't be included in the set. Likewise,logging.getLogger('foo').getChildren()might return a set including a logger namedfoo.bar, but it wouldn't include one namedfoo.bar.baz.在 3.12 版被加入.
- debug(msg, *args, **kwargs)¶
Logs a message with level
DEBUGon this logger. The msg is the message format string, and the args are the arguments which are merged into msg using the string formatting operator. (Note that this means that you can use keywords in the format string, together with a single dictionary argument.) No % formatting operation is performed on msg when no args are supplied.There are four keyword arguments in kwargs which are inspected: exc_info, stack_info, stacklevel and extra.
If exc_info does not evaluate as false, it causes exception information to be added to the logging message. If an exception tuple (in the format returned by
sys.exc_info()) or an exception instance is provided, it is used; otherwise,sys.exc_info()is called to get the exception information.The second optional keyword argument is stack_info, which defaults to
False. If true, stack information is added to the logging message, including the actual logging call. Note that this is not the same stack information as that displayed through specifying exc_info: The former is stack frames from the bottom of the stack up to the logging call in the current thread, whereas the latter is information about stack frames which have been unwound, following an exception, while searching for exception handlers.You can specify stack_info independently of exc_info, e.g. to just show how you got to a certain point in your code, even when no exceptions were raised. The stack frames are printed following a header line which says:
Stack (most recent call last):
This mimics the
Traceback (most recent call last):which is used when displaying exception frames.The third optional keyword argument is stacklevel, which defaults to
1. If greater than 1, the corresponding number of stack frames are skipped when computing the line number and function name set in theLogRecordcreated for the logging event. This can be used in logging helpers so that the function name, filename and line number recorded are not the information for the helper function/method, but rather its caller. The name of this parameter mirrors the equivalent one in thewarningsmodule.The fourth keyword argument is extra which can be used to pass a dictionary which is used to populate the
__dict__of theLogRecordcreated for the logging event with user-defined attributes. These custom attributes can then be used as you like. For example, they could be incorporated into logged messages. For example:FORMAT = '%(asctime)s %(clientip)-15s %(user)-8s %(message)s' logging.basicConfig(format=FORMAT) d = {'clientip': '192.168.0.1', 'user': 'fbloggs'} logger = logging.getLogger('tcpserver') logger.warning('Protocol problem: %s', 'connection reset', extra=d)
would print something like
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
The keys in the dictionary passed in extra should not clash with the keys used by the logging system. (See the section on LogRecord 屬性 for more information on which keys are used by the logging system.)
If you choose to use these attributes in logged messages, you need to exercise some care. In the above example, for instance, the
Formatterhas been set up with a format string which expects 'clientip' and 'user' in the attribute dictionary of theLogRecord. If these are missing, the message will not be logged because a string formatting exception will occur. So in this case, you always need to pass the extra dictionary with these keys.While this might be annoying, this feature is intended for use in specialized circumstances, such as multi-threaded servers where the same code executes in many contexts, and interesting conditions which arise are dependent on this context (such as remote client IP address and authenticated user name, in the above example). In such circumstances, it is likely that specialized
Formatters would be used with particularHandlers.If no handler is attached to this logger (or any of its ancestors, taking into account the relevant
Logger.propagateattributes), the message will be sent to the handler set onlastResort.在 3.2 版的變更: 新增 stack_info 參數。
在 3.5 版的變更: The exc_info parameter can now accept exception instances.
在 3.8 版的變更: 新增 stacklevel 參數。
- info(msg, *args, **kwargs)¶
Logs a message with level
INFOon this logger. The arguments are interpreted as fordebug().
- warning(msg, *args, **kwargs)¶
在此記錄器上記錄一條層級為
WARNING的訊息。這些引數被直譯的方式與debug()相同。備註
There is an obsolete method
warnwhich is functionally identical towarning. Aswarnis deprecated, please do not use it - usewarninginstead.
- error(msg, *args, **kwargs)¶
Logs a message with level
ERRORon this logger. The arguments are interpreted as fordebug().
- critical(msg, *args, **kwargs)¶
Logs a message with level
CRITICALon this logger. The arguments are interpreted as fordebug().
- log(level, msg, *args, **kwargs)¶
Logs a message with integer level level on this logger. The other arguments are interpreted as for
debug().
- exception(msg, *args, **kwargs)¶
Logs a message with level
ERRORon this logger. The arguments are interpreted as fordebug(). Exception info is added to the logging message. This method should only be called from an exception handler.
- addFilter(filter)¶
在該 logger 內增加指定的 filter filter。
- removeFilter(filter)¶
在該 logger 內移除指定的 filter filter。
- filter(record)¶
Apply this logger's filters to the record and return
Trueif the record is to be processed. The filters are consulted in turn, until one of them returns a false value. If none of them return a false value, the record will be processed (passed to handlers). If one returns a false value, no further processing of the record occurs.
- addHandler(hdlr)¶
Adds the specified handler hdlr to this logger.
- removeHandler(hdlr)¶
Removes the specified handler hdlr from this logger.
- findCaller(stack_info=False, stacklevel=1)¶
Finds the caller's source filename and line number. Returns the filename, line number, function name and stack information as a 4-element tuple. The stack information is returned as
Noneunless stack_info isTrue.The stacklevel parameter is passed from code calling the
debug()and other APIs. If greater than 1, the excess is used to skip stack frames before determining the values to be returned. This will generally be useful when calling logging APIs from helper/wrapper code, so that the information in the event log refers not to the helper/wrapper code, but to the code that calls it.
- handle(record)¶
Handles a record by passing it to all handlers associated with this logger and its ancestors (until a false value of propagate is found). This method is used for unpickled records received from a socket, as well as those created locally. Logger-level filtering is applied using
filter().
- makeRecord(name, level, fn, lno, msg, args, exc_info, func=None, extra=None, sinfo=None)¶
This is a factory method which can be overridden in subclasses to create specialized
LogRecordinstances.
- hasHandlers()¶
Checks to see if this logger has any handlers configured. This is done by looking for handlers in this logger and its parents in the logger hierarchy. Returns
Trueif a handler was found, elseFalse. The method stops searching up the hierarchy whenever a logger with the 'propagate' attribute set to false is found - that will be the last logger which is checked for the existence of handlers.在 3.2 版被加入.
在 3.7 版的變更: Loggers can now be pickled and unpickled.
Logging Levels¶
The numeric values of logging levels are given in the following table. These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost.
Level |
Numeric value |
What it means / When to use it |
|---|---|---|
|
0 |
When set on a logger, indicates that
ancestor loggers are to be consulted
to determine the effective level.
If that still resolves to
|
|
10 |
Detailed information, typically only of interest to a developer trying to diagnose a problem. |
|
20 |
Confirmation that things are working as expected. |
|
30 |
An indication that something unexpected happened, or that a problem might occur in the near future (e.g. 'disk space low'). The software is still working as expected. |
|
40 |
Due to a more serious problem, the software has not been able to perform some function. |
|
50 |
A serious error, indicating that the program itself may be unable to continue running. |
Handler Objects¶
Handlers have the following attributes and methods. Note that Handler
is never instantiated directly; this class acts as a base for more useful
subclasses. However, the __init__() method in subclasses needs to call
Handler.__init__().
- class logging.Handler¶
- __init__(level=NOTSET)¶
Initializes the
Handlerinstance by setting its level, setting the list of filters to the empty list and creating a lock (usingcreateLock()) for serializing access to an I/O mechanism.
- createLock()¶
Initializes a thread lock which can be used to serialize access to underlying I/O functionality which may not be threadsafe.
- acquire()¶
Acquires the thread lock created with
createLock().
- setLevel(level)¶
Sets the threshold for this handler to level. Logging messages which are less severe than level will be ignored. When a handler is created, the level is set to
NOTSET(which causes all messages to be processed).層級清單請見 Logging Levels。
在 3.2 版的變更: The level parameter now accepts a string representation of the level such as 'INFO' as an alternative to the integer constants such as
INFO.
- setFormatter(fmt)¶
Sets the formatter for this handler to fmt. The fmt argument must be a
Formatterinstance orNone.
- addFilter(filter)¶
Adds the specified filter filter to this handler.
- removeFilter(filter)¶
Removes the specified filter filter from this handler.
- filter(record)¶
Apply this handler's filters to the record and return
Trueif the record is to be processed. The filters are consulted in turn, until one of them returns a false value. If none of them return a false value, the record will be emitted. If one returns a false value, the handler will not emit the record.
- flush()¶
Ensure all logging output has been flushed. This version does nothing and is intended to be implemented by subclasses.
- close()¶
Tidy up any resources used by the handler. This version does no output but removes the handler from an internal map of handlers, which is used for handler lookup by name.
Subclasses should ensure that this gets called from overridden
close()methods.
- handle(record