Integrate logging system into Flutter app with service registration and testing setup
This commit is contained in:
@@ -1,5 +1,61 @@
|
||||
/// A Calculator.
|
||||
class Calculator {
|
||||
/// Returns [value] plus 1.
|
||||
int addOne(int value) => value + 1;
|
||||
library;
|
||||
|
||||
import 'package:fluttery/logger/logger.dart';
|
||||
import 'package:fluttery/src/logger/logger_impl.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
|
||||
/// A class to manage services.
|
||||
class App {
|
||||
static final _AppService _appService = _AppService();
|
||||
|
||||
/// Registers a service with a factory function to instantiate the implementation.
|
||||
///
|
||||
/// This ensures that the implementation is created when the service is requested.
|
||||
///
|
||||
/// `implFactory` - A factory method to create the service implementation.
|
||||
static void registerService<T extends Service>(T Function() implFactory) {
|
||||
_appService.registerSingleton<T>(implFactory);
|
||||
}
|
||||
|
||||
/// Retrieves the registered service.
|
||||
///
|
||||
/// Returns an instance of the registered service.
|
||||
static T service<T extends Service>() {
|
||||
return _appService.resolve<T>();
|
||||
}
|
||||
|
||||
/// Registers the default services required by the application.
|
||||
static void registerDefaultServices() {
|
||||
registerService<Logger>(() => LoggerImpl());
|
||||
}
|
||||
}
|
||||
|
||||
/// Abstract class to represent a service.
|
||||
abstract class Service {}
|
||||
|
||||
/// Internal class to manage the registration and resolution of services.
|
||||
class _AppService {
|
||||
static _AppService? _singleton;
|
||||
|
||||
static final KiwiContainer _kiwi = KiwiContainer();
|
||||
|
||||
/// Factory constructor to ensure singleton instance of _AppService.
|
||||
factory _AppService() => _singleton ??= _AppService._();
|
||||
|
||||
/// Private constructor.
|
||||
_AppService._();
|
||||
|
||||
/// Registers a singleton service with a factory function to create the instance.
|
||||
///
|
||||
/// `serviceFactory` - A factory method to create the service implementation.
|
||||
void registerSingleton<T extends Service>(T Function() serviceFactory) {
|
||||
_kiwi.registerFactory<T>((c) => serviceFactory());
|
||||
}
|
||||
|
||||
/// Resolves and retrieves the registered service.
|
||||
///
|
||||
/// Returns an instance of the registered service.
|
||||
T resolve<T extends Service>() {
|
||||
return _kiwi.resolve<T>();
|
||||
}
|
||||
}
|
||||
|
||||
33
finlog_app/fluttery/lib/logger/logger.dart
Normal file
33
finlog_app/fluttery/lib/logger/logger.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'package:fluttery/fluttery.dart';
|
||||
import 'package:logging/logging.dart' as lib;
|
||||
|
||||
/// Abstract class for logging service.
|
||||
/// Provides methods for different log levels and configuration.
|
||||
abstract class Logger extends Service {
|
||||
/// Logs an informational message.
|
||||
///
|
||||
/// [message] is the information to log.
|
||||
void info(String message);
|
||||
|
||||
/// Logs a warning message.
|
||||
///
|
||||
/// [message] is the warning to log.
|
||||
void warning(String message);
|
||||
|
||||
/// Logs an error message with optional error and stack trace.
|
||||
///
|
||||
/// [message] is the error message to log.
|
||||
/// [error] is the optional error object associated with this log entry.
|
||||
/// [stackTrace] is the optional stack trace associated with this log entry.
|
||||
void error(String message, [Object? error, StackTrace? stackTrace]);
|
||||
|
||||
/// Logs a debug message.
|
||||
///
|
||||
/// [message] is the debug message to log.
|
||||
void debug(String message);
|
||||
|
||||
/// Sets the log level for the logger.
|
||||
///
|
||||
/// [level] is the new log level to set.
|
||||
void setLogLevel(lib.Level level);
|
||||
}
|
||||
57
finlog_app/fluttery/lib/src/logger/logger_impl.dart
Normal file
57
finlog_app/fluttery/lib/src/logger/logger_impl.dart
Normal file
@@ -0,0 +1,57 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fluttery/logger/logger.dart';
|
||||
import 'package:logging/logging.dart' as lib;
|
||||
|
||||
// ignore_for_file: avoid_print
|
||||
class LoggerImpl implements Logger {
|
||||
final lib.Logger _logger;
|
||||
|
||||
// coverage:ignore-start
|
||||
/// Constructor
|
||||
LoggerImpl() : _logger = lib.Logger("Logger") {
|
||||
_logger.onRecord.listen((lib.LogRecord record) {
|
||||
print('${record.level.name}: ${record.time}: ${record.message}');
|
||||
if (record.error != null) {
|
||||
print('Error: ${record.error}');
|
||||
}
|
||||
if (record.stackTrace != null) {
|
||||
print('Stack Trace: ${record.stackTrace}');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// coverage:ignore-end
|
||||
@visibleForTesting
|
||||
factory LoggerImpl.forTest(lib.Logger logger) {
|
||||
final instance = LoggerImpl._internal(logger);
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Private internal constructor
|
||||
LoggerImpl._internal(this._logger);
|
||||
|
||||
@override
|
||||
void info(String message) {
|
||||
_logger.info(message);
|
||||
}
|
||||
|
||||
@override
|
||||
void warning(String message) {
|
||||
_logger.warning(message);
|
||||
}
|
||||
|
||||
@override
|
||||
void error(String message, [Object? error, StackTrace? stackTrace]) {
|
||||
_logger.severe(message, error, stackTrace);
|
||||
}
|
||||
|
||||
@override
|
||||
void debug(String message) {
|
||||
_logger.fine(message);
|
||||
}
|
||||
|
||||
@override
|
||||
void setLogLevel(lib.Level level) {
|
||||
_logger.level = level;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user