[3.12] gh-113421: Fix multiprocessing logger for "%(filename)s" (GH-113423) (GH-113450)

(cherry picked from commit ce77ee5035)

Co-authored-by: Xu Song <xusong.vip@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-12-24 11:23:32 +01:00 committed by GitHub
parent 269cb342ad
commit 1bff2fd013
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 4 deletions

View file

@ -4630,6 +4630,29 @@ class _TestLogging(BaseTestCase):
root_logger.setLevel(root_level)
logger.setLevel(level=LOG_LEVEL)
def test_filename(self):
logger = multiprocessing.get_logger()
original_level = logger.level
try:
logger.setLevel(util.DEBUG)
stream = io.StringIO()
handler = logging.StreamHandler(stream)
logging_format = '[%(levelname)s] [%(filename)s] %(message)s'
handler.setFormatter(logging.Formatter(logging_format))
logger.addHandler(handler)
logger.info('1')
util.info('2')
logger.debug('3')
filename = os.path.basename(__file__)
log_record = stream.getvalue()
self.assertIn(f'[INFO] [{filename}] 1', log_record)
self.assertIn(f'[INFO] [{filename}] 2', log_record)
self.assertIn(f'[DEBUG] [{filename}] 3', log_record)
finally:
logger.setLevel(original_level)
logger.removeHandler(handler)
handler.close()
# class _TestLoggingProcessName(BaseTestCase):
#