Skip to content

Commit 85d1be2

Browse files
committed
Fix server logs
1 parent 5a9981f commit 85d1be2

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

src/superannotate/lib/app/server/core.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ def index():
152152
def _dispatch_request(self, request):
153153
"""Dispatches the request."""
154154
adapter = self._url_map.bind_to_environ(request.environ)
155-
response = None
156-
content = None
157155
try:
158156
endpoint, values = adapter.match()
159157
view_func = self._view_function_map.get(endpoint)
@@ -171,34 +169,35 @@ def _dispatch_request(self, request):
171169
return response
172170
except HTTPException as e:
173171
return e
174-
finally:
175-
if "monitor" not in request.full_path and "log" not in request.full_path:
176-
data = {
177-
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
178-
"request": {
179-
"method": request.full_path,
180-
"path": request.url,
181-
"headers": dict(request.headers.items()),
182-
"data": request.data.decode("utf-8"),
183-
},
184-
"response": {
185-
"headers": dict(request.headers.items()) if response else None,
186-
# 'data': response.data.decode('utf-8') if response else None,
187-
"data": content.data.decode("utf-8")
188-
if isinstance(content, Response)
189-
else content,
190-
"status_code": response.status_code if response else None,
191-
},
192-
}
193-
# import ndjson
194-
print(11111, request.full_path)
195-
logger.info(json.dumps(data))
196172

197173
def wsgi_app(self, environ, start_response):
198174
"""WSGI application that processes requests and returns responses."""
199175
request = Request(environ)
200176
response = self._dispatch_request(request)
201-
return response(environ, start_response)
177+
return_value = response(environ, start_response)
178+
if not any(i in request.full_path for i in ("monitor", "logs")):
179+
data = {
180+
"date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
181+
"request": {
182+
"method": request.method,
183+
"path": request.url,
184+
"headers": dict(request.headers.items()),
185+
"data": request.data.decode("utf-8"),
186+
},
187+
"response": {
188+
"headers": dict(response.headers.items())
189+
if hasattr(response, "headers")
190+
else {},
191+
"data": response.data.decode("utf-8")
192+
if hasattr(response, "data")
193+
else response.description,
194+
"status_code": response.status_code
195+
if hasattr(response, "status_code")
196+
else response.code,
197+
},
198+
}
199+
logger.info(json.dumps(data))
200+
return return_value
202201

203202
def __call__(self, environ, start_response):
204203
"""The WSGI server calls this method as the WSGI application."""

src/superannotate/lib/app/server/default_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def logs(request):
4343
break
4444
response["data"] = []
4545
response["offset"] = cursor
46+
response["data"].reverse()
4647
return response
4748

4849

src/superannotate/logger.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def get_server_logger():
2121
logger.addHandler(stream_handler)
2222
try:
2323
log_file_path = os.path.join(constances.LOG_FILE_LOCATION, "sa_server.log")
24-
open(log_file_path, "w").close()
2524
if os.access(log_file_path, os.W_OK):
2625
file_handler = RotatingFileHandler(
2726
log_file_path,

0 commit comments

Comments
 (0)