Files
umbrix/lib/core/logger/logger_controller.dart

66 lines
1.6 KiB
Dart
Raw Permalink Normal View History

2023-12-22 14:16:24 +03:30
import 'dart:io';
import 'package:umbrix/core/logger/custom_logger.dart';
import 'package:umbrix/utils/custom_loggers.dart';
2023-12-22 14:16:24 +03:30
import 'package:loggy/loggy.dart';
class LoggerController extends LoggyPrinter with InfraLogger {
LoggerController(
this.consolePrinter,
this.otherPrinters,
);
final LoggyPrinter consolePrinter;
final Map<String, LoggyPrinter> otherPrinters;
static LoggerController get instance => _instance;
static late LoggerController _instance;
2024-01-13 23:10:48 +03:30
static void preInit() {
Loggy.initLoggy(logPrinter: const ConsolePrinter());
}
2023-12-22 14:16:24 +03:30
static void init(String appLogPath) {
_instance = LoggerController(
2024-01-13 23:10:48 +03:30
const ConsolePrinter(),
2023-12-22 14:16:24 +03:30
{"app": FileLogPrinter(appLogPath)},
);
Loggy.initLoggy(logPrinter: _instance);
}
static Future<void> postInit(bool debugMode) async {
final logLevel = debugMode ? LogLevel.all : LogLevel.info;
final logToFile = debugMode || (!Platform.isAndroid && !Platform.isIOS);
if (!logToFile) _instance.removePrinter("app");
Loggy.initLoggy(
logPrinter: _instance,
logOptions: LogOptions(logLevel),
);
}
void addPrinter(String name, LoggyPrinter printer) {
loggy.debug("adding [$name] printer");
otherPrinters.putIfAbsent(name, () => printer);
}
void removePrinter(String name) {
loggy.debug("removing [$name] printer");
final printer = otherPrinters[name];
if (printer case FileLogPrinter()) {
printer.dispose();
}
otherPrinters.remove(name);
}
@override
void onLog(LogRecord record) {
consolePrinter.onLog(record);
for (final printer in otherPrinters.values) {
printer.onLog(record);
}
}
}