File: log.py

package info (click to toggle)
jupyter-notebook 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,444 kB
  • sloc: javascript: 23,377; python: 15,064; makefile: 252; sh: 66
file content (56 lines) | stat: -rw-r--r-- 1,981 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#-----------------------------------------------------------------------------
#  Copyright (c) Jupyter Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file LICENSE, distributed as part of this software.
#-----------------------------------------------------------------------------

import json
from tornado.log import access_log
from .prometheus.log_functions import prometheus_log_method


def log_request(handler, log=access_log, log_json=False):
    """log a bit more information about each request than tornado's default
    
    - move static file get success to debug-level (reduces noise)
    - get proxied IP instead of proxy IP
    - log referer for redirect and failed requests
    - log user-agent for failed requests
    """
    status = handler.get_status()
    request = handler.request
    if status < 300 or status == 304:
        # Successes (or 304 FOUND) are debug-level
        log_method = log.debug
    elif status < 400:
        log_method = log.info
    elif status < 500:
        log_method = log.warning
    else:
        log_method = log.error
    
    request_time = 1000.0 * request.request_time()
    ns = dict(
        status=status,
        method=request.method,
        ip=request.remote_ip,
        uri=request.uri,
        request_time=float('%.2f' % request_time),
    )
    msg = "{status} {method} {uri} ({ip}) {request_time:f}ms"
    if status >= 400:
        # log bad referers
        ns['referer'] = request.headers.get('Referer', 'None')
        msg = msg + ' referer={referer}'
    if status >= 500 and status != 502:
        # log all headers if it caused an error
        if log_json:
            log_method("", extra=dict(props=dict(request.headers)))
        else:
            log_method(json.dumps(dict(request.headers), indent=2))
    if log_json:
        log_method("", extra=dict(props=ns))
    else:
        log_method(msg.format(**ns))
    prometheus_log_method(handler)