修改日志接口 新增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

@@ -27,29 +27,64 @@ class LokiLogger implements Logger {
"Content-Type": "application/json",
"Content-Encoding": "gzip"
}));
@override
void info(String msg, {String? tag}) {
log(msg, level: LoggerLevel.info, tag: tag);
void debug(String msg,
{String? tag, String? traceId, String? spanId, String? parentSpanId}) {
log(msg,
level: LoggerLevel.debug,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId);
}
@override
void warning(String msg, {String? tag}) {
log(msg, level: LoggerLevel.warning, tag: tag);
void info(String msg,
{String? tag, String? traceId, String? spanId, String? parentSpanId}) {
log(msg,
level: LoggerLevel.info,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId);
}
@override
void error(String msg, {String? tag}) {
log(msg, level: LoggerLevel.error, tag: tag);
void warning(String msg,
{String? tag, String? traceId, String? spanId, String? parentSpanId}) {
log(msg,
level: LoggerLevel.warning,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId);
}
void log(String msg, {required LoggerLevel level, String? tag}) {
@override
void error(String msg,
{String? tag, String? traceId, String? spanId, String? parentSpanId}) {
log(msg,
level: LoggerLevel.error,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId);
}
void log(String msg,
{required LoggerLevel level,
String? tag,
String? traceId,
String? spanId,
String? parentSpanId}) {
if (level.level < this.level.level) {
//日志等级小于设置的输出日志等级
return;
}
String log =
"traceId=$traceId, spanId=$spanId parentSpanId=$parentSpanId tag=$tag ${level.name.toUpperCase()} $msg";
if (_config == null) {
print("$tag $level $msg");
print(log);
} else {
//推送到loki服务器
//{_config.url}
@@ -59,14 +94,22 @@ class LokiLogger implements Logger {
var zip = gzip.encode(utf8.encode(jsonEncode({
"streams": [
{
"stream": {"service_name": _config.serviceName},
"stream": {
"service_name": _config.serviceName,
// "traceId": traceId,
// "spanId": spanId,
// "parentSpanId": parentSpanId
},
"values": [
[nanoseconds.toString(), "$tag ${level.name.toUpperCase()} $msg"]
[nanoseconds.toString(), log]
]
}
]
})));
dio.post("/loki/api/v1/push", data: zip);
if (level == LoggerLevel.debug) {
print(log);
}
}
}
}