-
Notifications
You must be signed in to change notification settings - Fork 862
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pino.multistream Source code imported from https://github.com/pinojs/pino-multi-stream. * Update docs/api.md Co-authored-by: James Sumners <james@sumners.email> * Update docs/api.md Co-authored-by: James Sumners <james@sumners.email> * Update docs/help.md Co-authored-by: James Sumners <james@sumners.email> * deleted multistream.js * resue levels Co-authored-by: James Sumners <james@sumners.email>
- Loading branch information
Showing
7 changed files
with
781 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict' | ||
|
||
const bench = require('fastbench') | ||
const bunyan = require('bunyan') | ||
const pino = require('../') | ||
const fs = require('fs') | ||
const dest = fs.createWriteStream('/dev/null') | ||
|
||
const tenStreams = [ | ||
{ stream: dest }, | ||
{ stream: dest }, | ||
{ stream: dest }, | ||
{ stream: dest }, | ||
{ stream: dest }, | ||
{ level: 'debug', stream: dest }, | ||
{ level: 'debug', stream: dest }, | ||
{ level: 'trace', stream: dest }, | ||
{ level: 'warn', stream: dest }, | ||
{ level: 'fatal', stream: dest } | ||
] | ||
const pinomsTen = pino({ level: 'debug' }, pino.multistream(tenStreams)) | ||
|
||
const fourStreams = [ | ||
{ stream: dest }, | ||
{ stream: dest }, | ||
{ level: 'debug', stream: dest }, | ||
{ level: 'trace', stream: dest } | ||
] | ||
const pinomsFour = pino({ level: 'debug' }, pino.multistream(fourStreams)) | ||
|
||
const pinomsOne = pino({ level: 'info' }, pino.multistream(dest)) | ||
const blogOne = bunyan.createLogger({ | ||
name: 'myapp', | ||
streams: [{ stream: dest }] | ||
}) | ||
|
||
const blogTen = bunyan.createLogger({ | ||
name: 'myapp', | ||
streams: tenStreams | ||
}) | ||
const blogFour = bunyan.createLogger({ | ||
name: 'myapp', | ||
streams: fourStreams | ||
}) | ||
|
||
const max = 10 | ||
const run = bench([ | ||
function benchBunyanTen (cb) { | ||
for (let i = 0; i < max; i++) { | ||
blogTen.info('hello world') | ||
blogTen.debug('hello world') | ||
blogTen.trace('hello world') | ||
blogTen.warn('hello world') | ||
blogTen.fatal('hello world') | ||
} | ||
setImmediate(cb) | ||
}, | ||
function benchPinoMSTen (cb) { | ||
for (let i = 0; i < max; i++) { | ||
pinomsTen.info('hello world') | ||
pinomsTen.debug('hello world') | ||
pinomsTen.trace('hello world') | ||
pinomsTen.warn('hello world') | ||
pinomsTen.fatal('hello world') | ||
} | ||
setImmediate(cb) | ||
}, | ||
function benchBunyanFour (cb) { | ||
for (let i = 0; i < max; i++) { | ||
blogFour.info('hello world') | ||
blogFour.debug('hello world') | ||
blogFour.trace('hello world') | ||
} | ||
setImmediate(cb) | ||
}, | ||
function benchPinoMSFour (cb) { | ||
for (let i = 0; i < max; i++) { | ||
pinomsFour.info('hello world') | ||
pinomsFour.debug('hello world') | ||
pinomsFour.trace('hello world') | ||
} | ||
setImmediate(cb) | ||
}, | ||
function benchBunyanOne (cb) { | ||
for (let i = 0; i < max; i++) { | ||
blogOne.info('hello world') | ||
} | ||
setImmediate(cb) | ||
}, | ||
function benchPinoMSOne (cb) { | ||
for (let i = 0; i < max; i++) { | ||
pinomsOne.info('hello world') | ||
} | ||
setImmediate(cb) | ||
} | ||
], 10000) | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
'use strict' | ||
|
||
const metadata = Symbol.for('pino.metadata') | ||
const { levels } = require('./levels') | ||
|
||
const defaultLevels = Object.create(levels) | ||
defaultLevels.silent = Infinity | ||
|
||
function multistream (streamsArray, opts) { | ||
let counter = 0 | ||
|
||
streamsArray = streamsArray || [] | ||
opts = opts || { dedupe: false } | ||
|
||
let levels = defaultLevels | ||
if (opts.levels && typeof opts.levels === 'object') { | ||
levels = opts.levels | ||
} | ||
|
||
const res = { | ||
write, | ||
add, | ||
flushSync, | ||
minLevel: 0, | ||
streams: [], | ||
clone, | ||
[metadata]: true | ||
} | ||
|
||
if (Array.isArray(streamsArray)) { | ||
streamsArray.forEach(add, res) | ||
} else { | ||
add.call(res, streamsArray) | ||
} | ||
|
||
// clean this object up | ||
// or it will stay allocated forever | ||
// as it is closed on the following closures | ||
streamsArray = null | ||
|
||
return res | ||
|
||
// we can exit early because the streams are ordered by level | ||
function write (data) { | ||
let dest | ||
const level = this.lastLevel | ||
const { streams } = this | ||
let stream | ||
for (let i = 0; i < streams.length; i++) { | ||
dest = streams[i] | ||
if (dest.level <= level) { | ||
stream = dest.stream | ||
if (stream[metadata]) { | ||
const { lastTime, lastMsg, lastObj, lastLogger } = this | ||
stream.lastLevel = level | ||
stream.lastTime = lastTime | ||
stream.lastMsg = lastMsg | ||
stream.lastObj = lastObj | ||
stream.lastLogger = lastLogger | ||
} | ||
if (!opts.dedupe) { | ||
stream.write(data) | ||
} | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
if (opts.dedupe && stream) { | ||
stream.write(data) | ||
} | ||
} | ||
|
||
function flushSync () { | ||
for (const { stream } of this.streams) { | ||
if (typeof stream.flushSync === 'function') { | ||
stream.flushSync() | ||
} | ||
} | ||
} | ||
|
||
function add (dest) { | ||
const { streams } = this | ||
if (typeof dest.write === 'function') { | ||
return add.call(this, { stream: dest }) | ||
} else if (typeof dest.levelVal === 'number') { | ||
return add.call(this, Object.assign({}, dest, { level: dest.levelVal, levelVal: undefined })) | ||
} else if (typeof dest.level === 'string') { | ||
return add.call(this, Object.assign({}, dest, { level: levels[dest.level] })) | ||
} else if (typeof dest.level !== 'number') { | ||
// we default level to 'info' | ||
dest = Object.assign({}, dest, { level: 30 }) | ||
} else { | ||
dest = Object.assign({}, dest) | ||
} | ||
dest.id = counter++ | ||
|
||
streams.unshift(dest) | ||
streams.sort(compareByLevel) | ||
|
||
this.minLevel = streams[0].level | ||
|
||
return res | ||
} | ||
|
||
function clone (level) { | ||
const streams = new Array(this.streams.length) | ||
|
||
for (let i = 0; i < streams.length; i++) { | ||
streams[i] = { | ||
level: level, | ||
stream: this.streams[i].stream | ||
} | ||
} | ||
|
||
return { | ||
write, | ||
add, | ||
minLevel: level, | ||
streams, | ||
clone, | ||
flushSync, | ||
[metadata]: true | ||
} | ||
} | ||
} | ||
|
||
function compareByLevel (a, b) { | ||
return a.level - b.level | ||
} | ||
|
||
module.exports = multistream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.