Skip to content

Commit

Permalink
Do nothing on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommaso Allevi committed Nov 13, 2019
1 parent b765a55 commit 0fcb023
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 30 deletions.
11 changes: 7 additions & 4 deletions lib/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const {
useLevelLabelsSym,
changeLevelNameSym,
useOnlyCustomLevelsSym,
streamSym,
forceFlushOnFatalSym
streamSym
} = require('./symbols')
const { noop, genLog } = require('./tools')

Expand All @@ -24,8 +23,12 @@ const levelMethods = {
fatal (...args) {
const stream = this[streamSym]
logFatal.call(this, ...args)
if (this[forceFlushOnFatalSym] && typeof stream.flushSync === 'function') {
stream.flushSync()
if (typeof stream.flushSync === 'function') {
try {
stream.flushSync()
} catch (e) {
// do nothing
}
}
},
error: genLog(levels.error),
Expand Down
5 changes: 1 addition & 4 deletions lib/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const messageKeySym = Symbol('pino.messageKey')

const wildcardFirstSym = Symbol('pino.wildcardFirst')

const forceFlushOnFatalSym = Symbol('pino.forceFlushOnFatal')

// public symbols, no need to use the same pino
// version for these
const serializersSym = Symbol.for('pino.serializers')
Expand Down Expand Up @@ -58,6 +56,5 @@ module.exports = {
changeLevelNameSym,
wildcardGsym,
needsMetadataGsym,
useOnlyCustomLevelsSym,
forceFlushOnFatalSym
useOnlyCustomLevelsSym
}
12 changes: 4 additions & 8 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const {
messageKeySym,
useLevelLabelsSym,
changeLevelNameSym,
useOnlyCustomLevelsSym,
forceFlushOnFatalSym
useOnlyCustomLevelsSym
} = symbols
const { epochTime, nullTime } = time
const { pid } = process
Expand All @@ -51,8 +50,7 @@ const defaultOptions = {
redact: null,
customLevels: null,
changeLevelName: 'level',
useOnlyCustomLevels: false,
forceFlushOnFatal: false
useOnlyCustomLevels: false
}

const normalize = createArgsNormalizer(defaultOptions)
Expand All @@ -73,8 +71,7 @@ function pino (...args) {
customLevels,
useLevelLabels,
changeLevelName,
useOnlyCustomLevels,
forceFlushOnFatal
useOnlyCustomLevels
} = opts

const stringifiers = redact ? redaction(redact, stringify) : {}
Expand Down Expand Up @@ -113,8 +110,7 @@ function pino (...args) {
[formatOptsSym]: formatOpts,
[messageKeySym]: messageKey,
[serializersSym]: serializers,
[chindingsSym]: chindings,
[forceFlushOnFatalSym]: forceFlushOnFatal
[chindingsSym]: chindings
}
Object.setPrototypeOf(instance, proto)

Expand Down
27 changes: 13 additions & 14 deletions test/levels.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ test('fatal method sync-flushes the destination if sync flushing is available',
stream.flushSync = () => {
pass('destination flushed')
}
const instance = pino({ forceFlushOnFatal: true }, stream)
const instance = pino(stream)
instance.fatal('this is fatal')
await once(stream, 'data')
doesNotThrow(() => {
Expand All @@ -418,17 +418,16 @@ test('fatal method sync-flushes the destination if sync flushing is available',
})
})

test('fatal method should not sync-flushes the destination if sync flushing is disavailable', async ({ pass, fail, doesNotThrow, plan }) => {
plan(1)
const stream = sink()
stream.flushSync = () => fail('flushSync should not be called')
stream.flush = () => {
pass('destination flushed')
}
const instance = pino({ forceFlushOnFatal: false }, stream)
instance.fatal('this is fatal')
await once(stream, 'data')
doesNotThrow(() => {
instance.fatal('this is fatal')
})
test('fatal method should call async when sync-flushing fails', ({ equal, fail, doesNotThrow, plan }) => {
plan(2)
const messages = [
'this is fatal 1'
]
const stream = sink((result) => equal(result.msg, messages.shift()))
stream.flushSync = () => { throw new Error('Error') }
stream.flush = () => fail('flush should be called')

const instance = pino(stream)
doesNotThrow(() => instance.fatal(messages[0]))
once(stream, 'data')
})

0 comments on commit 0fcb023

Please sign in to comment.