加入 连接状态判断 控制台日志输出

This commit is contained in:
2026-02-05 11:39:27 +08:00
parent f772e16119
commit 9e3a7d5721
7 changed files with 134 additions and 15 deletions

View File

@@ -9,6 +9,8 @@ abstract class DataBase {
_dataBase = database;
}
bool isConnected();
// 执行查询操作
Future<List<Map<String, dynamic>>> query(String table, {dynamic condition});

View File

@@ -10,6 +10,10 @@ class MongoDb implements DataBase {
connect(false);
}
bool isConnected() {
return db.isConnected;
}
void connect(reconnect) {
if (reconnect) {
print("尝试重连MongoDb");

View File

@@ -36,13 +36,33 @@ abstract class Logger {
}
class LoggerConfig {
LoggerType type;
String host;
String serviceName;
Map<String, String>? lables;
bool print;
LoggerConfig(
{required this.host,
required this.serviceName,
{this.host = "",
this.serviceName = "",
this.type = LoggerType.LOKI,
this.print = false,
this.lables});
}
enum LoggerType {
LOKI,
CONSOLSE,
;
}
enum LoggerLevel {
debug(1),
info(2),
warning(3),
error(4),
off(5),
;
final int level;
const LoggerLevel(this.level);
}

View File

@@ -0,0 +1,93 @@
import 'package:EasyDartModule/base/logger/Logger.dart';
import 'package:intl/intl.dart';
class ConsoleLogger implements Logger {
final LoggerConfig _config;
LoggerLevel level = LoggerLevel.info;
ConsoleLogger(this._config);
@override
void debug(String msg,
{String? tag,
String? traceId,
String? spanId,
String? parentSpanId,
Map<String, String>? lable}) {
log(msg,
level: LoggerLevel.debug,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId,
lable: lable);
}
@override
void error(String msg,
{String? tag,
String? traceId,
String? spanId,
String? parentSpanId,
Map<String, String>? lable}) {
log(msg,
level: LoggerLevel.error,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId,
lable: lable);
}
@override
void info(String msg,
{String? tag,
String? traceId,
String? spanId,
String? parentSpanId,
Map<String, String>? lable}) {
log(msg,
level: LoggerLevel.info,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId,
lable: lable);
}
@override
void warning(String msg,
{String? tag,
String? traceId,
String? spanId,
String? parentSpanId,
Map<String, String>? lable}) {
log(msg,
level: LoggerLevel.warning,
tag: tag,
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId,
lable: lable);
}
void log(String msg,
{required LoggerLevel level,
String? tag,
String? traceId,
String? spanId,
String? parentSpanId,
Map<String, String>? lable}) {
if (level.level < this.level.level) {
//日志等级小于设置的输出日志等级
return;
}
String time = DateFormat("yyyy-MM-dd HH:mm:ss").format(DateTime.now().toUtc().add(Duration(hours: 8)));
String log =
"$time traceId=$traceId, spanId=$spanId parentSpanId=$parentSpanId tag=$tag ${level.name.toUpperCase()} $msg";
if (level == LoggerLevel.debug || _config.print) {
print(log);
}
}
}

View File

@@ -5,18 +5,6 @@ import 'dart:io';
import 'package:EasyDartModule/base/logger/Logger.dart';
import 'package:dio/dio.dart';
enum LoggerLevel {
debug(1),
info(2),
warning(3),
error(4),
off(5),
;
final int level;
const LoggerLevel(this.level);
}
class LokiLogger implements Logger {
final LoggerConfig? _config;
final Dio dio;

View File

@@ -8,6 +8,7 @@ import 'package:typed_data/src/typed_buffer.dart';
class Mqtt {
final MqttConfig _config;
MqttClient? _client;
bool _connected = false;
//标记是否自动重连
bool reconnect = true;
//缓存自定义主题订阅信息
@@ -26,10 +27,12 @@ class Mqtt {
// _client?.autoReconnect = true;
_client?.onConnected = () {
// print("aa:mqtt服务器连接成功");
_connected = true;
print("mqtt服务器: ${_config.host} 连接成功");
};
_client?.onDisconnected = () {
// print("aa:mqtt服务器连接断开");
_connected = false;
print("mqtt服务器: ${_config.host} 连接断开");
//执行重连操作
if (reconnect) {
@@ -48,6 +51,10 @@ class Mqtt {
_mqtt = server;
}
bool isConnected() {
return this._connected;
}
Future<bool> connect({bool reconnect = false}) async {
if (reconnect) {
//限制重连速率