Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4549c4

Browse files
committed
Added information on function name added to LogRecord, and the 'extra' keyword parameter.
1 parent ed1992f commit b4549c4

1 file changed

Lines changed: 96 additions & 13 deletions

File tree

Doc/lib/liblogging.tex

Lines changed: 96 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,52 @@ \section{\module{logging} ---
183183
\begin{funcdesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
184184
Logs a message with level \constant{DEBUG} on the root logger.
185185
The \var{msg} is the message format string, and the \var{args} are the
186-
arguments which are merged into \var{msg}. The only keyword argument in
187-
\var{kwargs} which is inspected is \var{exc_info} which, if it does not
188-
evaluate as false, causes exception information to be added to the logging
189-
message. If an exception tuple (in the format returned by
190-
\function{sys.exc_info()}) is provided, it is used; otherwise,
191-
\function{sys.exc_info()} is called to get the exception information.
186+
arguments which are merged into \var{msg} using the string formatting
187+
operator. (Note that this means that you can use keywords in the
188+
format string, together with a single dictionary argument.)
189+
190+
There are two keyword arguments in \var{kwargs} which are inspected:
191+
\var{exc_info} which, if it does not evaluate as false, causes exception
192+
information to be added to the logging message. If an exception tuple (in the
193+
format returned by \function{sys.exc_info()}) is provided, it is used;
194+
otherwise, \function{sys.exc_info()} is called to get the exception
195+
information.
196+
197+
The other optional keyword argument is \var{extra} which can be used to pass
198+
a dictionary which is used to populate the __dict__ of the LogRecord created
199+
for the logging event with user-defined attributes. These custom attributes
200+
can then be used as you like. For example, they could be incorporated into
201+
logged messages. For example:
202+
203+
\begin{verbatim}
204+
FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"
205+
logging.basicConfig(format=FORMAT)
206+
dict = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
207+
logging.warning("Protocol problem: %s", "connection reset", extra=d)
208+
\end{verbatim}
209+
210+
would print something like
211+
\begin{verbatim}
212+
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
213+
\end{verbatim}
214+
215+
The keys in the dictionary passed in \var{extra} should not clash with the keys
216+
used by the logging system. (See the \class{Formatter} documentation for more
217+
information on which keys are used by the logging system.)
218+
219+
If you choose to use these attributes in logged messages, you need to exercise
220+
some care. In the above example, for instance, the \class{Formatter} has been
221+
set up with a format string which expects 'clientip' and 'user' in the
222+
attribute dictionary of the LogRecord. If these are missing, the message will
223+
not be logged because a string formatting exception will occur. So in this
224+
case, you always need to pass the \var{extra} dictionary with these keys.
225+
226+
While this might be annoying, this feature is intended for use in specialized
227+
circumstances, such as multi-threaded servers where the same code executes
228+
in many contexts, and interesting conditions which arise are dependent on this
229+
context (such as remote client IP address and authenticated user name, in the
230+
above example). In such circumstances, it is likely that specialized
231+
\class{Formatter}s would be used with particular \class{Handler}s.
192232
\end{funcdesc}
193233

194234
\begin{funcdesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
@@ -367,12 +407,53 @@ \subsection{Logger Objects}
367407
\begin{methoddesc}{debug}{msg\optional{, *args\optional{, **kwargs}}}
368408
Logs a message with level \constant{DEBUG} on this logger.
369409
The \var{msg} is the message format string, and the \var{args} are the
370-
arguments which are merged into \var{msg}. The only keyword argument in
371-
\var{kwargs} which is inspected is \var{exc_info} which, if it does not
372-
evaluate as false, causes exception information to be added to the logging
373-
message. If an exception tuple (as provided by \function{sys.exc_info()})
374-
is provided, it is used; otherwise, \function{sys.exc_info()} is called
375-
to get the exception information.
410+
arguments which are merged into \var{msg} using the string formatting
411+
operator. (Note that this means that you can use keywords in the
412+
format string, together with a single dictionary argument.)
413+
414+
There are two keyword arguments in \var{kwargs} which are inspected:
415+
\var{exc_info} which, if it does not evaluate as false, causes exception
416+
information to be added to the logging message. If an exception tuple (in the
417+
format returned by \function{sys.exc_info()}) is provided, it is used;
418+
otherwise, \function{sys.exc_info()} is called to get the exception
419+
information.
420+
421+
The other optional keyword argument is \var{extra} which can be used to pass
422+
a dictionary which is used to populate the __dict__ of the LogRecord created
423+
for the logging event with user-defined attributes. These custom attributes
424+
can then be used as you like. For example, they could be incorporated into
425+
logged messages. For example:
426+
427+
\begin{verbatim}
428+
FORMAT = "%(asctime)-15s %(clientip)s %(user)-8s %(message)s"
429+
logging.basicConfig(format=FORMAT)
430+
dict = { 'clientip' : '192.168.0.1', 'user' : 'fbloggs' }
431+
logger = logging.getLogger("tcpserver")
432+
logger.warning("Protocol problem: %s", "connection reset", extra=d)
433+
\end{verbatim}
434+
435+
would print something like
436+
\begin{verbatim}
437+
2006-02-08 22:20:02,165 192.168.0.1 fbloggs Protocol problem: connection reset
438+
\end{verbatim}
439+
440+
The keys in the dictionary passed in \var{extra} should not clash with the keys
441+
used by the logging system. (See the \class{Formatter} documentation for more
442+
information on which keys are used by the logging system.)
443+
444+
If you choose to use these attributes in logged messages, you need to exercise
445+
some care. In the above example, for instance, the \class{Formatter} has been
446+
set up with a format string which expects 'clientip' and 'user' in the
447+
attribute dictionary of the LogRecord. If these are missing, the message will
448+
not be logged because a string formatting exception will occur. So in this
449+
case, you always need to pass the \var{extra} dictionary with these keys.
450+
451+
While this might be annoying, this feature is intended for use in specialized
452+
circumstances, such as multi-threaded servers where the same code executes
453+
in many contexts, and interesting conditions which arise are dependent on this
454+
context (such as remote client IP address and authenticated user name, in the
455+
above example). In such circumstances, it is likely that specialized
456+
\class{Formatter}s would be used with particular \class{Handler}s.
376457
\end{methoddesc}
377458

378459
\begin{methoddesc}{info}{msg\optional{, *args\optional{, **kwargs}}}
@@ -441,7 +522,8 @@ \subsection{Logger Objects}
441522
\method{filter()}.
442523
\end{methoddesc}
443524

444-
\begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info}
525+
\begin{methoddesc}{makeRecord}{name, lvl, fn, lno, msg, args, exc_info,
526+
func, extra}
445527
This is a factory method which can be overridden in subclasses to create
446528
specialized \class{LogRecord} instances.
447529
\end{methoddesc}
@@ -1305,6 +1387,7 @@ \subsection{Formatter Objects}
13051387
call was issued (if available).}
13061388
\lineii{\%(filename)s} {Filename portion of pathname.}
13071389
\lineii{\%(module)s} {Module (name portion of filename).}
1390+
\lineii{\%(funcName)s} {Name of function containing the logging call.}
13081391
\lineii{\%(lineno)d} {Source line number where the logging call was issued
13091392
(if available).}
13101393
\lineii{\%(created)f} {Time when the \class{LogRecord} was created (as

0 commit comments

Comments
 (0)