refactor: logging
This commit is contained in:
@@ -9,10 +9,10 @@ import 'package:immich_mobile/domain/models/store.model.dart';
|
||||
import 'package:immich_mobile/presentation/router/router.dart';
|
||||
import 'package:immich_mobile/service_locator.dart';
|
||||
import 'package:immich_mobile/utils/constants/globals.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log_context.mixin.dart';
|
||||
import 'package:immich_mobile/utils/mixins/log.mixin.dart';
|
||||
import 'package:openapi/api.dart';
|
||||
|
||||
class ImmichApiClient extends ApiClient with LogContext {
|
||||
class ImmichApiClient extends ApiClient with LogMixin {
|
||||
ImmichApiClient({required String endpoint}) : super(basePath: endpoint);
|
||||
|
||||
Map<String, String> get headers => defaultHeaderMap;
|
||||
@@ -58,7 +58,7 @@ class ImmichApiClient extends ApiClient with LogContext {
|
||||
);
|
||||
|
||||
if (res.statusCode == HttpStatus.unauthorized) {
|
||||
log.severe("Token invalid. Redirecting to login route");
|
||||
log.e("Token invalid. Redirecting to login route");
|
||||
await di<AppRouter>().replaceAll([const LoginRoute()]);
|
||||
throw ApiException(res.statusCode, "Unauthorized");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
8
mobile-v2/lib/utils/mixins/log.mixin.dart
Normal file
8
mobile-v2/lib/utils/mixins/log.mixin.dart
Normal file
@@ -0,0 +1,8 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:immich_mobile/utils/log_manager.dart';
|
||||
|
||||
mixin LogMixin {
|
||||
@protected
|
||||
@nonVirtual
|
||||
Logger get log => LogManager.I.get(runtimeType.toString());
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
mixin LogContext {
|
||||
@protected
|
||||
@nonVirtual
|
||||
Logger get log => Logger(runtimeType.toString());
|
||||
}
|
||||
Reference in New Issue
Block a user