修改日志接口 新增redis

This commit is contained in:
qmqz
2025-01-07 16:50:57 +08:00
parent fd62ed3d98
commit b2fe531919
6 changed files with 161 additions and 45 deletions

View File

@@ -28,9 +28,11 @@ class ShelfWebServer implements WebServer {
final requestSpanId = request.context['request_span_id'] as String;
final requestParentSpanId =
request.context['request_parent_span_id'] as String?;
final String logPer =
"traceId=$requestId spanId=$requestSpanId parentSpanId=$requestParentSpanId";
logger.info('$logPer | 请求路径: ${request.requestedUri}', tag: tag);
logger.info('| 请求路径: ${request.requestedUri}',
tag: tag,
traceId: requestId,
spanId: requestSpanId,
parentSpanId: requestParentSpanId);
// final stopwatch = Stopwatch()..start();
final stopwatch = request.context["request_stop_watch"] as Stopwatch;
stopwatch.start();
@@ -39,18 +41,25 @@ class ShelfWebServer implements WebServer {
response = await innerHandler(request);
stopwatch.stop();
logger.info(
'$logPer | 响应状态码: ${response.statusCode} | 响应时间: ${stopwatch.elapsedMilliseconds}ms',
tag: tag);
'| 响应状态码: ${response.statusCode} | 响应时间: ${stopwatch.elapsedMilliseconds}ms',
tag: tag,
traceId: requestId,
spanId: requestSpanId,
parentSpanId: requestParentSpanId);
} catch (e, s) {
if (e is HijackException) {
//不能处理该异常直接抛出
throw e;
}
stopwatch.stop();
logger.error("$logPer | 服务器错误 | ${e.toString()} ${s.toString()}",
tag: tag);
logger.error("| 服务器错误 | ${e.toString()} ${s.toString()}",
tag: tag,
traceId: requestId,
spanId: requestSpanId,
parentSpanId: requestParentSpanId);
response = Response(500,
body: "Internal Server Error\r\nTraceId:$requestId");
body:
"Internal Server Error\r\nTraceId:$requestId\r\nSpanId:$requestSpanId");
}
return response;
};
@@ -92,7 +101,7 @@ class ShelfWebServer implements WebServer {
_wsCall[path]!.open(spanId);
// 监听 WebSocket 消息
channel.stream.listen((message) {
print('Received message: $message');
// print('Received message: $message');
// 处理消息并回应客户端
// channel.sink.add(message);
_wsCall[path]!.message(spanId, message);
@@ -107,12 +116,16 @@ class ShelfWebServer implements WebServer {
final stopwatch = request.context["request_stop_watch"] as Stopwatch;
stopwatch.stop();
logger.info(
'traceId=$requestId spanId=$spanId parentSpanId=$requestParentSpanId | WebSocket closed | 连接时长: ${stopwatch.elapsedMilliseconds}ms',
tag: "webserver");
'parentSpanId=$requestParentSpanId | WebSocket closed | 连接时长: ${stopwatch.elapsedMilliseconds}ms',
tag: "webserver",
traceId: requestId,
spanId: spanId);
}, onError: (error) {
logger.info(
'traceId=$requestId spanId=$spanId parentSpanId=$requestParentSpanId | WebSocket error: $error',
tag: "webserver");
'parentSpanId=$requestParentSpanId | WebSocket error: $error',
tag: "webserver",
traceId: requestId,
spanId: spanId);
});
})(request);
} else {