Improved the more elaborate multiprocessing example in the logging cookbook (GH-9326)

This commit is contained in:
Géry Ogam 2018-09-25 09:24:52 +02:00 committed by Vinay Sajip
parent f6c8007a29
commit 5b3cbcd4a0

View file

@ -1456,12 +1456,18 @@ works::
which then get dispatched, by the logging system, to the handlers which then get dispatched, by the logging system, to the handlers
configured for those loggers. configured for those loggers.
""" """
def handle(self, record): def handle(self, record):
logger = logging.getLogger(record.name) if record.name == "root":
# The process name is transformed just to show that it's the listener logger = logging.getLogger()
# doing the logging to files and console else:
record.processName = '%s (for %s)' % (current_process().name, record.processName) logger = logging.getLogger(record.name)
logger.handle(record)
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): def listener_process(q, stop_event, config):
""" """
@ -1526,22 +1532,16 @@ works::
# The main process gets a simple configuration which prints to the console. # The main process gets a simple configuration which prints to the console.
config_initial = { config_initial = {
'version': 1, 'version': 1,
'formatters': {
'detailed': {
'class': 'logging.Formatter',
'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
}
},
'handlers': { 'handlers': {
'console': { 'console': {
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'level': 'INFO', 'level': 'INFO'
}, }
}, },
'root': { 'root': {
'level': 'DEBUG', 'handlers': ['console'],
'handlers': ['console'] 'level': 'DEBUG'
}, }
} }
# The worker process configuration is just a QueueHandler attached to the # The worker process configuration is just a QueueHandler attached to the
# root logger, which allows all messages to be sent to the queue. # root logger, which allows all messages to be sent to the queue.
@ -1554,13 +1554,13 @@ works::
'handlers': { 'handlers': {
'queue': { 'queue': {
'class': 'logging.handlers.QueueHandler', 'class': 'logging.handlers.QueueHandler',
'queue': q, 'queue': q
}, }
}, },
'root': { 'root': {
'level': 'DEBUG', 'handlers': ['queue'],
'handlers': ['queue'] 'level': 'DEBUG'
}, }
} }
# The listener process configuration shows that the full flexibility of # The listener process configuration shows that the full flexibility of
# logging configuration is available to dispatch events to handlers however # logging configuration is available to dispatch events to handlers however
@ -1584,28 +1584,28 @@ works::
'handlers': { 'handlers': {
'console': { 'console': {
'class': 'logging.StreamHandler', 'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'simple', 'formatter': 'simple',
'level': 'INFO'
}, },
'file': { 'file': {
'class': 'logging.FileHandler', 'class': 'logging.FileHandler',
'filename': 'mplog.log', 'filename': 'mplog.log',
'mode': 'w', 'mode': 'w',
'formatter': 'detailed', 'formatter': 'detailed'
}, },
'foofile': { 'foofile': {
'class': 'logging.FileHandler', 'class': 'logging.FileHandler',
'filename': 'mplog-foo.log', 'filename': 'mplog-foo.log',
'mode': 'w', 'mode': 'w',
'formatter': 'detailed', 'formatter': 'detailed'
}, },
'errors': { 'errors': {
'class': 'logging.FileHandler', 'class': 'logging.FileHandler',
'filename': 'mplog-errors.log', 'filename': 'mplog-errors.log',
'mode': 'w', 'mode': 'w',
'level': 'ERROR',
'formatter': 'detailed', 'formatter': 'detailed',
}, 'level': 'ERROR'
}
}, },
'loggers': { 'loggers': {
'foo': { 'foo': {
@ -1613,9 +1613,9 @@ works::
} }
}, },
'root': { '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 # Log some initial events, just to show that logging in the parent works
# normally. # normally.