diff --git a/lib/levels.js b/lib/levels.js index 9ede9f11c..995b54e0e 100644 --- a/lib/levels.js +++ b/lib/levels.js @@ -6,8 +6,7 @@ const { useLevelLabelsSym, changeLevelNameSym, useOnlyCustomLevelsSym, - streamSym, - forceFlushOnFatalSym + streamSym } = require('./symbols') const { noop, genLog } = require('./tools') @@ -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), diff --git a/lib/symbols.js b/lib/symbols.js index 810f0d12e..38c3609e6 100644 --- a/lib/symbols.js +++ b/lib/symbols.js @@ -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') @@ -58,6 +56,5 @@ module.exports = { changeLevelNameSym, wildcardGsym, needsMetadataGsym, - useOnlyCustomLevelsSym, - forceFlushOnFatalSym + useOnlyCustomLevelsSym } diff --git a/pino.js b/pino.js index a77995eb1..14ad86c8c 100644 --- a/pino.js +++ b/pino.js @@ -29,8 +29,7 @@ const { messageKeySym, useLevelLabelsSym, changeLevelNameSym, - useOnlyCustomLevelsSym, - forceFlushOnFatalSym + useOnlyCustomLevelsSym } = symbols const { epochTime, nullTime } = time const { pid } = process @@ -51,8 +50,7 @@ const defaultOptions = { redact: null, customLevels: null, changeLevelName: 'level', - useOnlyCustomLevels: false, - forceFlushOnFatal: false + useOnlyCustomLevels: false } const normalize = createArgsNormalizer(defaultOptions) @@ -73,8 +71,7 @@ function pino (...args) { customLevels, useLevelLabels, changeLevelName, - useOnlyCustomLevels, - forceFlushOnFatal + useOnlyCustomLevels } = opts const stringifiers = redact ? redaction(redact, stringify) : {} @@ -113,8 +110,7 @@ function pino (...args) { [formatOptsSym]: formatOpts, [messageKeySym]: messageKey, [serializersSym]: serializers, - [chindingsSym]: chindings, - [forceFlushOnFatalSym]: forceFlushOnFatal + [chindingsSym]: chindings } Object.setPrototypeOf(instance, proto) diff --git a/test/levels.test.js b/test/levels.test.js index 66b312c20..3eaabf750 100644 --- a/test/levels.test.js +++ b/test/levels.test.js @@ -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') })