From b6393c8ac2cdab6dbe648133644bd1023da34db2 Mon Sep 17 00:00:00 2001 From: luksab Date: Fri, 31 Oct 2025 19:08:19 +0100 Subject: [PATCH] Add custom log printer to only print stack traces for errors --- lib/utils/logger.dart | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/utils/logger.dart b/lib/utils/logger.dart index 7af63e8..122f313 100644 --- a/lib/utils/logger.dart +++ b/lib/utils/logger.dart @@ -1,9 +1,30 @@ import 'package:logger/logger.dart'; import 'package:rotation_log/rotation_log.dart'; +/// Custom printer that shows stack traces only for errors +class CustomLogPrinter extends LogPrinter { + // needed, because apparently, PrettyPrinter errorMethodCount doesn't do what I would think it does + final _errorPrinter = PrettyPrinter( + methodCount: 8, + ); + final _otherPrinter = PrettyPrinter( + methodCount: 0, + noBoxingByDefault: true, + ); + + @override + List log(LogEvent event) { + if (event.level >= Level.error) { + return _errorPrinter.log(event); + } + return _otherPrinter.log(event); + } +} + /// Application logger using rotation_log for file management final rotLog = RotationLogger(RotationLogTerm.term(RotationLogTermEnum.daily)); final log = Logger( + printer: CustomLogPrinter(), output: MultiOutput([ConsoleOutput(), RotationLogOutput(rotLog)]), );