From 7cfb2fb4d2486fab3f4b09cf531dac5211e34809 Mon Sep 17 00:00:00 2001 From: Michael Lazebny <62852417+hawkkiller@users.noreply.github.com> Date: Mon, 9 Oct 2023 08:10:50 +0200 Subject: [PATCH] Logger & Dependencies update (#263) * Refactored logger a bit * GH-253: Updated exception tracking manager * Format * Logger & Dependencies updates * Cleaned logger code * Use v3 * Fix issue --- .github/workflows/code-analysis.yml | 2 +- lib/src/core/utils/logger.dart | 89 ++++++++----------- lib/src/feature/app/widget/app.dart | 14 --- .../logic/initialization_processor.dart | 2 +- .../initialization/model/dependencies.dart | 36 -------- .../model/initialization_progress.dart | 3 - 6 files changed, 41 insertions(+), 105 deletions(-) diff --git a/.github/workflows/code-analysis.yml b/.github/workflows/code-analysis.yml index 31cadb7..908df83 100644 --- a/.github/workflows/code-analysis.yml +++ b/.github/workflows/code-analysis.yml @@ -46,7 +46,7 @@ jobs: run: flutter test --coverage --coverage-path=coverage/lcov.info --test-randomize-ordering-seed=random --concurrency=10 - name: 📈 Upload Coverage - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 with: # not needed for public repos token: ${{ secrets.CODECOV_TOKEN }} diff --git a/lib/src/core/utils/logger.dart b/lib/src/core/utils/logger.dart index 8f6620e..fdf94f3 100644 --- a/lib/src/core/utils/logger.dart +++ b/lib/src/core/utils/logger.dart @@ -4,7 +4,7 @@ import 'package:flutter/foundation.dart'; import 'package:logging/logging.dart' as logging; /// Logger instance -final Logger logger = AppLogger$Logging(); +final Logger logger = LoggerLogging(); /// Typedef for the log formatter typedef LogFormatter = String Function(LogMessage message, LogOptions options); @@ -102,16 +102,16 @@ abstract base class Logger { void error(String message, {Object? error, StackTrace? stackTrace}); /// Logs the warning to the console - void warning(Object message); + void warning(String message); /// Logs the info to the console - void info(Object message); + void info(String message); /// Logs the debug to the console - void debug(Object message); + void debug(String message); /// Logs the verbose to the console - void verbose(Object message); + void verbose(String message); /// Set up the logger L runLogging( @@ -140,26 +140,24 @@ abstract base class Logger { } /// Default logger using logging package -final class AppLogger$Logging extends Logger { +final class LoggerLogging extends Logger { final _logger = logging.Logger('SizzleLogger'); @override - void debug(Object message) => _logger.fine(message); + void debug(String message) => _logger.fine(message); @override - void error( - String message, { - Object? error, - StackTrace? stackTrace, - }) => - _logger.severe( - message, - error, - stackTrace, - ); + void error(String message, {Object? error, StackTrace? stackTrace}) => + _logger.severe(message, error, stackTrace); + + @override + void info(String message) => _logger.info(message); + + @override + void verbose(String message) => _logger.finest(message); @override - void info(Object message) => _logger.info(message); + void warning(String message) => _logger.warning(message); @override Stream get logs => _logger.onRecord.map( @@ -198,41 +196,32 @@ final class AppLogger$Logging extends Logger { return fn(); } +} - /// Formats the logger message - /// - /// Combines emoji, time and message - static String _formatLoggerMessage({ - required LogMessage log, - required LogOptions options, - }) { - final buffer = StringBuffer(); - if (options.showEmoji) { - buffer.write(log.logLevel.emoji); - buffer.write(' '); - } - if (options.showTime) { - buffer.write(log.time?.formatTime()); - buffer.write(' | '); - } - buffer.write(log.message); - if (log.error != null) { - buffer.write(' | '); - buffer.write(log.error); - } - if (log.stackTrace != null) { - buffer.write(' | '); - buffer.writeln(log.stackTrace); - } - - return buffer.toString(); +String _formatLoggerMessage({ + required LogMessage log, + required LogOptions options, +}) { + final buffer = StringBuffer(); + if (options.showEmoji) { + buffer.write(log.logLevel.emoji); + buffer.write(' '); + } + if (options.showTime) { + buffer.write(log.time?.formatTime()); + buffer.write(' | '); + } + buffer.write(log.message); + if (log.error != null) { + buffer.write(' | '); + buffer.write(log.error); + } + if (log.stackTrace != null) { + buffer.write(' | '); + buffer.writeln(log.stackTrace); } - @override - void verbose(Object message) => _logger.finest(message); - - @override - void warning(Object message) => _logger.warning(message); + return buffer.toString(); } extension on DateTime { diff --git a/lib/src/feature/app/widget/app.dart b/lib/src/feature/app/widget/app.dart index 56887ac..46fc718 100644 --- a/lib/src/feature/app/widget/app.dart +++ b/lib/src/feature/app/widget/app.dart @@ -1,4 +1,3 @@ -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:sizzle_starter/src/feature/app/widget/locale_scope.dart'; @@ -30,19 +29,6 @@ class App extends StatelessWidget { /// The initialization result from the [InitializationProcessor] final InitializationResult result; - @override - void debugFillProperties(DiagnosticPropertiesBuilder properties) { - super.debugFillProperties(properties); - properties.add( - DiagnosticsProperty( - 'result', - result, - description: 'The initialization result from the ' - '[InitializationProcessor]', - ), - ); - } - @override Widget build(BuildContext context) => DefaultAssetBundle( bundle: SentryAssetBundle(), diff --git a/lib/src/feature/initialization/logic/initialization_processor.dart b/lib/src/feature/initialization/logic/initialization_processor.dart index 5429451..8d50e6a 100644 --- a/lib/src/feature/initialization/logic/initialization_processor.dart +++ b/lib/src/feature/initialization/logic/initialization_processor.dart @@ -51,7 +51,7 @@ mixin InitializationProcessor { } stopwatch.stop(); final result = InitializationResult( - dependencies: progress.freeze(), + dependencies: progress.dependencies, stepCount: stepCount, msSpent: stopwatch.elapsedMilliseconds, ); diff --git a/lib/src/feature/initialization/model/dependencies.dart b/lib/src/feature/initialization/model/dependencies.dart index 6b804df..41cc5e8 100644 --- a/lib/src/feature/initialization/model/dependencies.dart +++ b/lib/src/feature/initialization/model/dependencies.dart @@ -17,9 +17,6 @@ abstract base class Dependencies { /// Locale repository abstract final LocaleRepository localeRepository; - - /// Freeze dependencies, so they cannot be modified - Dependencies freeze(); } /// {@template mutable_dependencies} @@ -39,39 +36,6 @@ final class DependenciesMutable extends Dependencies { @override late LocaleRepository localeRepository; - - @override - Dependencies freeze() => _DependenciesImmutable( - sharedPreferences: sharedPreferences, - themeRepository: themeRepository, - localeRepository: localeRepository, - ); -} - -/// {@template immutable_dependencies} -/// Immutable version of dependencies -/// -/// Used to store dependencies -/// {@endtemplate} -final class _DependenciesImmutable extends Dependencies { - /// {@macro immutable_dependencies} - const _DependenciesImmutable({ - required this.sharedPreferences, - required this.themeRepository, - required this.localeRepository, - }); - - @override - final SharedPreferences sharedPreferences; - - @override - final ThemeRepository themeRepository; - - @override - final LocaleRepository localeRepository; - - @override - Dependencies freeze() => this; } /// {@template initialization_result} diff --git a/lib/src/feature/initialization/model/initialization_progress.dart b/lib/src/feature/initialization/model/initialization_progress.dart index 99a5a80..4883082 100644 --- a/lib/src/feature/initialization/model/initialization_progress.dart +++ b/lib/src/feature/initialization/model/initialization_progress.dart @@ -16,7 +16,4 @@ final class InitializationProgress { /// Environment store final IEnvironmentStore environmentStore; - - /// Freeze dependencies, so they cannot be modified - Dependencies freeze() => dependencies.freeze(); }