Comparing version 4.9.0 to 4.10.0
@@ -21,2 +21,3 @@ # Table of Contents | ||
* [.levels.labels](#levelLabels) | ||
* [.isLevelEnabled](#isLevelEnabled) | ||
* [LOG_VERSION](#log_version) | ||
@@ -81,2 +82,6 @@ * [.stdSerializers](#stdSerializers) | ||
* `browser` (Object): browser only, may have `asObject` and `write` keys, see [Pino in the Browser](../readme.md#browser) | ||
* `base` (Object): key-value object added as child logger to each log line. If set to `null` the `base` child logger is not added . Default: | ||
* `pid` (process.pid) | ||
* `hostname` (os.hostname) | ||
* `name` of logger if supplied as option | ||
* `crlf` (boolean): logs newline delimited JSON with `\r\n` instead of `\n`. Default: `false`. | ||
@@ -111,4 +116,6 @@ + `stream` (Writable): a writable stream where the logs will be written. | ||
timestamp to ISO 8601 date format, and reserialize the JSON (equivalent to `pino -t`). | ||
* `formatter` (function): a custom function to format the line, is passed the | ||
JSON object as an argument and should return a string value. | ||
* `formatter` (function): a custom function to format the line. It's passed 2 arguments, | ||
JSON object log data and an options object | ||
that [exposes utility functions](https://github.com/pinojs/pino/blob/master/pretty.js#L110). | ||
It should return a string value. | ||
* `levelFirst` (boolean): if set to `true`, it will print the name of the log | ||
@@ -475,2 +482,14 @@ level as the first field in the log line. Default: `false`. | ||
<a id="isLevelEnabled"></a> | ||
## .isLevelEnabled(logLevel) | ||
### Example: | ||
```js | ||
if (logger.isLevelEnabled('debug')) logger.debug('conditional log') | ||
``` | ||
### Discussion: | ||
A utility method for determining if a given log level will write to the output | ||
stream. | ||
<a id="log_version"></a> | ||
@@ -477,0 +496,0 @@ ## .LOG_VERSION |
@@ -21,3 +21,3 @@ 'use strict' | ||
var lscache = Object.keys(nums).reduce(function (o, k) { | ||
o[k] = flatstr('"level":' + Number(k)) | ||
o[k] = flatstr('{"level":' + Number(k)) | ||
return o | ||
@@ -24,0 +24,0 @@ }, {}) |
'use strict' | ||
var flatstr = require('flatstr') | ||
var format = require('quick-format-unescaped') | ||
var util = require('util') | ||
var pid = process.pid | ||
var os = require('os') | ||
var hostname = os.hostname() | ||
var baseLog = flatstr('{"pid":' + pid + ',"hostname":"' + hostname + '",') | ||
var levels = require('./levels') | ||
@@ -33,6 +28,2 @@ var serializers = require('./serializers') | ||
self._setLevel(opts.level) | ||
var str = baseLog + | ||
(self.name === undefined ? '' : '"name":' + self.stringify(self.name) + ',') | ||
Number(str) | ||
self._baseLog = str | ||
} | ||
@@ -39,0 +30,0 @@ |
{ | ||
"name": "pino", | ||
"version": "4.9.0", | ||
"version": "4.10.0", | ||
"description": "super fast, all natural json logger", | ||
@@ -5,0 +5,0 @@ "main": "pino.js", |
31
pino.js
'use strict' | ||
var os = require('os') | ||
var EventEmitter = require('events').EventEmitter | ||
@@ -35,2 +36,6 @@ var stringifySafe = require('fast-safe-stringify') | ||
}, | ||
base: { | ||
pid: process.pid, | ||
hostname: os.hostname() | ||
}, | ||
enabled: true, | ||
@@ -132,3 +137,3 @@ messageKey: 'msg' | ||
msg = !msg && objError ? obj.message : msg || undefined | ||
var data = this._baseLog + this._lscache[num] + this.time() | ||
var data = this._lscache[num] + this.time() | ||
if (msg !== undefined) { | ||
@@ -241,3 +246,3 @@ // JSON.stringify is safe here | ||
this.levels.labels[lvl] = name | ||
this._lscache[lvl] = flatstr('"level":' + Number(lvl)) | ||
this._lscache[lvl] = flatstr('{"level":' + Number(lvl)) | ||
Object.defineProperty(this, name, { | ||
@@ -256,2 +261,11 @@ value: lvl < this._levelVal ? tools.noop : tools.genLog(lvl), | ||
function isLevelEnabled (logLevel) { | ||
var logLevelVal = this.levels.values[logLevel] | ||
return logLevelVal && (logLevelVal >= this._levelVal) | ||
} | ||
Object.defineProperty(pinoPrototype, 'isLevelEnabled', { | ||
enumerable: true, | ||
value: isLevelEnabled | ||
}) | ||
function pino (opts, stream) { | ||
@@ -266,2 +280,3 @@ var iopts = opts | ||
if (iopts.extreme && iopts.prettyPrint) throw Error('cannot enable pretty print in extreme mode') | ||
if (stream && iopts.prettyPrint) throw Error('cannot enable pretty print when stream specified') | ||
istream = istream || process.stdout | ||
@@ -346,2 +361,14 @@ if (iopts.prettyPrint) { | ||
var base = (typeof iopts.base === 'object') ? iopts.base : defaultOptions.base | ||
if (iopts.name !== undefined) { | ||
base = Object.assign({}, base, { | ||
name: iopts.name | ||
}) | ||
} | ||
if (base !== null) { | ||
instance = instance.child(base) | ||
} | ||
return instance | ||
@@ -348,0 +375,0 @@ } |
@@ -100,9 +100,5 @@ 'use strict' | ||
// pass through | ||
return line + '\n' | ||
return line + eol | ||
} | ||
if (formatter) { | ||
return opts.formatter(parsed.value) + eol | ||
} | ||
if (timeTransOnly) { | ||
@@ -117,2 +113,14 @@ value.time = asISODate(value.time) | ||
if (formatter) { | ||
return opts.formatter(value, { | ||
prefix: line, | ||
chalk: ctx, | ||
withSpaces: withSpaces, | ||
filter: filter, | ||
formatTime: formatTime, | ||
asColoredText: asColoredText, | ||
asColoredLevel: asColoredLevel | ||
}) + eol | ||
} | ||
line += ' (' | ||
@@ -127,3 +135,3 @@ if (value.name) { | ||
} | ||
line += '\n' | ||
line += eol | ||
if (value.type === 'Error') { | ||
@@ -155,6 +163,10 @@ line += ' ' + withSpaces(value.stack, eol) + eol | ||
function asColoredLevel (value) { | ||
return asColoredText(value, levelColors.hasOwnProperty(value.level) ? levels[value.level] : levels.default) | ||
} | ||
function asColoredText (value, text) { | ||
if (levelColors.hasOwnProperty(value.level)) { | ||
return levelColors[value.level](levels[value.level]) | ||
return levelColors[value.level](text) | ||
} else { | ||
return levelColors.default(levels.default) | ||
return levelColors.default(text) | ||
} | ||
@@ -161,0 +173,0 @@ } |
@@ -275,2 +275,43 @@ 'use strict' | ||
test('set the base', function (t) { | ||
t.plan(2) | ||
var instance = pino({ | ||
base: { | ||
a: 'b' | ||
} | ||
}, sink(function (chunk, enc, cb) { | ||
t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()') | ||
delete chunk.time | ||
t.deepEqual(chunk, { | ||
a: 'b', | ||
level: 60, | ||
msg: 'this is fatal', | ||
v: 1 | ||
}) | ||
cb() | ||
})) | ||
instance.fatal('this is fatal') | ||
}) | ||
test('set the base to null', function (t) { | ||
t.plan(2) | ||
var instance = pino({ | ||
base: null | ||
}, sink(function (chunk, enc, cb) { | ||
t.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()') | ||
delete chunk.time | ||
t.deepEqual(chunk, { | ||
level: 60, | ||
msg: 'this is fatal', | ||
v: 1 | ||
}) | ||
cb() | ||
})) | ||
instance.fatal('this is fatal') | ||
}) | ||
test('throw if creating child without bindings', function (t) { | ||
@@ -277,0 +318,0 @@ t.plan(1) |
@@ -68,4 +68,11 @@ 'use strict' | ||
test('pino transform can format with a custom function', function (t) { | ||
t.plan(1) | ||
var prettier = pretty({ formatter: function (line) { | ||
t.plan(8) | ||
var prettier = pretty({ formatter: function (line, opts) { | ||
t.ok(opts.prefix.indexOf('INFO') > -1, 'prefix contains level') | ||
t.ok(typeof opts.chalk.white === 'function', 'chalk instance') | ||
t.ok(typeof opts.withSpaces === 'function', 'withSpaces function') | ||
t.ok(typeof opts.filter === 'function', 'filter function') | ||
t.ok(typeof opts.formatTime === 'function', 'formatTime function') | ||
t.ok(typeof opts.asColoredText === 'function', 'asColoredText function') | ||
t.ok(typeof opts.asColoredLevel === 'function', 'asColoredLevel function') | ||
return 'msg: ' + line.msg + ', foo: ' + line.foo | ||
@@ -192,3 +199,3 @@ } }) | ||
test('accept customLogLevvel', function (t) { | ||
test('accept customLogLevel', function (t) { | ||
t.plan(1) | ||
@@ -254,1 +261,10 @@ var prettier = pretty() | ||
}) | ||
test('throws error when enabled with stream specified', function (t) { | ||
t.plan(1) | ||
var logStream = writeStream(function (s, enc, cb) { | ||
cb() | ||
}) | ||
t.throws(() => pino({prettyPrint: true}, logStream), {}) | ||
}) |
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
171112
47
4094