Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OpenVMS
Python
cpython
Commits
c51389fa4f9a
Commit
c51389fa4f9a
authored
20 years ago
by
Vinay Sajip
Browse files
Options
Downloads
Patches
Plain Diff
Added example of multiple destinations
parent
185c2677c004
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Doc/lib/liblogging.tex
+66
-2
66 additions, 2 deletions
Doc/lib/liblogging.tex
with
66 additions
and
2 deletions
Doc/lib/liblogging.tex
+
66
−
2
View file @
c51389fa
...
@@ -507,9 +507,9 @@
...
@@ -507,9 +507,9 @@
datefmt='
%a, %d %b %Y %H:%M:%S',
datefmt='
%a, %d %b %Y %H:%M:%S',
filename='/temp/myapp.log',
filename='/temp/myapp.log',
filemode='w')
filemode='w')
logging.error('Pack my box with
%d dozen %s',
12
, 'liquor jugs')
logging.error('Pack my box with
%d dozen %s',
5
, 'liquor jugs')
\end{verbatim}
\end{verbatim}
which would result in
which would result in
\begin{verbatim}
\begin{verbatim}
...
@@ -511,8 +511,8 @@
...
@@ -511,8 +511,8 @@
\end{verbatim}
\end{verbatim}
which would result in
which would result in
\begin{verbatim}
\begin{verbatim}
Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with
12
dozen liquor jugs
Wed, 21 Jul 2004 15:35:16 ERROR Pack my box with
5
dozen liquor jugs
\end{verbatim}
\end{verbatim}
...
@@ -517,5 +517,69 @@
...
@@ -517,5 +517,69 @@
\end{verbatim}
\end{verbatim}
\subsection
{
Logging to multiple destinations
\label
{
multiple-destinations
}}
Let's say you want to log to console and file with different message formats
and in differing circumstances. Say you want to log messages with levels
of DEBUG and higher to file, and those messages at level INFO and higher to
the console. Let's also assume that the file should contain timestamps, but
the console messages should not. Here's how you can achieve this:
\begin{verbatim}
import logging
#set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
format='
%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='
%m-%d %H:%M',
filename='/temp/myapp.log',
filemode='w')
#define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
#set a format which is simpler for console use
formatter = logging.Formatter('
%(name)-12s: %(levelname)-8s %(message)s')
#tell the handler to use this format
console.setFormatter(formatter)
#add the handler to the root logger
logging.getLogger('').addHandler(console)
#Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')
#Now, define a couple of other loggers which might represent areas in your
#application:
logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')
\end{verbatim}
When you run this, on the console you will see
\begin{verbatim}
root : INFO Jackdaws love my big sphinx of quartz.
myapp.area1 : INFO How quickly daft jumping zebras vex.
myapp.area2 : WARNING Jail zesty vixen who grabbed pay from quack.
myapp.area2 : ERROR The five boxing wizards jump quickly.
\end{verbatim}
and in the file you will see something like
\begin{verbatim}
10-22 22:19 root INFO Jackdaws love my big sphinx of quartz.
10-22 22:19 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim.
10-22 22:19 myapp.area1 INFO How quickly daft jumping zebras vex.
10-22 22:19 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack.
10-22 22:19 myapp.area2 ERROR The five boxing wizards jump quickly.
\end{verbatim}
As you can see, the DEBUG message only shows up in the file. The other
messages are sent to both destinations.
\subsection
{
Handler Objects
}
\subsection
{
Handler Objects
}
Handlers have the following attributes and methods. Note that
Handlers have the following attributes and methods. Note that
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment