@@ -330,11 +330,10 @@ Suppose you configure logging with the following JSON:
330330 }
331331 }
332332
333- This configuration does *almost * what we want, except that ``sys.stdout `` would show messages
334- of severity ``ERROR `` and only events of this severity and higher will be tracked
335- as well as ``INFO `` and ``WARNING `` messages. To prevent this, we can set up a filter which
336- excludes those messages and add it to the relevant handler. This can be configured by
337- adding a ``filters `` section parallel to ``formatters `` and ``handlers ``:
333+ This configuration does *almost * what we want, except that ``sys.stdout `` would
334+ also show messages of severity ``ERROR `` and above. To exclude those messages,
335+ we can add a filter to the stdout handler. This can be configured by adding a
336+ ``filters `` section parallel to ``formatters `` and ``handlers ``:
338337
339338.. code-block :: json
340339
@@ -562,9 +561,9 @@ One solution is to use a two-part approach. For the first part, attach only a
562561:class: `QueueHandler ` to those loggers which are accessed from
563562performance-critical threads. They simply write to their queue, which can be
564563sized to a large enough capacity or initialized with no upper bound to their
565- size. The write to the queue will typically be accepted quickly, though you
566- will probably need to catch the :exc: ` queue.Full ` exception as a precaution
567- in your code . If you are a library developer who has performance-critical
564+ size. The write to the queue will typically be accepted quickly. If a bounded
565+ queue is full, :class: ` QueueHandler ` calls :meth: ` ~logging.Handler.handleError `
566+ with the record . If you are a library developer who has performance-critical
568567threads in their code, be sure to document this (together with a suggestion to
569568attach only ``QueueHandlers `` to your loggers) for the benefit of other
570569developers who will use your code.
@@ -1608,8 +1607,8 @@ application:
16081607
16091608 The most current file is always :file: `logging_rotatingfile_example.out `,
16101609and each time it reaches the size limit it is renamed with the suffix
1611- ``.1 ``. Each of the existing backup files is renamed to increment the suffix
1612- (``.1 `` becomes ``.2 ``, etc.) and the `` .6 `` file is erased .
1610+ ``.1 ``. Before this happens, the oldest backup (`` .5 ``), if present, is
1611+ deleted and the remaining backups are renamed (``.4 `` becomes ``.5 ``, etc.).
16131612
16141613Obviously this example sets the log length much too small as an extreme
16151614example. You would want to set *maxBytes * to an appropriate value.
@@ -1801,7 +1800,7 @@ Before Python 3.2, there were only two places where this creation was done:
18011800* :func: `makeLogRecord `, which is called with a dictionary containing
18021801 attributes to be added to the LogRecord. This is typically invoked when a
18031802 suitable dictionary has been received over the network (e.g. in pickle form
1804- via a :class: `~handlers.SocketHandler `, or in JSON form via an
1803+ via a :class: `~handlers.SocketHandler `, or as URL-encoded data via an
18051804 :class: `~handlers.HTTPHandler `).
18061805
18071806This has usually meant that if you need to do anything special with a
@@ -1827,11 +1826,7 @@ do simply by adding new packages or modules and doing ::
18271826
18281827 logger = logging.getLogger(__name__)
18291828
1830- at module level). It's probably one too many things to think about. Developers
1831- could also add the filter to a :class: `~logging.NullHandler ` attached to their
1832- top-level logger, but this would not be invoked if an application developer
1833- attached a handler to a lower-level library logger --- so output from that
1834- handler would not reflect the intentions of the library developer.
1829+ at module level). It's probably one too many things to think about.
18351830
18361831In Python 3.2 and later, :class: `~logging.LogRecord ` creation is done through a
18371832factory, which you can specify. The factory is just a callable you can set with
@@ -1913,15 +1908,15 @@ You can also subclass :class:`QueueListener` to get messages from other kinds
19131908of queues, for example a ZeroMQ 'subscribe' socket. Here's an example::
19141909
19151910 class ZeroMQSocketListener(QueueListener):
1916- def __init__(self, uri, /, *handlers, **kwargs):
1917- self.ctx = kwargs.get(' ctx') or zmq.Context()
1911+ def __init__(self, uri, /, *handlers, ctx=None, **kwargs):
1912+ self.ctx = ctx or zmq.Context()
19181913 socket = zmq.Socket(self.ctx, zmq.SUB)
19191914 socket.setsockopt_string(zmq.SUBSCRIBE, '') # subscribe to everything
19201915 socket.connect(uri)
19211916 super().__init__(socket, *handlers, **kwargs)
19221917
1923- def dequeue(self):
1924- msg = self.queue.recv_json()
1918+ def dequeue(self, block ):
1919+ msg = self.queue.recv_json(flags=0 if block else zmq.NOBLOCK )
19251920 return logging.makeLogRecord(msg)
19261921
19271922.. _pynng-handlers :
@@ -2245,7 +2240,7 @@ works::
22452240 import logging.config
22462241 import logging.handlers
22472242 from multiprocessing import Process, Queue, Event, current_process
2248- import os
2243+ from multiprocessing import get_start_method
22492244 import random
22502245 import time
22512246
@@ -2281,13 +2276,8 @@ works::
22812276 logging.config.dictConfig(config)
22822277 listener = logging.handlers.QueueListener(q, MyHandler())
22832278 listener.start()
2284- if os.name == 'posix':
2285- # On POSIX, the setup logger will have been configured in the
2286- # parent process, but should have been disabled following the
2287- # dictConfig call.
2288- # On Windows, since fork isn't used, the setup logger won't
2289- # exist in the child, so it would be created and the message
2290- # would appear - hence the "if posix" clause.
2279+ if get_start_method() == 'fork':
2280+ # dictConfig disables the setup logger inherited from the parent.
22912281 logger = logging.getLogger('setup')
22922282 logger.critical('Should not appear, because of disabled logger ...')
22932283 stop_event.wait()
@@ -2312,13 +2302,8 @@ works::
23122302 logging.CRITICAL]
23132303 loggers = ['foo', 'foo.bar', 'foo.bar.baz',
23142304 'spam', 'spam.ham', 'spam.ham.eggs']
2315- if os.name == 'posix':
2316- # On POSIX, the setup logger will have been configured in the
2317- # parent process, but should have been disabled following the
2318- # dictConfig call.
2319- # On Windows, since fork isn't used, the setup logger won't
2320- # exist in the child, so it would be created and the message
2321- # would appear - hence the "if posix" clause.
2305+ if get_start_method() == 'fork':
2306+ # dictConfig disables the setup logger inherited from the parent.
23222307 logger = logging.getLogger('setup')
23232308 logger.critical('Should not appear, because of disabled logger ...')
23242309 for i in range(100):
@@ -2345,9 +2330,7 @@ works::
23452330 }
23462331 # The worker process configuration is just a QueueHandler attached to the
23472332 # root logger, which allows all messages to be sent to the queue.
2348- # We disable existing loggers to disable the "setup" logger used in the
2349- # parent process. This is needed on POSIX because the logger will
2350- # be there in the child following a fork().
2333+ # Disable the parent's "setup" logger if it was inherited via fork.
23512334 config_worker = {
23522335 'version': 1,
23532336 'disable_existing_loggers': True,
@@ -2365,9 +2348,7 @@ works::
23652348 # The listener process configuration shows that the full flexibility of
23662349 # logging configuration is available to dispatch events to handlers however
23672350 # you want.
2368- # We disable existing loggers to disable the "setup" logger used in the
2369- # parent process. This is needed on POSIX because the logger will
2370- # be there in the child following a fork().
2351+ # Disable the parent's "setup" logger if it was inherited via fork.
23712352 config_listener = {
23722353 'version': 1,
23732354 'disable_existing_loggers': True,
@@ -4077,11 +4058,9 @@ How to uniformly handle newlines in logging output
40774058
40784059Usually, messages that are logged (say to console or file) consist of a single
40794060line of text. However, sometimes there is a need to handle messages with
4080- multiple lines - whether because a logging format string contains newlines, or
4081- logged data contains newlines. If you want to handle such messages uniformly, so
4082- that each line in the logged message appears uniformly formatted as if it was
4083- logged separately, you can do this using a handler mixin, as in the following
4084- snippet:
4061+ multiple lines because the message or its arguments contain newlines.
4062+ To format each line as if it were logged separately, you can use a handler
4063+ mixin, as in the following snippet:
40854064
40864065.. code-block :: python
40874066
0 commit comments