From 7a7bd5e31ddfe23e34d37ded82d6d0cd5706862c Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Thu, 7 Mar 2024 14:43:59 -0800 Subject: [PATCH] Override empty stack traces for traced events (#158) We already default to the current stack trace of the logging caller when there is no stack trace available so the author has some signal for where to look in the code. Also default for non-null but empty stack traces. This is more useful where an empty trace was passed through some signature to satisfy nullability requirements and forwarded to the log. --- CHANGELOG.md | 3 ++- lib/src/logger.dart | 7 ++++++- pubspec.yaml | 2 +- test/logging_test.dart | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41843d8..fa6646a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ -## 1.2.1-wip +## 1.3.0-wip +* Override empty stack traces for trace level events. * Require Dart 3.2 ## 1.2.0 diff --git a/lib/src/logger.dart b/lib/src/logger.dart index bf27f50..d9fa254 100644 --- a/lib/src/logger.dart +++ b/lib/src/logger.dart @@ -202,6 +202,10 @@ class Logger { /// was made. This can be advantageous if a log listener wants to handler /// records of different zones differently (e.g. group log records by HTTP /// request if each HTTP request handler runs in it's own zone). + /// + /// If this record is logged at a level equal to or higher than + /// [recordStackTraceAtLevel] and [stackTrace] is `null` or [StackTrace.empty] + /// it will be defaulted to the current stack trace for this call. void log(Level logLevel, Object? message, [Object? error, StackTrace? stackTrace, Zone? zone]) { Object? object; @@ -218,7 +222,8 @@ class Logger { object = message; } - if (stackTrace == null && logLevel >= recordStackTraceAtLevel) { + if ((stackTrace == null || stackTrace == StackTrace.empty) && + logLevel >= recordStackTraceAtLevel) { stackTrace = StackTrace.current; error ??= 'autogenerated stack trace for $logLevel $msg'; } diff --git a/pubspec.yaml b/pubspec.yaml index 6d05e98..bb3ff1a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: logging -version: 1.2.1-wip +version: 1.3.0-wip description: >- Provides APIs for debugging and error logging, similar to loggers in other languages, such as the Closure JS Logger and java.util.logging.Logger. diff --git a/test/logging_test.dart b/test/logging_test.dart index 7edabb0..6bff3d8 100644 --- a/test/logging_test.dart +++ b/test/logging_test.dart @@ -690,6 +690,22 @@ void main() { expect(records[2].stackTrace, isNull); }); + test('defaults a missing trace', () { + final records = []; + recordStackTraceAtLevel = Level.SEVERE; + root.onRecord.listen(records.add); + root.severe('hello'); + expect(records.single.stackTrace, isNotNull); + }); + + test('defaults an empty trace', () { + final records = []; + recordStackTraceAtLevel = Level.SEVERE; + root.onRecord.listen(records.add); + root.severe('hello', 'error', StackTrace.empty); + expect(records.single.stackTrace, isNot(StackTrace.empty)); + }); + test('provided trace is used if given', () { final trace = StackTrace.current; final records = [];