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

Skip to content

Commit 07b1661

Browse files
committed
Issue #18759: Merged updates from 3.3.
2 parents e06d47c + 67f3977 commit 07b1661

4 files changed

Lines changed: 76 additions & 66 deletions

File tree

Doc/howto/logging-cookbook.rst

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ The output looks like this::
9797
Multiple handlers and formatters
9898
--------------------------------
9999

100-
Loggers are plain Python objects. The :func:`addHandler` method has no minimum
101-
or maximum quota for the number of handlers you may add. Sometimes it will be
102-
beneficial for an application to log all messages of all severities to a text
103-
file while simultaneously logging errors or above to the console. To set this
104-
up, simply configure the appropriate handlers. The logging calls in the
100+
Loggers are plain Python objects. The :meth:`~Logger.addHandler` method has no
101+
minimum or maximum quota for the number of handlers you may add. Sometimes it
102+
will be beneficial for an application to log all messages of all severities to a
103+
text file while simultaneously logging errors or above to the console. To set
104+
this up, simply configure the appropriate handlers. The logging calls in the
105105
application code will remain unchanged. Here is a slight modification to the
106106
previous simple module-based configuration example::
107107

@@ -459,8 +459,9 @@ printed on the console; on the server side, you should see something like::
459459

460460
Note that there are some security issues with pickle in some scenarios. If
461461
these affect you, you can use an alternative serialization scheme by overriding
462-
the :meth:`makePickle` method and implementing your alternative there, as
463-
well as adapting the above script to use your alternative serialization.
462+
the :meth:`~handlers.SocketHandler.makePickle` method and implementing your
463+
alternative there, as well as adapting the above script to use your alternative
464+
serialization.
464465

465466

466467
.. _context-info:
@@ -509,9 +510,9 @@ information in the delegated call. Here's a snippet from the code of
509510
msg, kwargs = self.process(msg, kwargs)
510511
self.logger.debug(msg, *args, **kwargs)
511512

512-
The :meth:`process` method of :class:`LoggerAdapter` is where the contextual
513-
information is added to the logging output. It's passed the message and
514-
keyword arguments of the logging call, and it passes back (potentially)
513+
The :meth:`~LoggerAdapter.process` method of :class:`LoggerAdapter` is where the
514+
contextual information is added to the logging output. It's passed the message
515+
and keyword arguments of the logging call, and it passes back (potentially)
515516
modified versions of these to use in the call to the underlying logger. The
516517
default implementation of this method leaves the message alone, but inserts
517518
an 'extra' key in the keyword argument whose value is the dict-like object
@@ -523,8 +524,8 @@ merged into the :class:`LogRecord` instance's __dict__, allowing you to use
523524
customized strings with your :class:`Formatter` instances which know about
524525
the keys of the dict-like object. If you need a different method, e.g. if you
525526
want to prepend or append the contextual information to the message string,
526-
you just need to subclass :class:`LoggerAdapter` and override :meth:`process`
527-
to do what you need. Here is a simple example::
527+
you just need to subclass :class:`LoggerAdapter` and override
528+
:meth:`~LoggerAdapter.process` to do what you need. Here is a simple example::
528529

529530
class CustomAdapter(logging.LoggerAdapter):
530531
"""
@@ -633,20 +634,20 @@ threads in a single process *is* supported, logging to a single file from
633634
*multiple processes* is *not* supported, because there is no standard way to
634635
serialize access to a single file across multiple processes in Python. If you
635636
need to log to a single file from multiple processes, one way of doing this is
636-
to have all the processes log to a :class:`SocketHandler`, and have a separate
637-
process which implements a socket server which reads from the socket and logs
638-
to file. (If you prefer, you can dedicate one thread in one of the existing
639-
processes to perform this function.) :ref:`This section <network-logging>`
640-
documents this approach in more detail and includes a working socket receiver
641-
which can be used as a starting point for you to adapt in your own
642-
applications.
637+
to have all the processes log to a :class:`~handlers.SocketHandler`, and have a
638+
separate process which implements a socket server which reads from the socket
639+
and logs to file. (If you prefer, you can dedicate one thread in one of the
640+
existing processes to perform this function.)
641+
:ref:`This section <network-logging>` documents this approach in more detail and
642+
includes a working socket receiver which can be used as a starting point for you
643+
to adapt in your own applications.
643644

644645
If you are using a recent version of Python which includes the
645646
:mod:`multiprocessing` module, you could write your own handler which uses the
646-
:class:`Lock` class from this module to serialize access to the file from
647-
your processes. The existing :class:`FileHandler` and subclasses do not make
648-
use of :mod:`multiprocessing` at present, though they may do so in the future.
649-
Note that at present, the :mod:`multiprocessing` module does not provide
647+
:class:`~multiprocessing.Lock` class from this module to serialize access to the
648+
file from your processes. The existing :class:`FileHandler` and subclasses do
649+
not make use of :mod:`multiprocessing` at present, though they may do so in the
650+
future. Note that at present, the :mod:`multiprocessing` module does not provide
650651
working lock functionality on all platforms (see
651652
http://bugs.python.org/issue3770).
652653

@@ -878,7 +879,7 @@ Sometimes you want to let a log file grow to a certain size, then open a new
878879
file and log to that. You may want to keep a certain number of these files, and
879880
when that many files have been created, rotate the files so that the number of
880881
files and the size of the files both remain bounded. For this usage pattern, the
881-
logging package provides a :class:`RotatingFileHandler`::
882+
logging package provides a :class:`~handlers.RotatingFileHandler`::
882883

883884
import glob
884885
import logging
@@ -1252,7 +1253,7 @@ An example dictionary-based configuration
12521253

12531254
Below is an example of a logging configuration dictionary - it's taken from
12541255
the `documentation on the Django project <https://docs.djangoproject.com/en/1.3/topics/logging/#configuring-logging>`_.
1255-
This dictionary is passed to :func:`~logging.config.dictConfig` to put the configuration into effect::
1256+
This dictionary is passed to :func:`~config.dictConfig` to put the configuration into effect::
12561257

12571258
LOGGING = {
12581259
'version': 1,

Doc/howto/logging.rst

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,13 @@ Handlers
469469

470470
:class:`~logging.Handler` objects are responsible for dispatching the
471471
appropriate log messages (based on the log messages' severity) to the handler's
472-
specified destination. Logger objects can add zero or more handler objects to
473-
themselves with an :func:`addHandler` method. As an example scenario, an
474-
application may want to send all log messages to a log file, all log messages
475-
of error or higher to stdout, and all messages of critical to an email address.
476-
This scenario requires three individual handlers where each handler is
477-
responsible for sending messages of a specific severity to a specific location.
472+
specified destination. :class:`Logger` objects can add zero or more handler
473+
objects to themselves with an :meth:`~Logger.addHandler` method. As an example
474+
scenario, an application may want to send all log messages to a log file, all
475+
log messages of error or higher to stdout, and all messages of critical to an
476+
email address. This scenario requires three individual handlers where each
477+
handler is responsible for sending messages of a specific severity to a specific
478+
location.
478479

479480
The standard library includes quite a few handler types (see
480481
:ref:`useful-handlers`); the tutorials use mainly :class:`StreamHandler` and
@@ -485,16 +486,17 @@ themselves with. The only handler methods that seem relevant for application
485486
developers who are using the built-in handler objects (that is, not creating
486487
custom handlers) are the following configuration methods:
487488

488-
* The :meth:`Handler.setLevel` method, just as in logger objects, specifies the
489+
* The :meth:`~Handler.setLevel` method, just as in logger objects, specifies the
489490
lowest severity that will be dispatched to the appropriate destination. Why
490491
are there two :func:`setLevel` methods? The level set in the logger
491492
determines which severity of messages it will pass to its handlers. The level
492493
set in each handler determines which messages that handler will send on.
493494

494-
* :func:`setFormatter` selects a Formatter object for this handler to use.
495+
* :meth:`~Handler.setFormatter` selects a Formatter object for this handler to
496+
use.
495497

496-
* :func:`addFilter` and :func:`removeFilter` respectively configure and
497-
deconfigure filter objects on handlers.
498+
* :meth:`~Handler.addFilter` and :meth:`~Handler.removeFilter` respectively
499+
configure and deconfigure filter objects on handlers.
498500

499501
Application code should not directly instantiate and use instances of
500502
:class:`Handler`. Instead, the :class:`Handler` class is a base class that
@@ -948,16 +950,16 @@ Logged messages are formatted for presentation through instances of the
948950
use with the % operator and a dictionary.
949951

950952
For formatting multiple messages in a batch, instances of
951-
:class:`BufferingFormatter` can be used. In addition to the format string (which
952-
is applied to each message in the batch), there is provision for header and
953-
trailer format strings.
953+
:class:`~handlers.BufferingFormatter` can be used. In addition to the format
954+
string (which is applied to each message in the batch), there is provision for
955+
header and trailer format strings.
954956

955957
When filtering based on logger level and/or handler level is not enough,
956958
instances of :class:`Filter` can be added to both :class:`Logger` and
957-
:class:`Handler` instances (through their :meth:`addFilter` method). Before
958-
deciding to process a message further, both loggers and handlers consult all
959-
their filters for permission. If any filter returns a false value, the message
960-
is not processed further.
959+
:class:`Handler` instances (through their :meth:`~Handler.addFilter` method).
960+
Before deciding to process a message further, both loggers and handlers consult
961+
all their filters for permission. If any filter returns a false value, the
962+
message is not processed further.
961963

962964
The basic :class:`Filter` functionality allows filtering by specific logger
963965
name. If this feature is used, messages sent to the named logger and its
@@ -975,12 +977,14 @@ in production. This is so that errors which occur while handling logging events
975977
cause the application using logging to terminate prematurely.
976978

977979
:class:`SystemExit` and :class:`KeyboardInterrupt` exceptions are never
978-
swallowed. Other exceptions which occur during the :meth:`emit` method of a
979-
:class:`Handler` subclass are passed to its :meth:`handleError` method.
980+
swallowed. Other exceptions which occur during the :meth:`~Handler.emit` method
981+
of a :class:`Handler` subclass are passed to its :meth:`~Handler.handleError`
982+
method.
980983

981-
The default implementation of :meth:`handleError` in :class:`Handler` checks
982-
to see if a module-level variable, :data:`raiseExceptions`, is set. If set, a
983-
traceback is printed to :data:`sys.stderr`. If not set, the exception is swallowed.
984+
The default implementation of :meth:`~Handler.handleError` in :class:`Handler`
985+
checks to see if a module-level variable, :data:`raiseExceptions`, is set. If
986+
set, a traceback is printed to :data:`sys.stderr`. If not set, the exception is
987+
swallowed.
984988

985989
.. note:: The default value of :data:`raiseExceptions` is ``True``. This is
986990
because during development, you typically want to be notified of any
@@ -997,11 +1001,11 @@ Using arbitrary objects as messages
9971001
In the preceding sections and examples, it has been assumed that the message
9981002
passed when logging the event is a string. However, this is not the only
9991003
possibility. You can pass an arbitrary object as a message, and its
1000-
:meth:`__str__` method will be called when the logging system needs to convert
1001-
it to a string representation. In fact, if you want to, you can avoid
1004+
:meth:`~object.__str__` method will be called when the logging system needs to
1005+
convert it to a string representation. In fact, if you want to, you can avoid
10021006
computing a string representation altogether - for example, the
1003-
:class:`SocketHandler` emits an event by pickling it and sending it over the
1004-
wire.
1007+
:class:`~handlers.SocketHandler` emits an event by pickling it and sending it
1008+
over the wire.
10051009

10061010

10071011
Optimization
@@ -1010,9 +1014,10 @@ Optimization
10101014
Formatting of message arguments is deferred until it cannot be avoided.
10111015
However, computing the arguments passed to the logging method can also be
10121016
expensive, and you may want to avoid doing it if the logger will just throw
1013-
away your event. To decide what to do, you can call the :meth:`isEnabledFor`
1014-
method which takes a level argument and returns true if the event would be
1015-
created by the Logger for that level of call. You can write code like this::
1017+
away your event. To decide what to do, you can call the
1018+
:meth:`~Logger.isEnabledFor` method which takes a level argument and returns
1019+
true if the event would be created by the Logger for that level of call.
1020+
You can write code like this::
10161021

10171022
if logger.isEnabledFor(logging.DEBUG):
10181023
logger.debug('Message with %s, %s', expensive_func1(),

Doc/library/logging.config.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ in :mod:`logging` itself) and defining handlers which are declared either in
122122
configurations. If no port is specified, the module's default
123123
:const:`DEFAULT_LOGGING_CONFIG_PORT` is used. Logging configurations will be
124124
sent as a file suitable for processing by :func:`fileConfig`. Returns a
125-
:class:`Thread` instance on which you can call :meth:`start` to start the
126-
server, and which you can :meth:`join` when appropriate. To stop the server,
125+
:class:`~threading.Thread` instance on which you can call
126+
:meth:`~threading.Thread.start` to start the server, and which you can
127+
:meth:`~threading.Thread.join` when appropriate. To stop the server,
127128
call :func:`stopListening`.
128129

129130
The ``verify`` argument, if specified, should be a callable which should
@@ -203,11 +204,11 @@ otherwise, the context is used to determine what to instantiate.
203204

204205
* *formatters* - the corresponding value will be a dict in which each
205206
key is a formatter id and each value is a dict describing how to
206-
configure the corresponding Formatter instance.
207+
configure the corresponding :class:`~logging.Formatter` instance.
207208

208209
The configuring dict is searched for keys ``format`` and ``datefmt``
209210
(with defaults of ``None``) and these are used to construct a
210-
:class:`logging.Formatter` instance.
211+
:class:`~logging.Formatter` instance.
211212

212213
* *filters* - the corresponding value will be a dict in which each key
213214
is a filter id and each value is a dict describing how to configure
@@ -741,8 +742,9 @@ format string, with a comma separator. An example time in ISO8601 format is
741742

742743
The ``class`` entry is optional. It indicates the name of the formatter's class
743744
(as a dotted module and class name.) This option is useful for instantiating a
744-
:class:`Formatter` subclass. Subclasses of :class:`Formatter` can present
745-
exception tracebacks in an expanded or condensed format.
745+
:class:`~logging.Formatter` subclass. Subclasses of
746+
:class:`~logging.Formatter` can present exception tracebacks in an expanded or
747+
condensed format.
746748

747749
.. note:: Due to the use of :func:`eval` as described above, there are
748750
potential security risks which result from using the :func:`listen` to send

Doc/library/logging.handlers.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ and :meth:`flush` methods).
5353
.. method:: flush()
5454

5555
Flushes the stream by calling its :meth:`flush` method. Note that the
56-
:meth:`close` method is inherited from :class:`Handler` and so does
57-
no output, so an explicit :meth:`flush` call may be needed at times.
56+
:meth:`close` method is inherited from :class:`~logging.Handler` and so
57+
does no output, so an explicit :meth:`flush` call may be needed at times.
5858

5959
.. versionchanged:: 3.2
6060
The ``StreamHandler`` class now has a ``terminator`` attribute, default
@@ -145,8 +145,8 @@ new stream.
145145
This handler is not appropriate for use under Windows, because under Windows
146146
open log files cannot be moved or renamed - logging opens the files with
147147
exclusive locks - and so there is no need for such a handler. Furthermore,
148-
*ST_INO* is not supported under Windows; :func:`stat` always returns zero for
149-
this value.
148+
*ST_INO* is not supported under Windows; :func:`~os.stat` always returns zero
149+
for this value.
150150

151151

152152
.. class:: WatchedFileHandler(filename[,mode[, encoding[, delay]]])
@@ -389,7 +389,8 @@ sends logging output to a network socket. The base class uses a TCP socket.
389389
binary format. If there is an error with the socket, silently drops the
390390
packet. If the connection was previously lost, re-establishes the
391391
connection. To unpickle the record at the receiving end into a
392-
:class:`LogRecord`, use the :func:`makeLogRecord` function.
392+
:class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
393+
function.
393394

394395

395396
.. method:: handleError()
@@ -467,7 +468,8 @@ over UDP sockets.
467468
Pickles the record's attribute dictionary and writes it to the socket in
468469
binary format. If there is an error with the socket, silently drops the
469470
packet. To unpickle the record at the receiving end into a
470-
:class:`LogRecord`, use the :func:`makeLogRecord` function.
471+
:class:`~logging.LogRecord`, use the :func:`~logging.makeLogRecord`
472+
function.
471473

472474

473475
.. method:: makeSocket()

0 commit comments

Comments
 (0)