Files
easy_dart_module/lib/base/logger/impl/ConsoleLogger.dart

94 lines
2.2 KiB
Dart

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);
}
}
}