From 700479d131b6230d87aebae94ea610ca3a8decfc Mon Sep 17 00:00:00 2001 From: qmqz Date: Sat, 12 Apr 2025 16:00:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E6=97=A5=E5=BF=97=20=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/base/logger/impl/LokiLogger.dart | 64 ++++++++++++++++++------- lib/base/storage/Storage.dart | 2 + lib/base/storage/impl/MinIoStorage.dart | 2 +- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/lib/base/logger/impl/LokiLogger.dart b/lib/base/logger/impl/LokiLogger.dart index e840414..722ee02 100644 --- a/lib/base/logger/impl/LokiLogger.dart +++ b/lib/base/logger/impl/LokiLogger.dart @@ -1,3 +1,4 @@ +import 'dart:collection'; import 'dart:convert'; import 'dart:io'; @@ -20,6 +21,7 @@ class LokiLogger implements Logger { final LoggerConfig? _config; final Dio dio; LoggerLevel level = LoggerLevel.info; + final Queue> _queue = Queue(); LokiLogger(this._config) : dio = Dio( BaseOptions(baseUrl: _config == null ? "" : _config.host, headers: { @@ -32,7 +34,49 @@ class LokiLogger implements Logger { print("logerr:###$error###") }, )); + pushLog(); } + + void pushLog() { + Future.delayed(Duration(seconds: 3), () { + //向服务器推送日志 + var len = _queue.length; + List logs = []; + var labels; + while (len != 0) { + var data = _queue.removeFirst(); + len--; + var log = data["log"]; + var lable = data["lable"]; + var time = data["time"]; + //合并日志 + if (lable.length == 1) { + //只有默认标签 + labels = lable; + logs.add([time, log]); + } else { + //有多个标签按照不同标签进行合并 + } + } + if (labels != null) { + var data = jsonEncode({ + "streams": [ + {"stream": labels, "values": logs} + ] + }); + //判断平台 + if (identical(0, 0.0)) { + dio.post("/loki/api/v1/push", data: data); + } else { + var zip = gzip.encode(utf8.encode(data)); + dio.post("/loki/api/v1/push", data: zip); + } + } + + pushLog(); + }); + } + @override void debug(String msg, {String? tag, @@ -122,24 +166,8 @@ class LokiLogger implements Logger { if (lable != null) { lableMap.addAll(lable); } - var data = jsonEncode({ - "streams": [ - { - "stream": lableMap, - "values": [ - [nanoseconds.toString(), log] - ] - } - ] - }); - //判断平台 - - if (identical(0, 0.0)) { - dio.post("/loki/api/v1/push", data: data); - } else { - var zip = gzip.encode(utf8.encode(data)); - dio.post("/loki/api/v1/push", data: zip); - } + _queue + .add({"log": log, "lable": lableMap, "time": nanoseconds.toString()}); if (level == LoggerLevel.debug || _config.print) { print(log); diff --git a/lib/base/storage/Storage.dart b/lib/base/storage/Storage.dart index 72270df..a20d37d 100644 --- a/lib/base/storage/Storage.dart +++ b/lib/base/storage/Storage.dart @@ -24,6 +24,7 @@ class StorageConfig { final String host; final int port; final bool ssl; + final String? path; final String accessKey; final String secretKey; @@ -31,6 +32,7 @@ class StorageConfig { {required this.host, required this.port, this.ssl = false, + this.path, required this.accessKey, required this.secretKey}); } diff --git a/lib/base/storage/impl/MinIoStorage.dart b/lib/base/storage/impl/MinIoStorage.dart index 962a254..671b0b8 100644 --- a/lib/base/storage/impl/MinIoStorage.dart +++ b/lib/base/storage/impl/MinIoStorage.dart @@ -49,6 +49,6 @@ class MinioStorage implements Storage { Future uploadObject( String bucketName, String objectName, Uint8List data) async { await _minio.putObject(bucketName, objectName, Stream.fromIterable([data])); - return "http${_config.ssl ? "s" : ""}://${_config.host}:${_config.port}/$bucketName/$objectName"; + return "${_config.path ?? 'http${_config.ssl ? "s" : ""}://${_config.host}:${_config.port}'}/$bucketName/$objectName"; } }