petsHero-AI/lib/core/log/app_logger.dart

49 lines
1.6 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import '../api/api_config.dart';
/// 统一应用日志,提升可读性:时间戳、级别、标签、格式化输出。
///
/// [Logger] 默认 [DevelopmentFilter] 依赖 `assert`,在 release/profile 下会屏蔽**全部**日志。
/// Release 全量日志:`flutter run --release --dart-define=APP_LOG_LEVEL=trace`(见 [ApiConfig.debugLogs])。
/// 此时使用 [ProductionFilter]debug/info 可见。
///
/// 使用示例:
/// final _log = AppLogger('GenerateVideo');
/// _log.d('task: $task');
/// _log.e('Generate failed', e, st);
class AppLogger {
AppLogger([this.tag = 'App']);
final String tag;
static Logger? _logger;
static Logger get _instance {
_logger ??= Logger(
filter: (!kDebugMode && ApiConfig.debugLogs)
? ProductionFilter()
: DevelopmentFilter(),
printer: PrettyPrinter(
methodCount: 0,
errorMethodCount: 6,
lineLength: 80,
colors: true,
printEmojis: true,
dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
),
level: (kDebugMode || ApiConfig.debugLogs) ? Level.trace : Level.warning,
);
return _logger!;
}
String _msg(Object? message) => '[$tag] $message';
void d(Object? message) => _instance.d(_msg(message));
void i(Object? message) => _instance.i(_msg(message));
void w(Object? message) => _instance.w(_msg(message));
void e(Object? message, [Object? error, StackTrace? stackTrace]) =>
_instance.e(_msg(message), error: error, stackTrace: stackTrace);
}