Skip to content

Commit

Permalink
Merge pull request #28 from mcollina/child-extreme
Browse files Browse the repository at this point in the history
Support extreme mode with children
mcollina committed Apr 13, 2016
2 parents 42b19c4 + f4b7a0e commit af6ed08
Showing 2 changed files with 83 additions and 11 deletions.
33 changes: 22 additions & 11 deletions pino.js
Original file line number Diff line number Diff line change
@@ -38,20 +38,23 @@ function pino (opts, stream) {
var level = opts.level || 'info'
var serializers = opts.serializers || {}
var end = ',"v":' + LOG_VERSION + '}\n'
var cache = opts.extreme ? 4096 : 0
var cache = !opts.extreme ? null : {
size: 4096,
buf: ''
}

var logger = new Pino(level, stream, serializers, stringify, end, name, hostname, slowtime, '', cache, formatOpts)
if (cache) {
onExit(function (code, evt) {
if (logger.buf) {
if (cache.buf) {
if (stream === process.stdout) {
console.log(logger.buf)
console.log(cache.buf)
} else if (stream === process.stderr) {
console.error(logger.buf)
console.error(cache.buf)
} else {
stream.write(logger.buf)
stream.write(cache.buf)
}
logger.buf = ''
cache.buf = ''
}
if (!process._events[evt] || process._events[evt].length < 2 || !process._events[evt].filter(function (f) {
return f + '' !== onExit.passCode + '' && f + '' !== onExit.insertCode + ''
@@ -75,7 +78,6 @@ function Pino (level, stream, serializers, stringify, end, name, hostname, slowt
this.hostname = hostname
this.slowtime = slowtime
this.chindings = chindings
this.buf = ''
this.cache = cache
this.formatOpts = formatOpts
this._setLevel(level)
@@ -174,13 +176,22 @@ Pino.prototype.write = function (obj, msg, num) {
return
}

this.buf += s
if (this.buf.length > this.cache) {
this.stream.write(flatstr(this.buf))
this.buf = ''
this.cache.buf += s
if (this.cache.buf.length > this.cache.size) {
this.stream.write(flatstr(this.cache.buf))
this.cache.buf = ''
}
}

Pino.prototype.flush = function () {
if (!this.cache) {
return
}

this.stream.write(flatstr(this.cache.buf))
this.cache.buf = ''
}

function noop () {}

function mapHttpRequest (req) {
61 changes: 61 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -582,3 +582,64 @@ test('extreme mode', function (t) {
t.end()
})
})

test('extreme mode with child', function (t) {
var now = Date.now
var hostname = os.hostname
var proc = process
global.process = {
__proto__: process,
pid: 123456
}
Date.now = function () {
return 1459875739796
}
os.hostname = function () {
return 'abcdefghijklmnopqr'
}
delete require.cache[require.resolve('./')]
var pino = require('./')
var expected = ''
var actual = ''
var normal = pino(writeStream(function (s, enc, cb) {
expected += s
cb()
})).child({ hello: 'world' })

var extreme = pino({extreme: true}, writeStream(function (s, enc, cb) {
actual += s
cb()
})).child({ hello: 'world' })

var i = 500
while (i--) {
normal.info('h')
extreme.info('h')
}

extreme.flush()

var expected2 = expected.split('\n')[0]
var actual2 = ''

var e = 'global.process = { __proto__: process, pid: 123456};Date.now = function () { return 1459875739796;};require("os").hostname = function () { return "abcdefghijklmnopqr";};var pino = require("./");var extreme = pino({extreme: true}).child({ hello: "world" });extreme.info("h")'

var child = spawn('node', ['-e', e])
child.stdout.pipe(writeStream(function (s, enc, cb) {
actual2 += s
cb()
}))

child.on('close', function () {
t.is(actual, expected)
t.is(actual2.trim(), expected2)

t.teardown(function () {
os.hostname = hostname
Date.now = now
global.process = proc
})

t.end()
})
})

0 comments on commit af6ed08

Please sign in to comment.