Skip to content

Commit

Permalink
feat: flush & flushSync
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored and davidmarkclements committed Jul 15, 2018
1 parent deffbb0 commit 1caf303
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
6 changes: 0 additions & 6 deletions lib/proto.js
Expand Up @@ -38,7 +38,6 @@ const constructor = class Pino {}
const prototype = {
constructor,
child,
flush,
isLevelEnabled,
version,
get level () { return this[getLevelSym]() },
Expand Down Expand Up @@ -97,8 +96,3 @@ function write (obj, msg, num) {
if (stream instanceof SonicBoom) stream.write(s)
else stream.write(flatstr(s))
}

function flush () {
const stream = this[streamSym]
if ('flushSync' in stream) stream.flushSync()
}
13 changes: 12 additions & 1 deletion lib/tools.js
Expand Up @@ -11,6 +11,7 @@ const {
messageKeyStringSym,
serializersSym,
formatOptsSym,
streamSym,
endSym,
stringifiersSym,
stringifySym,
Expand Down Expand Up @@ -197,11 +198,21 @@ function createArgsNormalizer (defaultOptions) {
}
}

function flush () {
this[streamSym].flush()
}

function flushSync () {
this[streamSym].flushSync()
}

module.exports = {
noop,
getPrettyStream,
asChindings,
asJson,
genLog,
createArgsNormalizer
createArgsNormalizer,
flush,
flushSync
}
6 changes: 4 additions & 2 deletions pino.js
Expand Up @@ -9,7 +9,7 @@ const time = require('./lib/time')
const proto = require('./lib/proto')
const symbols = require('./lib/symbols')
const { mappings, genLsCache, assertNoLevelCollisions } = require('./lib/levels')
const { createArgsNormalizer, asChindings } = require('./lib/tools')
const { createArgsNormalizer, asChindings, flush, flushSync, noop } = require('./lib/tools')
const { version, LOG_VERSION } = require('./lib/meta')
const {
chindingsSym,
Expand Down Expand Up @@ -82,10 +82,13 @@ function pino (...args) {
const time = (timestamp instanceof Function)
? timestamp : (timestamp ? epochTime : nullTime)

const extreme = stream.minLength === 4096
const levels = mappings(customLevels)

const instance = {
levels,
flush: extreme ? flush : noop,
flushSync: extreme ? flushSync : noop,
[streamSym]: stream,
[timeSym]: time,
[stringifySym]: stringify,
Expand All @@ -98,7 +101,6 @@ function pino (...args) {
[chindingsSym]: chindings
}
Object.setPrototypeOf(instance, proto)

if (customLevels) genLsCache(instance)

events(instance)
Expand Down
38 changes: 36 additions & 2 deletions test/extreme.test.js
Expand Up @@ -7,6 +7,7 @@ const { test } = require('tap')
const { fork } = require('child_process')
const writer = require('flush-write-stream')
const { once } = require('./helper')
const pino = require('..')

test('extreme mode', async ({is, teardown}) => {
const now = Date.now
Expand Down Expand Up @@ -119,7 +120,40 @@ test('throw an error if extreme is passed', async ({throws}) => {
})
})

test('flush does nothing without extreme mode', async () => {
var instance = require('..')()
test('flush is a no-op with non-extreme destinations', async ({is}) => {
const instance = require('..')()
is(instance.flush.toString(), 'function noop () {}')
})

test('logger.flush calls dest.flush on extreme destinations', async ({pass, fail}) => {
const dest = pino.extreme()
var passed = false
setTimeout(() => { if (passed === false) fail() })
const flush = dest.flush
dest.flush = () => {
passed = true
dest.flush = flush
pass('synchronously flushed')
}
const instance = require('..')(dest)
instance.flush()
})

test('flushSync is a no-op with non-extreme destinations', async ({is}) => {
const instance = require('..')()
is(instance.flushSync.toString(), 'function noop () {}')
})

test('logger.flushSync calls dest.flushSync on extreme destinations', async ({pass, fail}) => {
const dest = pino.extreme()
var passed = false
setTimeout(() => { if (passed === false) fail() })
const flushSync = dest.flushSync
dest.flushSync = () => {
passed = true
dest.flushSync = flushSync
pass('synchronously flushed')
}
const instance = require('..')(dest)
instance.flushSync()
})

0 comments on commit 1caf303

Please sign in to comment.