日志初始配置添加自定义标签

This commit is contained in:
2025-09-09 15:32:40 +08:00
parent 2d46fcaec3
commit 8076acc373
4 changed files with 25 additions and 13 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:EasyDartModule/base/logger/Logger.dart'; import 'package:EasyDartModule/base/logger/Logger.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:shelf/shelf.dart' as sf; import 'package:shelf/shelf.dart' as sf;
@@ -41,8 +43,11 @@ class TraceDio {
options.headers['X-Span-ID'] = spanId; options.headers['X-Span-ID'] = spanId;
} }
// 记录请求日志 // 记录请求日志
_logger?.info('发送请求: ${options.method} ${options.uri}', _logger?.info(
tag: "DIO", traceId: traceId, spanId: spanId); '发送请求: ${options.method} ${options.uri} ${options.data != null ? jsonEncode(options.data) : ""}',
tag: "DIO",
traceId: traceId,
spanId: spanId);
return handler.next(options); // 继续请求 return handler.next(options); // 继续请求
}, },
onResponse: (response, handler) { onResponse: (response, handler) {

View File

@@ -38,7 +38,11 @@ abstract class Logger {
class LoggerConfig { class LoggerConfig {
String host; String host;
String serviceName; String serviceName;
Map<String, String>? lables;
bool print; bool print;
LoggerConfig( LoggerConfig(
{required this.host, required this.serviceName, this.print = false}); {required this.host,
required this.serviceName,
this.print = false,
this.lables});
} }

View File

@@ -50,13 +50,13 @@ class LokiLogger implements Logger {
var lable = data["lable"]; var lable = data["lable"];
var time = data["time"]; var time = data["time"];
//合并日志 //合并日志
if (lable.length == 1) { // if (lable.length == 1) {
//只有默认标签 //只有默认标签
labels = lable; labels = lable;
logs.add([time, log]); logs.add([time, log]);
} else { // } else {
//有多个标签按照不同标签进行合并 //有多个标签按照不同标签进行合并
} // }
} }
if (labels != null) { if (labels != null) {
var data = jsonEncode({ var data = jsonEncode({
@@ -162,7 +162,12 @@ class LokiLogger implements Logger {
var now = DateTime.now(); var now = DateTime.now();
// 转换为纳秒 // 转换为纳秒
int nanoseconds = now.microsecondsSinceEpoch * 1000; int nanoseconds = now.microsecondsSinceEpoch * 1000;
var lableMap = {"service_name": _config.serviceName}; var lableMap = {
"service_name": _config.serviceName,
};
if (_config.lables != null) {
lableMap.addAll(_config.lables!);
}
if (lable != null) { if (lable != null) {
lableMap.addAll(lable); lableMap.addAll(lable);
} }

View File

@@ -1,5 +1,3 @@
import 'dart:typed_data';
import 'package:redis/redis.dart'; import 'package:redis/redis.dart';
class Redis { class Redis {