Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/multiprocessing/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@

def sub_debug(msg, *args):
if _logger:
_logger.log(SUBDEBUG, msg, *args)
_logger.log(SUBDEBUG, msg, *args, stacklevel=2)

def debug(msg, *args):
if _logger:
_logger.log(DEBUG, msg, *args)
_logger.log(DEBUG, msg, *args, stacklevel=2)

def info(msg, *args):
if _logger:
_logger.log(INFO, msg, *args)
_logger.log(INFO, msg, *args, stacklevel=2)

def sub_warning(msg, *args):
if _logger:
_logger.log(SUBWARNING, msg, *args)
_logger.log(SUBWARNING, msg, *args, stacklevel=2)

def get_logger():
'''
Expand Down
34 changes: 34 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4724,6 +4724,40 @@ def test_level(self):
root_logger.setLevel(root_level)
logger.setLevel(level=LOG_LEVEL)

def assert_log_lines(self, expected_values, stream=None):
stream = stream or self.stream
actual_lines = stream.getvalue().splitlines()
self.assertEqual(len(actual_lines), len(expected_values))
for actual, expected in zip(actual_lines, expected_values):
self.assertEqual(actual, expected)
s = stream.read()
if s:
self.fail("Remaining output at end of log stream:\n" + s)

def test_filename(self):
logger = multiprocessing.get_logger()
default_level = logger.level
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think original_level might be a more proper name for this. It does not have to be the default 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__)
self.assert_log_lines([
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a utility function might be a bit of an overkill here. Your check is very thorough but it might be too strict. I think something like

output = stream.getvalue()
self.assertIn(f'[INFO] [{filename}] 1', output)
... 

should be good enough here. It's clear enough for people about what it's doing, and it's testing against the core issue this PR solves. Making sure the output matches exactly what's expected is not necessarily the best way to do it (especially if it makes the code harder to understand).

f'[INFO] [{filename}] 1',
f'[INFO] [{filename}] 2',
f'[DEBUG] [{filename}] 3',
], stream)
finally:
logger.setLevel(default_level)
logger.removeHandler(handler)
handler.close()


# class _TestLoggingProcessName(BaseTestCase):
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix multiprocessing logger for ``%(filename)s``.