Skip to content

Commit

Permalink
fix(Emit): Only attach stack to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Sep 3, 2019
1 parent b28d086 commit 9ef7b84
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 5 additions & 3 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ test('logging', () => {
expect(events[0].tag).toBe(TAG)
expect(events[0].level).toBe(LogLevel.debug)
expect(events[0].message).toBe('a debug message')
expect(events[0].stack).toMatch(/^Error:/)
// Second line (first call stack) in stack trace should be this file
expect(events[0].stack.split('\n')[1]).toMatch(/logga\/index\.test\.ts/)
expect(events[0].stack).toBeUndefined()

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

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

})

test('TTY', () => {
Expand Down
5 changes: 3 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export interface LogHandler {
* Take a message `string`, or `LogInfo` object,
* and emit an event with a `LogData` object.
*
* If `LogData` does not have a stack attached, one is generated and set on the `LogData`.
* For `LogLevel.error`, if `LogInfo` does not have a `stack`,
* one is generated and set on the `LogData`.
*
* @param info
* @param level
Expand All @@ -46,7 +47,7 @@ function emitLogData(info: LogInfo | string, tag: string, level: LogLevel) {
let stack: string
if (typeof info === 'object' && info.stack) {
stack = info.stack
} else {
} else if (level <= LogLevel.error) {
const error = new Error()
// Remove the first three lines of the stack trace which
// are not useful (see issue #3)
Expand Down

0 comments on commit 9ef7b84

Please sign in to comment.