-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add minimalistic frontend logger
Signed-off-by: Philipp Hoenisch <philipp@coblox.tech>
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'package:get_10101/logger/simple_utc_printer.dart'; | ||
import 'package:logger/logger.dart'; | ||
|
||
// Getter to access the logger instance | ||
Logger get logger => AppLogger.instance; | ||
|
||
class AppLogger { | ||
static late final Logger instance; | ||
} | ||
|
||
void buildLogger(bool isLogLevelTrace) { | ||
final logger = Logger( | ||
output: ConsoleOutput(), | ||
filter: ProductionFilter(), | ||
level: isLogLevelTrace ? Level.trace : Level.debug, | ||
printer: SimpleUTCPrinter( | ||
// Colorful log messages | ||
colors: true, | ||
// Should each log print contain a timestamp | ||
printTime: true)); | ||
|
||
AppLogger.instance = logger; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:logger/logger.dart'; | ||
|
||
/// Outputs simple log messages: | ||
/// ``` | ||
/// [E] Log message ERROR: Error info | ||
/// ``` | ||
class SimpleUTCPrinter extends LogPrinter { | ||
static final levelPrefixes = { | ||
Level.trace: '[TRACE]', | ||
Level.debug: '[DEBUG]', | ||
Level.info: '[INFO]', | ||
Level.warning: '[WARN]', | ||
Level.error: '[ERROR]', | ||
Level.fatal: '[FATAL]', | ||
}; | ||
|
||
static final levelColors = { | ||
Level.trace: AnsiColor.fg(AnsiColor.grey(0.5)), | ||
Level.debug: const AnsiColor.none(), | ||
Level.info: const AnsiColor.fg(12), | ||
Level.warning: const AnsiColor.fg(208), | ||
Level.error: const AnsiColor.fg(196), | ||
Level.fatal: const AnsiColor.fg(199), | ||
}; | ||
|
||
final bool printTime; | ||
final bool colors; | ||
|
||
SimpleUTCPrinter({this.printTime = false, this.colors = true}); | ||
|
||
@override | ||
List<String> log(LogEvent event) { | ||
var messageStr = _stringifyMessage(event.message); | ||
var errorStr = event.error != null ? ' ERROR: ${event.error}' : ''; | ||
var timeStr = printTime ? 'TIME: ${event.time.toUtc().toIso8601String()}' : ''; | ||
return ['${_labelFor(event.level)} $timeStr $messageStr$errorStr']; | ||
} | ||
|
||
String _labelFor(Level level) { | ||
var prefix = levelPrefixes[level]!; | ||
var color = levelColors[level]!; | ||
|
||
return colors ? color(prefix) : prefix; | ||
} | ||
|
||
String _stringifyMessage(dynamic message) { | ||
final finalMessage = message is Function ? message() : message; | ||
if (finalMessage is Map || finalMessage is Iterable) { | ||
var encoder = const JsonEncoder.withIndent(null); | ||
return encoder.convert(finalMessage); | ||
} else { | ||
return finalMessage.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters