Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
689b68ad
Commit
689b68ad
authored
Dec 22, 2010
by
Vinay Sajip
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Logging documentation updates.
parent
5e9b14c3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
47 deletions
+48
-47
Doc/howto/logging-cookbook.rst
Doc/howto/logging-cookbook.rst
+2
-3
Doc/howto/logging.rst
Doc/howto/logging.rst
+42
-41
Doc/library/logging.handlers.rst
Doc/library/logging.handlers.rst
+4
-3
No files found.
Doc/howto/logging-cookbook.rst
View file @
689b68ad
...
@@ -8,8 +8,6 @@ Logging Cookbook
...
@@ -8,8 +8,6 @@ Logging Cookbook
This page contains a number of recipes related to logging, which have been found useful in the past.
This page contains a number of recipes related to logging, which have been found useful in the past.
.. Contents::
.. currentmodule:: logging
.. currentmodule:: logging
Using logging in multiple modules
Using logging in multiple modules
...
@@ -249,7 +247,8 @@ configuration::
...
@@ -249,7 +247,8 @@ configuration::
#!/usr/bin/env python
#!/usr/bin/env python
import socket, sys, struct
import socket, sys, struct
data_to_send = open(sys.argv[1], 'r').read()
with open(sys.argv[1], 'rb') as f:
data_to_send = f.read()
HOST = 'localhost'
HOST = 'localhost'
PORT = 9999
PORT = 9999
...
...
Doc/howto/logging.rst
View file @
689b68ad
...
@@ -4,8 +4,6 @@ Logging HOWTO
...
@@ -4,8 +4,6 @@ Logging HOWTO
:Author: Vinay Sajip <vinay_sajip at red-dove dot com>
:Author: Vinay Sajip <vinay_sajip at red-dove dot com>
.. Contents::
.. _logging-basic-tutorial:
.. _logging-basic-tutorial:
.. currentmodule:: logging
.. currentmodule:: logging
...
@@ -312,11 +310,9 @@ understand something, please post a question on the comp.lang.python Usenet
...
@@ -312,11 +310,9 @@ understand something, please post a question on the comp.lang.python Usenet
group (available at http://groups.google.com/group/comp.lang.python) and you
group (available at http://groups.google.com/group/comp.lang.python) and you
should receive help before too long.
should receive help before too long.
Still here? There's no need to read the whole of the logging documentation in
Still here? You can carry on reading the next few sections, which provide a
linear fashion, top to bottom (there's quite a lot of it still to come). You
slightly more advanced/in-depth tutorial than the basic one above. After that,
can carry on reading the next few sections, which provide a slightly more
you can take a look at the :ref:`logging-cookbook`.
advanced/in-depth tutorial than the basic one above. After that, you can
take a look at the :ref:`logging-cookbook`.
.. _logging-advanced-tutorial:
.. _logging-advanced-tutorial:
...
@@ -459,14 +455,14 @@ attribute of a logger to *False*.)
...
@@ -459,14 +455,14 @@ attribute of a logger to *False*.)
Handlers
Handlers
^^^^^^^^
^^^^^^^^
:class:`
Handler` objects are responsible for dispatching the appropriate log
:class:`
~logging.Handler` objects are responsible for dispatching the
messages (based on the log messages' severity) to the handler's specified
appropriate log messages (based on the log messages' severity) to the handler's
destination. Logger objects can add zero or more handler objects to themselves
specified destination. Logger objects can add zero or more handler objects to
with an :func:`addHandler` method. As an example scenario, an application may
themselves with an :func:`addHandler` method. As an example scenario, an
want to send all log messages to a log file, all log messages of error or higher
application may want to send all log messages to a log file, all log messages
to stdout, and all messages of critical to an email address. This scenario
of error or higher to stdout, and all messages of critical to an email address.
requires three individual handlers where each handler is responsible for sending
This scenario requires three individual handlers where each handler is
messages of a specific severity to a specific location.
responsible for sending
messages of a specific severity to a specific location.
The standard library includes quite a few handler types (see
The standard library includes quite a few handler types (see
:ref:`useful-handlers`); the tutorials use mainly :class:`StreamHandler` and
:ref:`useful-handlers`); the tutorials use mainly :class:`StreamHandler` and
...
@@ -542,6 +538,8 @@ Formatter class (to ``time.gmtime`` for GMT display).
...
@@ -542,6 +538,8 @@ Formatter class (to ``time.gmtime`` for GMT display).
Configuring Logging
Configuring Logging
^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^
.. currentmodule:: logging.config
Programmers can configure logging in three ways:
Programmers can configure logging in three ways:
1. Creating loggers, handlers, and formatters explicitly using Python
1. Creating loggers, handlers, and formatters explicitly using Python
...
@@ -653,10 +651,12 @@ You can see that the config file approach has a few advantages over the Python
...
@@ -653,10 +651,12 @@ You can see that the config file approach has a few advantages over the Python
code approach, mainly separation of configuration and code and the ability of
code approach, mainly separation of configuration and code and the ability of
noncoders to easily modify the logging properties.
noncoders to easily modify the logging properties.
.. currentmodule:: logging
Note that the class names referenced in config files need to be either relative
Note that the class names referenced in config files need to be either relative
to the logging module, or absolute values which can be resolved using normal
to the logging module, or absolute values which can be resolved using normal
import mechanisms. Thus, you could use either
import mechanisms. Thus, you could use either
:class:`handlers.WatchedFileHandler` (relative to the logging module) or
:class:`
~logging.
handlers.WatchedFileHandler` (relative to the logging module) or
``mypackage.mymodule.MyHandler`` (for a class defined in package ``mypackage``
``mypackage.mymodule.MyHandler`` (for a class defined in package ``mypackage``
and module ``mymodule``, where ``mypackage`` is available on the Python import
and module ``mymodule``, where ``mypackage`` is available on the Python import
path).
path).
...
@@ -718,8 +718,8 @@ In Python 3.2 and later, the behaviour is as follows:
...
@@ -718,8 +718,8 @@ In Python 3.2 and later, the behaviour is as follows:
* The event is output using a 'handler of last resort', stored in
* The event is output using a 'handler of last resort', stored in
``logging.lastResort``. This internal handler is not associated with any
``logging.lastResort``. This internal handler is not associated with any
logger, and acts like a :class:`
StreamHandler` which writes the event
logger, and acts like a :class:`
~logging.StreamHandler` which writes the
description message to the current value of ``sys.stderr`` (therefore
event
description message to the current value of ``sys.stderr`` (therefore
respecting any redirections which may be in effect). No formatting is
respecting any redirections which may be in effect). No formatting is
done on the message - just the bare event description message is printed.
done on the message - just the bare event description message is printed.
The handler's level is set to ``WARNING``, so all events at this and
The handler's level is set to ``WARNING``, so all events at this and
...
@@ -749,12 +749,13 @@ that configuration will add some handlers, and if levels are suitably
...
@@ -749,12 +749,13 @@ that configuration will add some handlers, and if levels are suitably
configured then logging calls made in library code will send output to those
configured then logging calls made in library code will send output to those
handlers, as normal.
handlers, as normal.
A do-nothing handler is included in the logging package: :class:`NullHandler`
A do-nothing handler is included in the logging package:
(since Python 3.1). An instance of this handler could be added to the top-level
:class:`~logging.NullHandler` (since Python 3.1). An instance of this handler
logger of the logging namespace used by the library (*if* you want to prevent
could be added to the top-level logger of the logging namespace used by the
your library's logged events being output to ``sys.stderr`` in the absence of
library (*if* you want to prevent your library's logged events being output to
logging configuration). If all logging by a library *foo* is done using loggers
``sys.stderr`` in the absence of logging configuration). If all logging by a
with names matching 'foo.x', 'foo.x.y', etc. then the code::
library *foo* is done using loggers with names matching 'foo.x', 'foo.x.y',
etc. then the code::
import logging
import logging
logging.getLogger('foo').addHandler(logging.NullHandler())
logging.getLogger('foo').addHandler(logging.NullHandler())
...
@@ -764,12 +765,12 @@ libraries, then the logger name specified can be 'orgname.foo' rather than
...
@@ -764,12 +765,12 @@ libraries, then the logger name specified can be 'orgname.foo' rather than
just 'foo'.
just 'foo'.
**PLEASE NOTE:** It is strongly advised that you *do not add any handlers other
**PLEASE NOTE:** It is strongly advised that you *do not add any handlers other
than* :class:`
NullHandler` *to your library's loggers*. This is because the
than* :class:`
~logging.NullHandler` *to your library's loggers*. This is
configuration of handlers is the prerogative of the application developer who
because the configuration of handlers is the prerogative of the application
uses your library. The application developer knows their target audience and
developer who uses your library. The application developer knows their target
what handlers are most appropriate for their application: if you add handlers
audience and what handlers are most appropriate for their application: if you
'under the hood', you might well interfere with their ability to carry out
add handlers 'under the hood', you might well interfere with their ability to
unit tests and deliver logs which suit their requirements.
carry out
unit tests and deliver logs which suit their requirements.
Logging Levels
Logging Levels
...
@@ -804,9 +805,9 @@ the method call. If the logger's level is higher than the method call's, no
...
@@ -804,9 +805,9 @@ the method call. If the logger's level is higher than the method call's, no
logging message is actually generated. This is the basic mechanism controlling
logging message is actually generated. This is the basic mechanism controlling
the verbosity of logging output.
the verbosity of logging output.
Logging messages are encoded as instances of the :class:`
LogRecord` class. When
Logging messages are encoded as instances of the :class:`
~logging.LogRecord`
a logger decides to actually log an event, a :class:`LogRecord` instance is
class. When a logger decides to actually log an event, a
created from the logging message.
:class:`~logging.LogRecord` instance is
created from the logging message.
Logging messages are subjected to a dispatch mechanism through the use of
Logging messages are subjected to a dispatch mechanism through the use of
:dfn:`handlers`, which are instances of subclasses of the :class:`Handler`
:dfn:`handlers`, which are instances of subclasses of the :class:`Handler`
...
@@ -816,17 +817,17 @@ which is useful for the target audience for that message (such as end users,
...
@@ -816,17 +817,17 @@ which is useful for the target audience for that message (such as end users,
support desk staff, system administrators, developers). Handlers are passed
support desk staff, system administrators, developers). Handlers are passed
:class:`LogRecord` instances intended for particular destinations. Each logger
:class:`LogRecord` instances intended for particular destinations. Each logger
can have zero, one or more handlers associated with it (via the
can have zero, one or more handlers associated with it (via the
:meth:`
addHandler` method of :class:`Logger`). In addition to any handlers
:meth:`
~Logger.addHandler` method of :class:`Logger`). In addition to any
directly associated with a logger, *all handlers associated with all ancestors
handlers directly associated with a logger, *all handlers associated with all
of the logger* are called to dispatch the message (unless the *propagate* flag
ancestors of the logger* are called to dispatch the message (unless the
for a logger is set to a false value, at which point the passing to ancestor
*propagate* flag for a logger is set to a false value, at which point the
handlers stops).
passing to ancestor
handlers stops).
Just as for loggers, handlers can have levels associated with them. A handler's
Just as for loggers, handlers can have levels associated with them. A handler's
level acts as a filter in the same way as a logger's level does. If a handler
level acts as a filter in the same way as a logger's level does. If a handler
decides to actually dispatch an event, the :meth:`
emit` method is used to sen
d
decides to actually dispatch an event, the :meth:`
~Handler.emit` method is use
d
t
he message to its destination. Most user-defined subclasses of :class:`Handler`
t
o send the message to its destination. Most user-defined subclasses of
will need to override this :meth:`
emit`.
:class:`Handler` will need to override this :meth:`~Handler.
emit`.
.. _custom-levels:
.. _custom-levels:
...
...
Doc/library/logging.handlers.rst
View file @
689b68ad
...
@@ -45,9 +45,9 @@ and :meth:`flush` methods).
...
@@ -45,9 +45,9 @@ and :meth:`flush` methods).
.. method:: emit(record)
.. method:: emit(record)
If a formatter is specified, it is used to format the record. The record
If a formatter is specified, it is used to format the record. The record
is then written to the stream with a t
railing newline. If excep
tion
is then written to the stream with a t
erminator. If exception informa
tion
i
nformation is present, it is formatted using
i
s present, it is formatted using :func:`traceback.print_exception` and
:func:`traceback.print_exception` and
appended to the stream.
appended to the stream.
.. method:: flush()
.. method:: flush()
...
@@ -61,6 +61,7 @@ and :meth:`flush` methods).
...
@@ -61,6 +61,7 @@ and :meth:`flush` methods).
value ``'\n'``, which is used as the terminator when writing a formatted
value ``'\n'``, which is used as the terminator when writing a formatted
record to a stream. If you don't want this newline termination, you can
record to a stream. If you don't want this newline termination, you can
set the handler instance's ``terminator`` attribute to the empty string.
set the handler instance's ``terminator`` attribute to the empty string.
In earlier versions, the terminator was hardcoded as ``'\n'``.
.. _file-handler:
.. _file-handler:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment