日志消息采用级别
DEBUG
on 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
.
若
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
,其默认为
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
,其默认为
1
. If greater than 1, the corresponding number of stack frames are skipped when computing the line number and function name set in the
LogRecord
created 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 the
warnings
模块。
The fourth keyword argument is
extra
which can be used to pass a dictionary which is used to populate the
__dict__
的
LogRecord
created 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
Formatter
has been set up with a format string which expects ‘clientip’ and ‘user’ in the attribute dictionary of the
LogRecord
. 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
Formatter
s would be used with particular
Handler
。
If no handler is attached to this logger (or any of its ancestors, taking into account the relevant
Logger.propagate
attributes), the message will be sent to the handler set on
lastResort
.
3.2 版改变:
The
stack_info
参数被添加。
3.5 版改变:
The
exc_info
参数现在可以接受异常实例。
3.8 版改变:
The
stacklevel
参数被添加。