Skip to content

Commit

Permalink
Logger & Dependencies update (#263)
Browse files Browse the repository at this point in the history
* Refactored logger a bit

* GH-253: Updated exception tracking manager

* Format

* Logger & Dependencies updates

* Cleaned logger code

* Use v3

* Fix issue
  • Loading branch information
hawkkiller authored Oct 9, 2023
1 parent 973d7d6 commit 7cfb2fb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
89 changes: 39 additions & 50 deletions lib/src/core/utils/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<L>(
Expand Down Expand Up @@ -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<LogMessage> get logs => _logger.onRecord.map(
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions lib/src/feature/app/widget/app.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<InitializationResult>(
'result',
result,
description: 'The initialization result from the '
'[InitializationProcessor]',
),
);
}

@override
Widget build(BuildContext context) => DefaultAssetBundle(
bundle: SentryAssetBundle(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mixin InitializationProcessor {
}
stopwatch.stop();
final result = InitializationResult(
dependencies: progress.freeze(),
dependencies: progress.dependencies,
stepCount: stepCount,
msSpent: stopwatch.elapsedMilliseconds,
);
Expand Down
36 changes: 0 additions & 36 deletions lib/src/feature/initialization/model/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,4 @@ final class InitializationProgress {

/// Environment store
final IEnvironmentStore environmentStore;

/// Freeze dependencies, so they cannot be modified
Dependencies freeze() => dependencies.freeze();
}

0 comments on commit 7cfb2fb

Please sign in to comment.