summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorGéry Ogam <gery.ogam@gmail.com>2018-09-25 07:24:52 (GMT)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>2018-09-25 07:24:52 (GMT)
commit5b3cbcd4a041eeda935dd6d0c75f2d38111ed03d (patch)
treec29084a74cf6502dabcf41701f5dbe4914ad6408 /Doc
parentf6c8007a29b95b3ea3ca687a9b4924769a696328 (diff)
downloadcpython-5b3cbcd4a041eeda935dd6d0c75f2d38111ed03d.zip
cpython-5b3cbcd4a041eeda935dd6d0c75f2d38111ed03d.tar.gz
cpython-5b3cbcd4a041eeda935dd6d0c75f2d38111ed03d.tar.bz2
Improved the more elaborate multiprocessing example in the logging cookbook (GH-9326)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/howto/logging-cookbook.rst58
1 files changed, 29 insertions, 29 deletions
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst
index b1930a7..285aff7 100644
--- a/Doc/howto/logging-cookbook.rst
+++ b/Doc/howto/logging-cookbook.rst
@@ -1456,12 +1456,18 @@ works::
which then get dispatched, by the logging system, to the handlers
configured for those loggers.
"""
+
def handle(self, record):
- logger = logging.getLogger(record.name)
- # The process name is transformed just to show that it's the listener
- # doing the logging to files and console
- record.processName = '%s (for %s)' % (current_process().name, record.processName)
- logger.handle(record)
+ if record.name == "root":
+ logger = logging.getLogger()
+ else:
+ logger = logging.getLogger(record.name)
+
+ if logger.isEnabledFor(record.levelno):
+ # The process name is transformed just to show that it's the listener
+ # doing the logging to files and console
+ record.processName = '%s (for %s)' % (current_process().name, record.processName)
+ logger.handle(record)
def listener_process(q, stop_event, config):
"""
@@ -1526,22 +1532,16 @@ works::
# The main process gets a simple configuration which prints to the console.
config_initial = {
'version': 1,
- 'formatters': {
- 'detailed': {
- 'class': 'logging.Formatter',
- 'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
- }
- },
'handlers': {
'console': {
'class': 'logging.StreamHandler',
- 'level': 'INFO',
- },
+ 'level': 'INFO'
+ }
},
'root': {
- 'level': 'DEBUG',
- 'handlers': ['console']
- },
+ 'handlers': ['console'],
+ 'level': 'DEBUG'
+ }
}
# The worker process configuration is just a QueueHandler attached to the
# root logger, which allows all messages to be sent to the queue.
@@ -1554,13 +1554,13 @@ works::
'handlers': {
'queue': {
'class': 'logging.handlers.QueueHandler',
- 'queue': q,
- },
+ 'queue': q
+ }
},
'root': {
- 'level': 'DEBUG',
- 'handlers': ['queue']
- },
+ 'handlers': ['queue'],
+ 'level': 'DEBUG'
+ }
}
# The listener process configuration shows that the full flexibility of
# logging configuration is available to dispatch events to handlers however
@@ -1584,28 +1584,28 @@ works::
'handlers': {
'console': {
'class': 'logging.StreamHandler',
- 'level': 'INFO',
'formatter': 'simple',
+ 'level': 'INFO'
},
'file': {
'class': 'logging.FileHandler',
'filename': 'mplog.log',
'mode': 'w',
- 'formatter': 'detailed',
+ 'formatter': 'detailed'
},
'foofile': {
'class': 'logging.FileHandler',
'filename': 'mplog-foo.log',
'mode': 'w',
- 'formatter': 'detailed',
+ 'formatter': 'detailed'
},
'errors': {
'class': 'logging.FileHandler',
'filename': 'mplog-errors.log',
'mode': 'w',
- 'level': 'ERROR',
'formatter': 'detailed',
- },
+ 'level': 'ERROR'
+ }
},
'loggers': {
'foo': {
@@ -1613,9 +1613,9 @@ works::
}
},
'root': {
- 'level': 'DEBUG',
- 'handlers': ['console', 'file', 'errors']
- },
+ 'handlers': ['console', 'file', 'errors'],
+ 'level': 'DEBUG'
+ }
}
# Log some initial events, just to show that logging in the parent works
# normally.