2026-04-01 14:46:47 +08:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'log_config.dart';
|
|
|
|
|
import 'log_entry.dart';
|
2026-04-01 14:17:23 +08:00
|
|
|
import 'log_service.dart';
|
|
|
|
|
|
|
|
|
|
LogService? _globalLogService;
|
|
|
|
|
|
|
|
|
|
class Logger {
|
|
|
|
|
final String module;
|
2026-04-01 14:46:47 +08:00
|
|
|
final LogService? _service;
|
|
|
|
|
final bool _isNoOp;
|
2026-04-01 14:17:23 +08:00
|
|
|
|
2026-04-01 14:46:47 +08:00
|
|
|
Logger(this.module, this._service) : _isNoOp = _service == null;
|
2026-04-01 14:17:23 +08:00
|
|
|
|
|
|
|
|
factory Logger.get(String module) {
|
2026-04-01 14:46:47 +08:00
|
|
|
return Logger(module, _globalLogService);
|
2026-04-01 14:17:23 +08:00
|
|
|
}
|
2026-04-01 14:36:09 +08:00
|
|
|
|
|
|
|
|
static void setLogService(LogService service) {
|
|
|
|
|
_globalLogService = service;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 14:46:47 +08:00
|
|
|
static LogService? _ensureService() {
|
|
|
|
|
return _globalLogService;
|
2026-04-01 14:36:09 +08:00
|
|
|
}
|
2026-04-01 14:17:23 +08:00
|
|
|
|
|
|
|
|
void debug({
|
|
|
|
|
required String message,
|
|
|
|
|
Map<String, dynamic>? extra,
|
|
|
|
|
StackTrace? stackTrace,
|
2026-04-01 14:46:47 +08:00
|
|
|
}) {
|
|
|
|
|
if (_isNoOp) return;
|
|
|
|
|
_service!.debug(
|
|
|
|
|
message: message,
|
|
|
|
|
module: module,
|
|
|
|
|
extra: extra ?? {},
|
|
|
|
|
stackTrace: stackTrace,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-01 14:17:23 +08:00
|
|
|
|
|
|
|
|
void info({
|
|
|
|
|
required String message,
|
2026-04-01 14:28:30 +08:00
|
|
|
Map<String, dynamic>? extra,
|
2026-04-01 14:17:23 +08:00
|
|
|
StackTrace? stackTrace,
|
2026-04-01 14:46:47 +08:00
|
|
|
}) {
|
|
|
|
|
if (_isNoOp) return;
|
|
|
|
|
_service!.info(
|
|
|
|
|
message: message,
|
|
|
|
|
module: module,
|
|
|
|
|
extra: extra ?? {},
|
|
|
|
|
stackTrace: stackTrace,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-01 14:17:23 +08:00
|
|
|
|
|
|
|
|
void warning({
|
|
|
|
|
required String message,
|
2026-04-01 14:28:30 +08:00
|
|
|
Map<String, dynamic>? extra,
|
2026-04-01 14:17:23 +08:00
|
|
|
StackTrace? stackTrace,
|
2026-04-01 14:46:47 +08:00
|
|
|
}) {
|
|
|
|
|
if (_isNoOp) return;
|
|
|
|
|
_service!.warning(
|
|
|
|
|
message: message,
|
|
|
|
|
module: module,
|
|
|
|
|
extra: extra ?? {},
|
|
|
|
|
stackTrace: stackTrace,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-01 14:17:23 +08:00
|
|
|
|
|
|
|
|
void error({
|
|
|
|
|
required String message,
|
|
|
|
|
required Object error,
|
|
|
|
|
required StackTrace stackTrace,
|
|
|
|
|
Map<String, dynamic>? extra,
|
2026-04-01 14:46:47 +08:00
|
|
|
}) {
|
|
|
|
|
if (_isNoOp) {
|
|
|
|
|
debugPrint('[$module] ERROR: $message, error: $error');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_service!.error(
|
|
|
|
|
message: message,
|
|
|
|
|
error: error,
|
|
|
|
|
stackTrace: stackTrace,
|
|
|
|
|
module: module,
|
|
|
|
|
extra: extra,
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-04-01 14:17:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger getLogger(String module) => Logger.get(module);
|