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

Override empty stack traces for traced events #158

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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