8000 Improved the more elaborate multiprocessing example in the logging co… · python/cpython@5b3cbcd · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b3cbcd

Browse files
geryogamvsajip
authored andcommitted
Improved the more elaborate multiprocessing example in the logging cookbook (GH-9326)
1 parent f6c8007 commit 5b3cbcd

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

Doc/howto/logging-cookbook.rst

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,12 +1456,18 @@ works::
14561456
which then get dispatched, by the logging system, to the handlers
14571457
configured for those loggers.
14581458
"""
1459+
14591460
def handle(self, record):
1460-
logger = logging.getLogger(record.name)
1461-
# The process name is transformed just to show that it's the listener
1462-
# doing the logging to files and console
1463-
record.processName = '%s (for %s)' % (current_process().name, record.processName)
1464-
logger.handle(record)
1461+
if record.name == "root":
1462+
logger = logging.getLogger()
1463+
else:
1464+
logger = logging.getLogger(record.name)
1465+
1466+
if logger.isEnabledFor(record.levelno):
1467+
# The process name is transformed just to show that it's the listener
1468+
# doing the logging to files and console
1469+
record.processName = '%s (for %s)' % (current_process().name, record.processName)
1470+
logger.handle(record)
14651471

14661472
def listener_process(q, stop_event, config):
14671473
"""
@@ -1526,22 +1532,16 @@ works::
15261532
# The main process gets a simple configuration which prints to the console.
15271533
config_initial = {
15281534
'version': 1,
1529-
'formatters': {
1530-
'detailed': {
1531-
'class': 'logging.Formatter',
1532-
'format': '%(asctime)s %(name)-15s %(levelname)-8s %(processName)-10s %(message)s'
1533-
}
1534-
},
15351535
'handlers': {
15361536
'console': {
15371537
'class': 'logging.StreamHandler',
1538-
'level': 'INFO',
1539-
},
1538+
'level': 'INFO'
1539+
}
15401540
},
15411541
'root': {
1542-
'level': 'DEBUG',
1543-
'handlers': ['console']
1544-
},
1542+
'handlers': ['console'],
1543+
'level': 'DEBUG'
1544+
}
15451545
}
15461546
# The worker process configuration is just a QueueHandler attached to the
15471547
# root logger, which allows all messages to be sent to the queue.
@@ -1554,13 +1554,13 @@ works::
15541554
'handlers': {
15551555
'queue': {
15561556
'class': 'logging.handlers.QueueHandler',
1557-
'queue': q,
1558-
},
1557+
'queue': q
1558+
}
15591559
},
15601560
'root': {
1561-
'level': 'DEBUG',
1562-
'handlers': ['queue']
1563-
},
1561+
'handlers': ['queue'],
1562+
'level': 'DEBUG'
1563+
}
15641564
}
15651565
# The listener process configuration shows that the full flexibility of
15661566
# logging configuration is available to dispatch events to handlers however
@@ -1584,38 +1584,38 @@ works::
15841584
'handlers': {
15851585
'console': {
15861586
'class': 'logging.StreamHandler',
1587-
'level': 'INFO',
15881587
'formatter': 'simple',
1588+
'level': 'INFO'
15891589
},
15901590
'file': {
15911591
'class': 'logging.FileHandler',
15921592
'filename': 'mplog.log',
15931593
'mode': 'w',
1594-
'formatter': 'detailed',
1594+
'formatter': 'detailed'
15951595
},
15961596
'foofile': {
15971597
'class': 'logging.FileHandler',
15981598
'filename': 'mplog-foo.log',
15991599
'mode': 'w',
1600-
'formatter': 'detailed',
1600+
'formatter': 'detailed'
16011601
},
16021602
'errors': {
16031603
'class': 'logging.FileHandler',
16041604
'filename': 'mplog-errors.log',
16051605
'mode': 'w',
1606-
'level': 'ERROR',
16071606
'formatter': 'detailed',
1608-
},
1607+
'level': 'ERROR'
1608+
}
16091609
},
16101610
'loggers': {
16111611
'foo': {
16121612
'handlers': ['foofile']
16131613
}
16141614
},
16151615
'root': {
1616-
'level': 'DEBUG',
1617-
'handlers': ['console', 'file', 'errors']
1618-
},
1616+
'handlers': ['console', 'file', 'errors'],
1617+
'level': 'DEBUG'
1618+
}
16191619
}
16201620
# Log some initial events, just to show that logging in the parent works
16211621
# normally.

0 commit comments

Comments
 (0)
0