Comparing version 4.1.2 to 4.2.0
'use strict' | ||
var format = require('quick-format-unescaped') | ||
module.exports = pino | ||
var _console = global.console || {} | ||
function Pino () {} | ||
Pino.prototype = _console | ||
function pino (opts) { | ||
opts = opts || {} | ||
opts.browser = opts.browser || {} | ||
var proto = opts.browser.write || _console | ||
if (opts.browser.write) opts.browser.asObject = true | ||
var levels = ['error', 'fatal', 'warn', 'info', 'debug', 'trace'] | ||
if (typeof proto === 'function') { | ||
proto.error = proto.fatal = proto.warn = | ||
proto.info = proto.debug = proto.trace = proto | ||
} | ||
var level = opts.level || 'info' | ||
var val = pino.levels.values[level] | ||
var logger = new Pino() | ||
var logger = Object.create(proto) | ||
if (!logger.log) logger.log = noop | ||
@@ -31,3 +42,4 @@ | ||
logger.child = function child (bindings) { | ||
logger.child = child | ||
function child (bindings) { | ||
if (!bindings) { | ||
@@ -48,6 +60,26 @@ throw new Error('missing bindings for child Pino') | ||
logger.levels = pino.levels | ||
return !opts.browser.asObject ? logger : asObject(logger, levels) | ||
} | ||
function asObject (logger, levels) { | ||
var k | ||
for (var i = 0; i < levels.length; i++) { | ||
k = levels[i] | ||
logger[k] = (function (write, k) { | ||
return function LOG () { | ||
var args = new Array(arguments.length) | ||
for (var i = 0; i < args.length; i++) args[i] = arguments[i] | ||
var msg = args[0] | ||
var o = { time: Date.now(), level: pino.levels.values[k] } | ||
// deliberate, catching objects, arrays | ||
if (msg !== null && typeof msg === 'object') Object.assign(o, msg) | ||
if (typeof msg === 'string') msg = format(args) | ||
o.msg = msg | ||
write.call(logger, o) | ||
} | ||
})(logger[k], k) | ||
} | ||
return logger | ||
} | ||
pino._Pino = Pino | ||
pino.LOG_VERSION = 1 | ||
@@ -93,3 +125,3 @@ | ||
logger[level] = val > pino.levels.values[level] ? noop | ||
: (logger[level] ? logger[level] : (_console[fallback] || noop)) | ||
: (logger[level] ? logger[level] : (_console[level] || _console[fallback] || noop)) | ||
} | ||
@@ -96,0 +128,0 @@ |
@@ -64,2 +64,3 @@ # Table of Contents | ||
* `enabled` (boolean): enables logging. Default: `true` | ||
* `browser` (Object): browser only, may have `asObject` and `write` keys, see [Pino in the Browser](../readme.md#browser) | ||
+ `stream` (Writable): a writable stream where the logs will be written. | ||
@@ -66,0 +67,0 @@ Default: `process.stdout` |
{ | ||
"name": "pino", | ||
"version": "4.1.2", | ||
"version": "4.2.0", | ||
"description": "super fast, all natural json logger", | ||
@@ -5,0 +5,0 @@ "main": "pino.js", |
@@ -149,4 +149,56 @@ ![banner](pino-banner.png) | ||
In the browser, `pino` uses corresponding [Log4j](https://en.wikipedia.org/wiki/Log4j) `console` methods (`console.error`, `console.warn`, `console.info`, `console.debug`, `console.trace`) and uses `console.error` for any `fatal` level logs. | ||
By default, in the browser, | ||
`pino` uses corresponding [Log4j](https://en.wikipedia.org/wiki/Log4j) `console` methods (`console.error`, `console.warn`, `console.info`, `console.debug`, `console.trace`) and uses `console.error` for any `fatal` level logs. | ||
### Browser Options | ||
Pino can be passed a `browser` object in the options object, | ||
which can have a `write` property or an `asObject` property. | ||
#### `asObject` (Boolean) | ||
```js | ||
var pino = require('pino')({browser: {asObject: true}}) | ||
``` | ||
The `asObject` option will create a pino-like log object instead of | ||
passing all arguments to a console method, for instance: | ||
```js | ||
pino.info('hi') // creates and logs {msg: 'hi', level: 30, time: <ts>} | ||
``` | ||
When `write` is set, `asObject` will always be `true`. | ||
#### `write` (Function | Object) | ||
Instead of passing log messages to `console.log` they can be passed to | ||
a supplied function. | ||
If `write` is set to a single function, all logging objects are passed | ||
to this function. | ||
```js | ||
var pino = require('pino')({browser: {write: (o) => { | ||
// do something with o | ||
}}}) | ||
``` | ||
If `write` is an object, it can have methods that correspond to the | ||
levels. When a message is logged at a given level, the corresponding | ||
method is called. If a method isn't present, the logging falls back | ||
to using the `console`. | ||
```js | ||
var pino = require('pino')({browser: {write: { | ||
info: function (o) { | ||
//process info log object | ||
}, | ||
error: function (o) { | ||
//process error log object | ||
} | ||
}}}) | ||
``` | ||
<a name="caveats"></a> | ||
@@ -153,0 +205,0 @@ ## Caveats |
@@ -74,7 +74,2 @@ 'use strict' | ||
test('exposes faux _Pino constructor', function (t) { | ||
t.ok(isFunc(pino._Pino)) | ||
t.end() | ||
}) | ||
test('exposes faux stdSerializers', function (t) { | ||
@@ -118,7 +113,152 @@ t.ok(pino.stdSerializers) | ||
test('exposes faux _Pino constructor', function (t) { | ||
t.ok(isFunc(pino._Pino)) | ||
test('opts.browser.asObject logs pino-like object to console', function (t) { | ||
t.plan(3) | ||
var info = console.info | ||
console.info = function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 'test') | ||
t.ok(o.time) | ||
console.info = info | ||
} | ||
var instance = require('../browser')({ | ||
browser: { | ||
asObject: true | ||
} | ||
}) | ||
instance.info('test') | ||
}) | ||
test('opts.browser.write func log single string', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 'test') | ||
t.ok(o.time) | ||
} | ||
} | ||
}) | ||
instance.info('test') | ||
t.end() | ||
}) | ||
test('opts.browser.write func string joining', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 'test test2 test3') | ||
t.ok(o.time) | ||
} | ||
} | ||
}) | ||
instance.info('test', 'test2', 'test3') | ||
t.end() | ||
}) | ||
test('opts.browser.write func string object joining', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 'test {"test":"test2"} {"test":"test3"}') | ||
t.ok(o.time) | ||
} | ||
} | ||
}) | ||
instance.info('test', {test: 'test2'}, {test: 'test3'}) | ||
t.end() | ||
}) | ||
test('opts.browser.write func string interpolation', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 'test2 test ({\\"test\\":\\"test3\\"})') | ||
t.ok(o.time) | ||
} | ||
} | ||
}) | ||
instance.info('%s test (%j)', 'test2', {test: 'test3'}) | ||
t.end() | ||
}) | ||
test('opts.browser.write func number', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 1) | ||
t.ok(o.time) | ||
} | ||
} | ||
}) | ||
instance.info(1) | ||
t.end() | ||
}) | ||
test('opts.browser.write func log single object', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.test, 'test') | ||
t.ok(o.time) | ||
} | ||
} | ||
}) | ||
instance.info({test: 'test'}) | ||
t.end() | ||
}) | ||
test('opts.browser.write obj writes to methods corresponding to level', function (t) { | ||
t.plan(3) | ||
var instance = pino({ | ||
browser: { | ||
write: { | ||
error: function (o) { | ||
t.is(o.level, 50) | ||
t.is(o.test, 'test') | ||
t.ok(o.time) | ||
} | ||
} | ||
} | ||
}) | ||
instance.error({test: 'test'}) | ||
t.end() | ||
}) | ||
test('opts.browser.write obj falls back to console where a method is not supplied', function (t) { | ||
t.plan(6) | ||
var info = console.info | ||
console.info = function (o) { | ||
t.is(o.level, 30) | ||
t.is(o.msg, 'test') | ||
t.ok(o.time) | ||
console.info = info | ||
} | ||
var instance = require('../browser')({ | ||
browser: { | ||
write: { | ||
error: function (o) { | ||
t.is(o.level, 50) | ||
t.is(o.test, 'test') | ||
t.ok(o.time) | ||
} | ||
} | ||
} | ||
}) | ||
instance.error({test: 'test'}) | ||
instance.info('test') | ||
t.end() | ||
}) | ||
function levelTest (name) { | ||
@@ -125,0 +265,0 @@ test(name + ' logs', function (t) { |
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
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
122460
2878
312