refactor: logging

This commit is contained in:
shenlong-tanwen
2024-09-21 10:14:21 +05:30
parent ded4481190
commit 239bca0cda
14 changed files with 105 additions and 72 deletions
+43 -12
View File
@@ -4,7 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:immich_mobile/domain/interfaces/log.interface.dart';
import 'package:immich_mobile/domain/models/log.model.dart';
import 'package:immich_mobile/service_locator.dart';
import 'package:logging/logging.dart';
import 'package:logging/logging.dart' as logging;
/// [LogManager] is a custom logger that is built on top of the [logging] package.
/// The logs are written to the database and onto console, using `debugPrint` method.
@@ -14,14 +14,16 @@ import 'package:logging/logging.dart';
class LogManager {
LogManager._();
static final LogManager _instance = LogManager._();
static final Map<String, Logger> _loggers = <String, Logger>{};
// ignore: match-getter-setter-field-names
static LogManager get I => _instance;
List<LogMessage> _msgBuffer = [];
Timer? _timer;
late final StreamSubscription<LogRecord> _subscription;
late final StreamSubscription<logging.LogRecord> _subscription;
void _onLogRecord(LogRecord record) {
void _onLogRecord(logging.LogRecord record) {
// Only print in development
assert(() {
debugPrint('[${record.level.name}] [${record.time}] ${record.message}');
@@ -55,11 +57,17 @@ class LogManager {
}
void init() {
_subscription = Logger.root.onRecord.listen(_onLogRecord);
_subscription = logging.Logger.root.onRecord.listen(_onLogRecord);
}
Logger get(String? loggerName) => _loggers.putIfAbsent(
loggerName ?? 'main',
() => Logger(loggerName ?? 'main'),
);
void updateLevel(LogLevel level) {
Logger.root.level = Level.LEVELS.elementAtOrNull(level.index);
logging.Logger.root.level =
logging.Level.LEVELS.elementAtOrNull(level.index);
}
void dispose() {
@@ -75,18 +83,41 @@ class LogManager {
static void setGlobalErrorCallbacks() {
FlutterError.onError = (details) {
Logger("FlutterError").severe(
'Unknown framework error occured in library ${details.library ?? "<unknown>"} at node ${details.context ?? "<unkown>"}',
details.exception,
details.stack,
);
LogManager.I.get("FlutterError").wtf(
'Unknown framework error occured in library ${details.library ?? "<unknown>"} at node ${details.context ?? "<unkown>"}',
details.exception,
details.stack,
);
FlutterError.presentError(details);
};
PlatformDispatcher.instance.onError = (error, stack) {
Logger("PlatformDispatcher")
.severe('Unknown error occured in root isolate', error, stack);
LogManager.I
.get("PlatformDispatcher")
.wtf('Unknown error occured in root isolate', error, stack);
return true;
};
}
}
class Logger {
final String _loggerName;
const Logger(this._loggerName);
logging.Logger get _logger => logging.Logger(_loggerName);
// Highly detailed
void v(String message) => _logger.finest(message);
// Troubleshooting
void d(String message) => _logger.fine(message);
// General purpose
void i(String message) => _logger.info(message);
// Potential issues
void w(String message) => _logger.warning(message);
// Error
void e(String message, [Object? error, StackTrace? stack]) =>
_logger.severe(message, error, stack);
// Crash / Serious failure
void wtf(String message, [Object? error, StackTrace? stack]) =>
_logger.shout(message, error, stack);
}