-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add support for JSON formatted logs #5799
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1929fb5
Add support for JSON formatted logs
mriedem b617d91
Check for json log formatting in log_request
mriedem ca95490
Add log_json config option with default and validation handling
mriedem 6a74393
Pass NotebookApp logger to log_request
mriedem 3e32ce5
Cleanup for log_json review
mriedem 47e06b0
Log request properties separately when log_json=True
mriedem 8b06db3
Address review comments
mriedem fac2285
Fix logging in _validate_log_json
mriedem 093aee2
Add some basic tests for log_json config
mriedem 08f8ccd
Add log_json=True test for log_request
mriedem 4ee4bb2
Use unittest assertion methods for improved logging
mriedem 6a883e0
Fix NotebookAppJSONLoggingTests.test_log_json_enabled
mriedem 67ad5a1
Merge remote-tracking branch 'origin/master' into 5798-json-logging
mriedem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from notebook import log | ||
|
|
||
|
|
||
| class TestLogRequest(unittest.TestCase): | ||
|
|
||
| @mock.patch('notebook.log.prometheus_log_method') | ||
| def test_log_request_json(self, mock_prometheus): | ||
| headers = {'Referer': 'test'} | ||
| request = mock.Mock( | ||
| request_time=mock.Mock(return_value=1), | ||
| headers=headers, | ||
| method='GET', | ||
| remote_ip='1.2.3.4', | ||
| uri='/notebooks/foo/bar' | ||
| ) | ||
| handler = mock.MagicMock( | ||
| request=request, | ||
| get_status=mock.Mock(return_value=500) | ||
| ) | ||
| logger = mock.MagicMock() | ||
| log.log_request(handler, log=logger, log_json=True) | ||
| # Since the status was 500 there should be two calls to log.error, | ||
| # one with the request headers and another with the other request | ||
| # parameters. | ||
| self.assertEqual(2, logger.error.call_count) | ||
| logger.error.assert_has_calls([ | ||
| mock.call("", extra=dict(props=dict(headers))), | ||
| mock.call("", extra=dict(props={ | ||
| 'status': handler.get_status(), | ||
| 'method': request.method, | ||
| 'ip': request.remote_ip, | ||
| 'uri': request.uri, | ||
| 'request_time': 1000.0, | ||
| 'referer': headers['Referer'] | ||
| })) | ||
| ]) | ||
| mock_prometheus.assert_called_once_with(handler) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.