Skip to content

Commit

Permalink
last logger improve iter
Browse files Browse the repository at this point in the history
  • Loading branch information
lesleysin committed Dec 27, 2024
1 parent d05b94c commit 788eb2f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
72 changes: 55 additions & 17 deletions lib/src/misc/logger.dart
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -18,36 +21,71 @@ 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();

/// 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)}");
}
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 788eb2f

Please sign in to comment.