Kaydet (Commit) 5b3cbcd4 authored tarafından Géry Ogam's avatar Géry Ogam Kaydeden (comit) Vinay Sajip

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

üst f6c8007a
...@@ -1456,12 +1456,18 @@ works:: ...@@ -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:: ...@@ -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:: ...@@ -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:: ...@@ -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:: ...@@ -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.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment