From 788eb2fbf6df4742a13a5eaf7e3345e56c2e350a Mon Sep 17 00:00:00 2001 From: lesleysin Date: Fri, 27 Dec 2024 12:19:22 +0200 Subject: [PATCH] last logger improve iter --- lib/src/misc/logger.dart | 72 ++++++++++++++++++++++++++++++---------- pubspec.yaml | 2 +- 2 files changed, 56 insertions(+), 18 deletions(-) diff --git a/lib/src/misc/logger.dart b/lib/src/misc/logger.dart index ac9c58d..448b5af 100644 --- a/lib/src/misc/logger.dart +++ b/lib/src/misc/logger.dart @@ -1,5 +1,8 @@ // import 'package:flutter/foundation.dart' show kDebugMode; -import 'package:logger/logger.dart'; +import 'dart:io'; + +import 'package:ansicolor/ansicolor.dart'; +import 'package:flutter/foundation.dart' show kDebugMode; /// The [DebugLogger] interface defines a contract for logger implementations. /// @@ -18,7 +21,12 @@ abstract interface class DebugLogger { /// /// It logs messages to the console if the app is running in debug mode. final class DefaultLogger implements DebugLogger { - late final Logger _logger = Logger(); + final _errorPen = AnsiPen()..red(); + final _warnPen = AnsiPen()..yellow(); + final _infoPen = AnsiPen()..white(); + final _headingPen = AnsiPen()..green(); + String? _tag; + static const logTag = "[DUIT FRAMEWORK]: "; /// The [DefaultLogger] singleton instance. static final instance = DefaultLogger._internal(); @@ -26,28 +34,58 @@ final class DefaultLogger implements DebugLogger { /// The internal constructor for the singleton instance. DefaultLogger._internal(); + String _colorize(String m, AnsiPen c) { + var lines = m.split('\n'); + lines = lines.map((e) => c.write(e)).toList(); + final coloredMsg = lines.join('\n'); + return coloredMsg; + } + + //ignore: avoid_print + void _outPrinter(String m) => m.split("\n").forEach(print); + + String _createTag() => _colorize(logTag, _headingPen); + @override void error( String message, { error, StackTrace? stackTrace, - }) => - _logger.e( - "[DUIT FRAMEWORK]: $message", - time: DateTime.now(), - error: error, - stackTrace: stackTrace, - ); + }) { + if (kDebugMode) { + if (Platform.isIOS || Platform.isMacOS) { + _outPrinter( + "$logTag$message\nError text: ${error.toString()}\nStackTrace: ${stackTrace.toString()}"); + } else { + _tag ??= _createTag(); + final text = + "$message\n Error text: ${error.toString()} \n StackTrace: ${stackTrace.toString()}"; + _outPrinter("$_tag${_colorize(text, _errorPen)}"); + } + } + } @override - void info(String message) => _logger.i( - "[DUIT FRAMEWORK]: $message", - time: DateTime.now(), - ); + void info(String message) { + if (kDebugMode) { + if (Platform.isIOS || Platform.isMacOS) { + _outPrinter("$logTag$message"); + } else { + _tag ??= _createTag(); + _outPrinter("$_tag${_colorize(message, _infoPen)}"); + } + } + } @override - void warn(String message) => _logger.w( - "[DUIT FRAMEWORK]: $message", - time: DateTime.now(), - ); + void warn(String message) { + if (kDebugMode) { + if (Platform.isIOS || Platform.isMacOS) { + _outPrinter("$logTag$message"); + } else { + _tag ??= _createTag(); + _outPrinter("$_tag${_colorize(message, _warnPen)}"); + } + } + } } diff --git a/pubspec.yaml b/pubspec.yaml index 3475004..e004003 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,9 +8,9 @@ environment: flutter: ">=1.17.0" dependencies: + ansicolor: ^2.0.3 flutter: sdk: flutter - logger: ^2.5.0 meta: ^1.15.0 dev_dependencies: