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

Skip to content

Commit a6b7d34

Browse files
committed
Fixed a table that wasn't in a tableii block, and added a very simple
example to show how to log to a file.
1 parent b5aa407 commit a6b7d34

1 file changed

Lines changed: 72 additions & 26 deletions

File tree

Doc/lib/liblogging.tex

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -802,32 +802,36 @@ \subsection{Formatter Objects}
802802
A Formatter can be initialized with a format string which makes use of
803803
knowledge of the \class{LogRecord} attributes - such as the default value
804804
mentioned above making use of the fact that the user's message and
805-
arguments are pre- formatted into a LogRecord's \var{message}
806-
attribute. Currently, the useful attributes in a LogRecord are
807-
described by:
808-
809-
\%(name)s Name of the logger (logging channel)
810-
\%(levelno)s Numeric logging level for the message (DEBUG, INFO,
811-
WARNING, ERROR, CRITICAL)
812-
\%(levelname)s Text logging level for the message ("DEBUG", "INFO",
813-
"WARNING", "ERROR", "CRITICAL")
814-
\%(pathname)s Full pathname of the source file where the logging
815-
call was issued (if available)
816-
\%(filename)s Filename portion of pathname
817-
\%(module)s Module (name portion of filename)
818-
\%(lineno)d Source line number where the logging call was issued
819-
(if available)
820-
\%(created)f Time when the LogRecord was created (time.time()
821-
return value)
822-
\%(asctime)s Textual time when the LogRecord was created
823-
\%(msecs)d Millisecond portion of the creation time
824-
\%(relativeCreated)d Time in milliseconds when the LogRecord was created,
825-
relative to the time the logging module was loaded
826-
(typically at application startup time)
827-
\%(thread)d Thread ID (if available)
828-
\%(process)d Process ID (if available)
829-
\%(message)s The result of msg \% args, computed just as the
830-
record is emitted
805+
arguments are pre-formatted into a LogRecord's \var{message}
806+
attribute. This format string contains standard python \%-style
807+
mapping keys. See section \ref{typesseq-strings}, ``String Formatting
808+
Operations,'' for more information on string formatting.
809+
810+
Currently, the useful mapping keys in a LogRecord are:
811+
812+
\begin{tableii}{l|l}{formats}{Format}{Description}
813+
\lineii{\%(name)s}{Name of the logger (logging channel).}
814+
\lineii{\%(levelno)s}{Numeric logging level for the message (DEBUG, INFO,
815+
WARNING, ERROR, CRITICAL).}
816+
\lineii{\%(levelname)s}{Text logging level for the message ("DEBUG", "INFO",
817+
"WARNING", "ERROR", "CRITICAL").}
818+
\lineii{\%(pathname)s}{Full pathname of the source file where the logging
819+
call was issued (if available).}
820+
\lineii{\%(filename)s}{Filename portion of pathname.}
821+
\lineii{\%(module)s}{Module (name portion of filename).}
822+
\lineii{\%(lineno)d}{Source line number where the logging call was issued
823+
(if available).}
824+
\lineii{\%(created)f}{Time when the LogRecord was created (as returned by
825+
\code{time.time()}).}
826+
\lineii{\%(asctime)s}{Human-readable time when the LogRecord was created.
827+
By default this is of the form ``2003-07-08 16:49:45,896'' (the numbers
828+
after the comma are millisecond portion of the time).}
829+
\lineii{\%(msecs)d}{Millisecond portion of the time when the LogRecord
830+
was created.}
831+
\lineii{\%(thread)d}{Thread ID (if available).}
832+
\lineii{\%(process)d}{Process ID (if available).}
833+
\lineii{\%(message)s}{The logged message, computed as msg \% args.}
834+
\end{tableii}
831835

832836
\begin{classdesc}{Formatter}{\optional{fmt\optional{, datefmt}}}
833837
Returns a new instance of the \class{Formatter} class. The
@@ -1124,3 +1128,45 @@ \subsubsection{Configuration file format}
11241128
The ISO8601 format also specifies milliseconds, which are appended to the
11251129
result of using the above format string, with a comma separator. An example
11261130
time in ISO8601 format is \code{2003-01-23 00:29:50,411}.
1131+
1132+
\subsection{Using the logging package}
1133+
1134+
\subsubsection{Basic example - log to a file}
1135+
1136+
Here's a simple logging example that just logs to a file. In order,
1137+
it creates a \class{Logger} instance, then a \class{FileHandler}
1138+
and a \class{Formatter}. It attaches the \class{Formatter} to the
1139+
\class{FileHandler}, then the \class{FileHandler} to the \class{Logger}.
1140+
Finally, it sets a debug level for the logger.
1141+
1142+
\begin{verbatim}
1143+
import logging
1144+
logger = logging.getLogger('myapp')
1145+
hdlr = logging.FileHandler('/var/tmp/myapp.log')
1146+
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
1147+
hdlr.setFormatter(formatter)
1148+
logger.addHandler(hdlr)
1149+
logger.setLevel(logging.WARNING)
1150+
\end{verbatim}
1151+
1152+
We can use this logger object now to write entries to the log file:
1153+
1154+
\begin{verbatim}
1155+
logger.error('We have a problem')
1156+
logger.info('While this is just chatty')
1157+
\end{verbatim}
1158+
1159+
If we look in the file that was created, we'll see something like this:
1160+
\begin{verbatim}
1161+
2003-07-08 16:49:45,896 ERROR We have a problem
1162+
\end{verbatim}
1163+
1164+
The info message was not written to the file - we called the \method{setLevel}
1165+
method to say we only wanted \code{WARNING} or worse, so the info message is
1166+
discarded.
1167+
1168+
The timestamp is of the form
1169+
``year-month-day hour:minutes:seconds,milliseconds.''
1170+
Note that despite the three digits of precision in the milliseconds field,
1171+
not all systems provide time with this much precision.
1172+

0 commit comments

Comments
 (0)