Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Override empty stack traces for traced events (#158)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natebosch authored Mar 7, 2024
1 parent cbaf4ee commit 7a7bd5e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion lib/src/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
16 changes: 16 additions & 0 deletions test/logging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,22 @@ void main() {
expect(records[2].stackTrace, isNull);
});

test('defaults a missing trace', () {
final records = <LogRecord>[];
recordStackTraceAtLevel = Level.SEVERE;
root.onRecord.listen(records.add);
root.severe('hello');
expect(records.single.stackTrace, isNotNull);
});

test('defaults an empty trace', () {
final records = <LogRecord>[];
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 = <LogRecord>[];
Expand Down

0 comments on commit 7a7bd5e

Please sign in to comment.