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 = [];