-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
GH-113421: Fix multiprocessing logger #113423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
772a742
0c71144
493414b
5313957
f42181a
9705a0b
04509a7
a49b399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| 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([ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
| # | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix multiprocessing logger for ``%(filename)s``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
original_levelmight be a more proper name for this. It does not have to be the default level.