Skip to content

Commit 0cc96e2

Browse files
committed
Added error handling to LogfmtPrinter
1 parent 57f4ec7 commit 0cc96e2

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/src/printers/logfmt_printer.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class LogfmtPrinter extends LogPrinter {
2929
}
3030
});
3131
}
32+
if (event.error != null) {
33+
output.write(' error="${event.error}"');
34+
}
3235

3336
return [output.toString()];
3437
}

test/printers/logfmt_printer_test.dart

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import 'package:test/test.dart';
21
import 'package:logger/logger.dart';
2+
import 'package:test/test.dart';
33

44
void main() {
55
var printer = LogfmtPrinter();
@@ -18,13 +18,14 @@ void main() {
1818

1919
test('with a string message includes a msg key', () {
2020
expect(
21-
printer.log(LogEvent(
22-
Level.debug,
23-
'some message',
24-
Exception('boom'),
25-
StackTrace.current,
26-
))[0],
27-
contains('msg="some message"'));
21+
printer.log(LogEvent(
22+
Level.debug,
23+
'some message',
24+
Exception('boom'),
25+
StackTrace.current,
26+
))[0],
27+
contains('msg="some message"'),
28+
);
2829
});
2930

3031
test('includes random key=value pairs', () {
@@ -39,6 +40,21 @@ void main() {
3940
expect(output, contains('foo="bar baz"'));
4041
});
4142

43+
test('handles an error/exception', () {
44+
var output = printer.log(LogEvent(
45+
Level.debug,
46+
'some message',
47+
Exception('boom'),
48+
StackTrace.current,
49+
))[0];
50+
expect(output, contains('error="Exception: boom"'));
51+
52+
output = printer.log(LogEvent(
53+
Level.debug,
54+
'some message',
55+
))[0];
56+
expect(output, isNot(contains('error=')));
57+
});
58+
4259
test('handles a stacktrace', () {}, skip: 'TODO');
43-
test('handles an error/exception', () {}, skip: 'TODO');
4460
}

0 commit comments

Comments
 (0)