From 3e7bb989c93f98bae020fc46c435f42b20e7c45c Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Sat, 20 Jan 2024 14:20:09 +0100 Subject: [PATCH] feat: add minimalistic frontend logger Signed-off-by: Philipp Hoenisch --- webapp/frontend/lib/logger/logger.dart | 23 ++++++++ .../lib/logger/simple_utc_printer.dart | 57 +++++++++++++++++++ webapp/frontend/lib/main.dart | 6 ++ webapp/frontend/pubspec.yaml | 1 + 4 files changed, 87 insertions(+) create mode 100644 webapp/frontend/lib/logger/logger.dart create mode 100644 webapp/frontend/lib/logger/simple_utc_printer.dart diff --git a/webapp/frontend/lib/logger/logger.dart b/webapp/frontend/lib/logger/logger.dart new file mode 100644 index 000000000..21c06aa65 --- /dev/null +++ b/webapp/frontend/lib/logger/logger.dart @@ -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; +} diff --git a/webapp/frontend/lib/logger/simple_utc_printer.dart b/webapp/frontend/lib/logger/simple_utc_printer.dart new file mode 100644 index 000000000..7ce3f8e3b --- /dev/null +++ b/webapp/frontend/lib/logger/simple_utc_printer.dart @@ -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 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(); + } + } +} diff --git a/webapp/frontend/lib/main.dart b/webapp/frontend/lib/main.dart index 771eb81c2..4b17de696 100644 --- a/webapp/frontend/lib/main.dart +++ b/webapp/frontend/lib/main.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:get_10101/common/version_service.dart'; +import 'package:get_10101/logger/logger.dart'; import 'package:get_10101/routes.dart'; import 'package:get_10101/wallet/wallet_service.dart'; import 'package:provider/provider.dart'; @@ -8,6 +9,11 @@ import 'common/color.dart'; import 'common/theme.dart'; void main() { + WidgetsFlutterBinding.ensureInitialized(); + buildLogger(false); + logger.i("Logger initialized"); + + var providers = [ Provider(create: (context) => const VersionService()), Provider(create: (context) => const WalletService()) diff --git a/webapp/frontend/pubspec.yaml b/webapp/frontend/pubspec.yaml index 55059aaeb..0453d5c6c 100644 --- a/webapp/frontend/pubspec.yaml +++ b/webapp/frontend/pubspec.yaml @@ -44,6 +44,7 @@ dependencies: bitcoin_icons: ^0.0.4 intl: ^0.19.0 decimal: ^2.3.3 + logger: ^2.0.2+1 dev_dependencies: flutter_test: