66
77import json_logging
88import 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+ )
1115from json_logging .util import is_not_match_any_pattern
1216
1317
@@ -16,13 +20,13 @@ def is_sanic_present():
1620 try :
1721 # noinspection PyPackageRequirements
1822 from sanic import Sanic
23+
1924 return True
2025 except :
2126 return False
2227
2328
2429class SanicAppConfigurator (FrameworkConfigurator ):
25-
2630 def config (self ):
2731 if not is_sanic_present ():
2832 raise RuntimeError ("Sanic is not available in system runtime" )
@@ -31,12 +35,16 @@ def config(self):
3135 # noinspection PyPackageRequirements
3236 from sanic .log import LOGGING_CONFIG_DEFAULTS
3337
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" ]
3743
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" ]
4048
4149 # logging.config.dictConfig(LOGGING_CONFIG_DEFAULTS)
4250
@@ -47,32 +55,34 @@ def config(self, app, exclude_url_patterns=[]):
4755 raise RuntimeError ("Sanic is not available in system runtime" )
4856 # noinspection PyPackageRequirements
4957 from sanic import Sanic
58+
5059 if not isinstance (app , Sanic ):
5160 raise RuntimeError ("app is not a valid Sanic.app.Sanic app instance" )
5261
5362 # noinspection PyAttributeOutsideInit
54- self .request_logger = logging .getLogger (' sanic-request' )
63+ self .request_logger = logging .getLogger (" sanic-request" )
5564
56- logging .getLogger (' sanic.access' ).disabled = True
65+ logging .getLogger (" sanic.access" ).disabled = True
5766
58- @app .middleware (' request' )
67+ @app .middleware (" request" )
5968 def before_request (request ):
6069 if is_not_match_any_pattern (request .path , exclude_url_patterns ):
6170 request .ctx .request_info = json_logging .RequestInfo (request )
6271
63- @app .middleware (' response' )
72+ @app .middleware (" response" )
6473 def after_request (request , response ):
65- if hasattr (request .ctx , ' request_info' ):
74+ if hasattr (request .ctx , " request_info" ):
6675 request_info = request .ctx .request_info
6776 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+ )
6980
7081 def get_request_logger (self ):
7182 return self .request_logger
7283
7384
7485class SanicRequestAdapter (RequestAdapter ):
75-
7686 @staticmethod
7787 def get_current_request ():
7888 raise NotImplementedError
@@ -85,6 +95,7 @@ def support_global_request_object():
8595 @staticmethod
8696 def get_request_class_type ():
8797 from sanic .request import Request
98+
8899 return Request
89100
90101 def get_remote_user (self , request ):
@@ -118,14 +129,13 @@ def get_method(self, request):
118129 return request .method
119130
120131 def get_remote_ip (self , request ):
121- return request .ip [ 0 ]
132+ return request .ip
122133
123134 def get_remote_port (self , request ):
124135 return json_logging .EMPTY_VALUE
125136
126137
127138class SanicResponseAdapter (ResponseAdapter ):
128-
129139 def get_status_code (self , response ):
130140 return response .status
131141
0 commit comments