Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb617d91

Browse files
committed
Check for json log formatting in log_request
This gets us closer to logging parts of the requestas separate json fields but not all the way there.For example:```json{"written_at": "2020-10-07T21:52:16.975Z", "written_ts": 1602107536975497000, "msg": "{'status': 404, 'method': 'GET', 'ip': '127.0.0.1', 'uri': '/nbextensions/widgets/notebook/js/extension.js', 'request_time': 1.9807815551757812, 'referer': 'http://localhost:8888/notebooks/test.ipynb'}", "type": "log", "logger": "NotebookApp", "thread": "MainThread", "level": "WARNING", "module": "log", "line_no": 54}```
1 parent1929fb5 commitb617d91

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

‎notebook/log.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
fromtornado.logimportaccess_log
1010
from .prometheus.log_functionsimportprometheus_log_method
1111

12+
from .utilsimportenable_json_logs
13+
14+
_enable_json_logs=enable_json_logs()
15+
1216

1317
deflog_request(handler):
1418
"""log a bit more information about each request than tornado's default
@@ -46,5 +50,11 @@ def log_request(handler):
4650
ifstatus>=500andstatus!=502:
4751
# log all headers if it caused an error
4852
log_method(json.dumps(dict(request.headers),indent=2))
49-
log_method(msg.format(**ns))
53+
if_enable_json_logs:
54+
# FIXME: this still logs the msg as a serialized json string,
55+
# presumably because it's using tornado's access_log rather than
56+
# the logger setup in notebook app with _log_formatter_cls.
57+
log_method(ns)
58+
else:
59+
log_method(msg.format(**ns))
5060
prometheus_log_method(handler)

‎notebook/notebookapp.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
from ._tzimportutcnow,utcfromtimestamp
114114
from .utilsimport (
115115
check_pid,
116+
enable_json_logs,
116117
pathname2url,
117118
run_sync,
118119
unix_socket_in_use,
@@ -139,17 +140,17 @@
139140
terminado_available=False
140141

141142
# Tolerate missing json_logging package.
142-
enable_json_logs=os.getenv('ENABLE_JSON_LOGGING','false').lower()=='true'
143+
_enable_json_logs=enable_json_logs()
143144
try:
144145
importjson_logging
145146
exceptImportError:
146147
# If configured for json logs and we can't do it, log a hint.
147-
ifenable_json_logs:
148+
if_enable_json_logs:
148149
logging.getLogger(__name__).exception(
149150
'Unable to use json logging due to missing packages. '
150151
'Run "pip install notebook[json-logging]" to fix.'
151152
)
152-
enable_json_logs=False
153+
_enable_json_logs=False
153154

154155
#-----------------------------------------------------------------------------
155156
# Module globals
@@ -717,7 +718,7 @@ class NotebookApp(JupyterApp):
717718

718719
# Unless there is a way to re-initialize the log formatter (like with _log_format_changed?)
719720
# we need to load the json logging formatter early here otherwise traitlets complains.
720-
_log_formatter_cls=json_logging.JSONLogFormatterifenable_json_logselseLogFormatter
721+
_log_formatter_cls=json_logging.JSONLogFormatterif_enable_json_logselseLogFormatter
721722

722723
@default('log_level')
723724
def_default_log_level(self):

‎notebook/utils.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,7 @@ def unix_socket_in_use(socket_path):
401401
returnTrue
402402
finally:
403403
sock.close()
404+
405+
406+
defenable_json_logs():
407+
returnos.getenv('ENABLE_JSON_LOGGING','false').lower()=='true'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp