6
6
7
7
import json_logging
8
8
import json_logging .framework
9
- from json_logging .framework_base import FrameworkConfigurator , AppRequestInstrumentationConfigurator , RequestAdapter , \
10
- ResponseAdapter
9
+ from json_logging .framework_base import (
10
+ FrameworkConfigurator ,
11
+ AppRequestInstrumentationConfigurator ,
12
+ RequestAdapter ,
13
+ ResponseAdapter ,
14
+ )
11
15
from json_logging .util import is_not_match_any_pattern
12
16
13
17
@@ -16,13 +20,13 @@ def is_sanic_present():
16
20
try :
17
21
# noinspection PyPackageRequirements
18
22
from sanic import Sanic
23
+
19
24
return True
20
25
except :
21
26
return False
22
27
23
28
24
29
class SanicAppConfigurator (FrameworkConfigurator ):
25
-
26
30
def config (self ):
27
31
if not is_sanic_present ():
28
32
raise RuntimeError ("Sanic is not available in system runtime" )
@@ -31,12 +35,16 @@ def config(self):
31
35
# noinspection PyPackageRequirements
32
36
from sanic .log import LOGGING_CONFIG_DEFAULTS
33
37
34
- LOGGING_CONFIG_DEFAULTS ['disable_existing_loggers' ] = False
35
- LOGGING_CONFIG_DEFAULTS ['formatters' ]['generic' ]['class' ] = "json_logging.JSONLogFormatter"
36
- del LOGGING_CONFIG_DEFAULTS ['formatters' ]['generic' ]['format' ]
38
+ LOGGING_CONFIG_DEFAULTS ["disable_existing_loggers" ] = False
39
+ LOGGING_CONFIG_DEFAULTS ["formatters" ]["generic" ][
40
+ "class"
41
+ ] = "json_logging.JSONLogFormatter"
42
+ del LOGGING_CONFIG_DEFAULTS ["formatters" ]["generic" ]["format" ]
37
43
38
- LOGGING_CONFIG_DEFAULTS ['formatters' ]['access' ]['class' ] = "json_logging.JSONLogFormatter"
39
- del LOGGING_CONFIG_DEFAULTS ['formatters' ]['access' ]['format' ]
44
+ LOGGING_CONFIG_DEFAULTS ["formatters" ]["access" ][
45
+ "class"
46
+ ] = "json_logging.JSONLogFormatter"
47
+ del LOGGING_CONFIG_DEFAULTS ["formatters" ]["access" ]["format" ]
40
48
41
49
# logging.config.dictConfig(LOGGING_CONFIG_DEFAULTS)
42
50
@@ -47,32 +55,34 @@ def config(self, app, exclude_url_patterns=[]):
47
55
raise RuntimeError ("Sanic is not available in system runtime" )
48
56
# noinspection PyPackageRequirements
49
57
from sanic import Sanic
58
+
50
59
if not isinstance (app , Sanic ):
51
60
raise RuntimeError ("app is not a valid Sanic.app.Sanic app instance" )
52
61
53
62
# noinspection PyAttributeOutsideInit
54
- self .request_logger = logging .getLogger (' sanic-request' )
63
+ self .request_logger = logging .getLogger (" sanic-request" )
55
64
56
- logging .getLogger (' sanic.access' ).disabled = True
65
+ logging .getLogger (" sanic.access" ).disabled = True
57
66
58
- @app .middleware (' request' )
67
+ @app .middleware (" request" )
59
68
def before_request (request ):
60
69
if is_not_match_any_pattern (request .path , exclude_url_patterns ):
61
70
request .ctx .request_info = json_logging .RequestInfo (request )
62
71
63
- @app .middleware (' response' )
72
+ @app .middleware (" response" )
64
73
def after_request (request , response ):
65
- if hasattr (request .ctx , ' request_info' ):
74
+ if hasattr (request .ctx , " request_info" ):
66
75
request_info = request .ctx .request_info
67
76
request_info .update_response_status (response )
68
- self .request_logger .info ("" , extra = {'request_info' : request_info , 'type' : 'request' })
77
+ self .request_logger .info (
78
+ "" , extra = {"request_info" : request_info , "type" : "request" }
79
+ )
69
80
70
81
def get_request_logger (self ):
71
82
return self .request_logger
72
83
73
84
74
85
class SanicRequestAdapter (RequestAdapter ):
75
-
76
86
@staticmethod
77
87
def get_current_request ():
78
88
raise NotImplementedError
@@ -85,6 +95,7 @@ def support_global_request_object():
85
95
@staticmethod
86
96
def get_request_class_type ():
87
97
from sanic .request import Request
98
+
88
99
return Request
89
100
90
101
def get_remote_user (self , request ):
@@ -118,14 +129,13 @@ def get_method(self, request):
118
129
return request .method
119
130
120
131
def get_remote_ip (self , request ):
121
- return request .ip [ 0 ]
132
+ return request .ip
122
133
123
134
def get_remote_port (self , request ):
124
135
return json_logging .EMPTY_VALUE
125
136
126
137
127
138
class SanicResponseAdapter (ResponseAdapter ):
128
-
129
139
def get_status_code (self , response ):
130
140
return response .status
131
141
0 commit comments