pino-multi-stream
Advanced tools
Comparing version 2.1.1 to 3.0.0
@@ -5,3 +5,3 @@ 'use strict' | ||
var bunyan = require('bunyan') | ||
var pinoms = require('./index') | ||
var pinoms = require('./') | ||
var fs = require('fs') | ||
@@ -8,0 +8,0 @@ var dest = fs.createWriteStream('/dev/null') |
123
index.js
'use strict' | ||
var realPino = require('pino') | ||
var noop = function () {} | ||
var pino = require('pino') | ||
var multistream = require('./multistream') | ||
function pino (opts, stream) { | ||
function pinoMultiStream (opts, stream) { | ||
var iopts = opts || {} | ||
stream = stream || process.stdout // same default of pino | ||
if (opts && (opts.writable || opts._writableState)) { | ||
return realPino(null, iopts) | ||
return fixLevel(pino(null, multistream({ streams: [{ stream }] }))) | ||
} | ||
if (iopts.hasOwnProperty('stream') === true) { | ||
return realPino(iopts, iopts.stream) | ||
return fixLevel(pino(null, multistream({ stream: iopts.stream }))) | ||
} | ||
if (iopts.hasOwnProperty('streams') === false) { | ||
return realPino(iopts, stream) | ||
return fixLevel(pino(null, multistream({ stream }))) | ||
} | ||
if (Array.isArray(iopts.streams) === false) { | ||
return realPino(iopts, iopts.streams) | ||
} | ||
return fixLevel(pino(Object.assign({}, iopts, { streams: undefined }), multistream(iopts.streams))) | ||
var streams = iopts.streams | ||
var loggers = {} | ||
function addLogger (opts, stream) { | ||
var logger = realPino(opts, stream) | ||
if (loggers[opts.level]) { | ||
loggers[opts.level].push(logger) | ||
} else { | ||
loggers[opts.level] = [logger] | ||
} | ||
return logger | ||
} | ||
for (var i = 0, j = streams.length; i < j; i += 1) { | ||
var _opts = Object.create(iopts) | ||
var s = streams[i] | ||
_opts.level = (s.level) ? s.level : 'info' | ||
_opts.levelVal = (s.levelVal) ? s.levelVal : undefined | ||
var logger = addLogger(_opts, s.stream) | ||
var levelValues = logger.levels.values | ||
var curLevel = levelValues[_opts.level] | ||
Object.keys(levelValues) | ||
.filter(function (l) { return levelValues[l] > curLevel }) | ||
.forEach(function (l) { addLogger({level: l}, s.stream) }) | ||
} | ||
function fixLevel (pino) { | ||
pino.levelVal = pino.stream.minLevel | ||
var stdLevels = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'] | ||
var levels = stdLevels.concat(Object.keys(loggers).filter(function (val) { | ||
return stdLevels.indexOf(val) === -1 | ||
})) | ||
function MSPino (_loggers) { | ||
this.loggers = _loggers | ||
for (var i = 0, j = levels.length; i < j; i += 1) { | ||
if (this.loggers.hasOwnProperty(levels[i]) === false) { | ||
this[levels[i]] = noop | ||
} | ||
} | ||
} | ||
MSPino.prototype = Object.create(realPino) | ||
MSPino.constructor = MSPino | ||
function genLog (level) { | ||
return function LOG (arg1, arg2, arg3, arg4, arg5, arg6) { | ||
for (var i = 0, j = this.loggers[level].length; i < j; i += 1) { | ||
var logger = this.loggers[level][i] | ||
var x | ||
var args | ||
switch (arguments.length) { | ||
case 1: | ||
logger[level](arg1) | ||
break | ||
case 2: | ||
logger[level](arg1, arg2) | ||
break | ||
case 3: | ||
logger[level](arg1, arg2, arg3) | ||
break | ||
case 4: | ||
logger[level](arg1, arg2, arg3, arg4) | ||
break | ||
case 5: | ||
logger[level](arg1, arg2, arg3, arg4, arg5) | ||
break | ||
case 6: | ||
logger[level](arg1, arg2, arg3, arg4, arg5, arg6) | ||
break | ||
default: | ||
args = [arg1, arg2, arg3, arg4, arg5, arg6] | ||
for (x = 7; x < arguments.length; x += 1) { | ||
args[x - 1] = arguments[x] | ||
} | ||
logger[level].apply(logger, args) | ||
if (Array.isArray(iopts.streams)) { | ||
iopts.streams.forEach(function (s) { | ||
if (s.levelVal) { | ||
pino.addLevel(s.level, s.levelVal) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
levels.forEach(function (level) { | ||
MSPino.prototype[level] = genLog(level) | ||
}) | ||
MSPino.prototype.child = function child (bindings) { | ||
var levels = Object.keys(this.loggers) | ||
var childLoggers = {} | ||
for (var i = 0, j = levels.length; i < j; i += 1) { | ||
childLoggers[levels[i]] = [] | ||
for (var x = 0, y = this.loggers[levels[i]].length; x < y; x += 1) { | ||
var log = this.loggers[levels[i]][x] | ||
childLoggers[levels[i]].push(log.child(bindings)) | ||
} | ||
} | ||
return new MSPino(childLoggers) | ||
return pino | ||
} | ||
return new MSPino(loggers) | ||
} | ||
pino.pretty = realPino.pretty | ||
pino.stdSerializers = realPino.stdSerializers | ||
module.exports = pino | ||
module.exports = pinoMultiStream | ||
module.exports.multistream = multistream | ||
module.exports.stdSerializers = pino.stdSerializers | ||
module.exports.pretty = pino.pretty |
{ | ||
"name": "pino-multi-stream", | ||
"version": "2.1.1", | ||
"version": "3.0.0", | ||
"description": "A wrapper for the Pino logger that provides Bunyan's multipe destination stream API", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "standard && tap --no-cov test/index.js", | ||
"benchmark": "node benchmark.js" | ||
"test": "standard && tap --no-cov test/*.js", | ||
"benchmark": "node benchmark.js", | ||
"ci": "standard && tap --cov test/*.js" | ||
}, | ||
@@ -31,2 +32,3 @@ "precommit": "test", | ||
"standard": "^10.0.2", | ||
"pino": "^4.6.0", | ||
"tap": "^10.3.3" | ||
@@ -36,3 +38,6 @@ }, | ||
"pino": "^4.6.0" | ||
}, | ||
"peerDependencies": { | ||
"pino": "^4.6.0" | ||
} | ||
} |
@@ -87,12 +87,30 @@ # pino-multi-stream | ||
### pinoms.multistream(streams) | ||
Manually create a single `multistream` as used internally by the | ||
wrapper: | ||
```js | ||
var fs = require('fs') | ||
var pino = require('pino') | ||
var multistream = require('pino-multi-stream').multistream | ||
var streams = [ | ||
{stream: fs.createWriteStream('/tmp/info.stream.out')}, | ||
{level: 'debug', stream: fs.createWriteStream('/tmp/debug.stream.out')}, | ||
{level: 'fatal', stream: fs.createWriteStream('/tmp/fatal.stream.out')} | ||
] | ||
var log = pino({ | ||
level: 'debug' // this MUST be set at the lowest level of the | ||
// destinations | ||
}, multistream(streams)) | ||
log.debug('this will be written to /tmp/debug.stream.out') | ||
log.info('this will be written to /tmp/debug.stream.out and /tmp/info.stream.out') | ||
log.fatal('this will be written to /tmp/debug.stream.out, /tmp/info.stream.out and /tmp/fatal.stream.out') | ||
``` | ||
<a id="caveats"></a> | ||
## Caveats | ||
+ The speed of *pino-multi-stream* is dependent upon the number of streams you | ||
add. Regardless of the number, it will be slower than a regular *pino* instance. | ||
+ If you create child loggers then a new logger will be created for ***each | ||
stream*** of the parent logger. This holds true for however many child loggers | ||
are created. | ||
**Stern warning:** the performance of this module being dependent on the number | ||
@@ -103,3 +121,4 @@ of streams you supply cannot be overstated. This module is being provided so | ||
use this module for only as long as it will take you to overhaul the way | ||
you handle logging in your application. | ||
you handle logging in your application. `pino-multi-stream` offers close | ||
to zero overhead if _there is only one destination stream_. | ||
@@ -110,4 +129,4 @@ To illustrate what we mean, here is a benchmark of *pino* and *Bunyan* using | ||
``` | ||
benchBunyanOne*10000: 798.461ms | ||
benchPinoMSOne*10000: 255.239ms | ||
benchBunyanOne*10000: 703.071ms | ||
benchPinoMSOne*10000: 287.060ms | ||
``` | ||
@@ -119,4 +138,4 @@ | ||
``` | ||
benchBunyanFour*10000: 2654.913ms | ||
benchPinoMSFour*10000: 1282.892ms | ||
benchBunyanFour*10000: 2249.955ms | ||
benchPinoMSFour*10000: 1017.886ms | ||
``` | ||
@@ -127,4 +146,4 @@ | ||
``` | ||
benchBunyanTen*10000: 6659.032ms | ||
benchPinoMSTen*10000: 5109.693ms | ||
benchBunyanTen*10000: 4950.301ms | ||
benchPinoMSTen*10000: 3127.361ms | ||
``` | ||
@@ -131,0 +150,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21006
11
544
150
2
7
2