Skip to content

Commit 9ef7b84

Browse files
committedSep 3, 2019
fix(Emit): Only attach stack to errors
1 parent b28d086 commit 9ef7b84

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed
 

‎index.test.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ test('logging', () => {
2020
expect(events[0].tag).toBe(TAG)
2121
expect(events[0].level).toBe(LogLevel.debug)
2222
expect(events[0].message).toBe('a debug message')
23-
expect(events[0].stack).toMatch(/^Error:/)
24-
// Second line (first call stack) in stack trace should be this file
25-
expect(events[0].stack.split('\n')[1]).toMatch(/logga\/index\.test\.ts/)
23+
expect(events[0].stack).toBeUndefined()
2624

2725
log.info({
2826
message: 'an info message',
@@ -39,6 +37,10 @@ test('logging', () => {
3937

4038
log.error('an error message')
4139
expect(events[3].level).toBe(LogLevel.error)
40+
expect(events[3].stack).toMatch(/^Error:/)
41+
// Second line (first call stack) in stack trace should be this file
42+
expect(events[3].stack.split('\n')[1]).toMatch(/logga\/index\.test\.ts/)
43+
4244
})
4345

4446
test('TTY', () => {

‎index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export interface LogHandler {
3030
* Take a message `string`, or `LogInfo` object,
3131
* and emit an event with a `LogData` object.
3232
*
33-
* If `LogData` does not have a stack attached, one is generated and set on the `LogData`.
33+
* For `LogLevel.error`, if `LogInfo` does not have a `stack`,
34+
* one is generated and set on the `LogData`.
3435
*
3536
* @param info
3637
* @param level
@@ -46,7 +47,7 @@ function emitLogData(info: LogInfo | string, tag: string, level: LogLevel) {
4647
let stack: string
4748
if (typeof info === 'object' && info.stack) {
4849
stack = info.stack
49-
} else {
50+
} else if (level <= LogLevel.error) {
5051
const error = new Error()
5152
// Remove the first three lines of the stack trace which
5253
// are not useful (see issue #3)

0 commit comments

Comments
 (0)
Please sign in to comment.