合并日志 新增自定义文件路径
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:collection';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
@@ -20,6 +21,7 @@ class LokiLogger implements Logger {
|
|||||||
final LoggerConfig? _config;
|
final LoggerConfig? _config;
|
||||||
final Dio dio;
|
final Dio dio;
|
||||||
LoggerLevel level = LoggerLevel.info;
|
LoggerLevel level = LoggerLevel.info;
|
||||||
|
final Queue<Map<String, dynamic>> _queue = Queue();
|
||||||
LokiLogger(this._config)
|
LokiLogger(this._config)
|
||||||
: dio = Dio(
|
: dio = Dio(
|
||||||
BaseOptions(baseUrl: _config == null ? "" : _config.host, headers: {
|
BaseOptions(baseUrl: _config == null ? "" : _config.host, headers: {
|
||||||
@@ -32,7 +34,49 @@ class LokiLogger implements Logger {
|
|||||||
print("logerr:###$error###")
|
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
|
@override
|
||||||
void debug(String msg,
|
void debug(String msg,
|
||||||
{String? tag,
|
{String? tag,
|
||||||
@@ -122,24 +166,8 @@ class LokiLogger implements Logger {
|
|||||||
if (lable != null) {
|
if (lable != null) {
|
||||||
lableMap.addAll(lable);
|
lableMap.addAll(lable);
|
||||||
}
|
}
|
||||||
var data = jsonEncode({
|
_queue
|
||||||
"streams": [
|
.add({"log": log, "lable": lableMap, "time": nanoseconds.toString()});
|
||||||
{
|
|
||||||
"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);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level == LoggerLevel.debug || _config.print) {
|
if (level == LoggerLevel.debug || _config.print) {
|
||||||
print(log);
|
print(log);
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ class StorageConfig {
|
|||||||
final String host;
|
final String host;
|
||||||
final int port;
|
final int port;
|
||||||
final bool ssl;
|
final bool ssl;
|
||||||
|
final String? path;
|
||||||
final String accessKey;
|
final String accessKey;
|
||||||
final String secretKey;
|
final String secretKey;
|
||||||
|
|
||||||
@@ -31,6 +32,7 @@ class StorageConfig {
|
|||||||
{required this.host,
|
{required this.host,
|
||||||
required this.port,
|
required this.port,
|
||||||
this.ssl = false,
|
this.ssl = false,
|
||||||
|
this.path,
|
||||||
required this.accessKey,
|
required this.accessKey,
|
||||||
required this.secretKey});
|
required this.secretKey});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,6 @@ class MinioStorage implements Storage {
|
|||||||
Future<String> uploadObject(
|
Future<String> uploadObject(
|
||||||
String bucketName, String objectName, Uint8List data) async {
|
String bucketName, String objectName, Uint8List data) async {
|
||||||
await _minio.putObject(bucketName, objectName, Stream.fromIterable([data]));
|
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";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user