This commit is contained in:
Matteo Collina 2016-03-05 17:42:18 +01:00
Родитель 32d4015dd4
Коммит 70924e2cee
3 изменённых файлов: 151 добавлений и 4 удалений

101
README.md
Просмотреть файл

@ -33,6 +33,107 @@ setImmediate(info, 'wrapped')
error(new Error('something bad happened'))
```
This produces:
```
{"pid":12244,"hostname":"MBP-di-Matteo","level":30,"msg":"hello world","time":"2016-03-05T16:00:45.858Z","v":0}
{"pid":12244,"hostname":"MBP-di-Matteo","level":50,"msg":"this is at error level","time":"2016-03-05T16:00:45.860Z","v":0}
{"pid":12244,"hostname":"MBP-di-Matteo","level":30,"msg":"the answer is 42","time":"2016-03-05T16:00:45.861Z","v":0}
{"pid":12244,"hostname":"MBP-di-Matteo","level":30,"msg":"hello world","time":"2016-03-05T16:00:45.861Z","v":0,"obj":42}
{"pid":12244,"hostname":"MBP-di-Matteo","level":30,"msg":"hello world","time":"2016-03-05T16:00:45.862Z","v":0,"obj":42,"b":2}
{"pid":12244,"hostname":"MBP-di-Matteo","level":30,"msg":"another","time":"2016-03-05T16:00:45.862Z","v":0,"obj":{"aa":"bbb"}}
{"pid":12244,"hostname":"MBP-di-Matteo","level":50,"msg":"an error","time":"2016-03-05T16:00:45.863Z","v":0,"type":"Error","stack":"Error: an error\n at Object.<anonymous> (/Users/matteo/Repositories/pino/example.js:14:7)\n at Module._compile (module.js:435:26)\n at Object.Module._extensions..js (module.js:442:10)\n at Module.load (module.js:356:32)\n at Function.Module._load (module.js:313:12)\n at Function.Module.runMain (module.js:467:10)\n at startup (node.js:136:18)\n at node.js:963:3"}
{"pid":12244,"hostname":"MBP-di-Matteo","level":30,"msg":"after setImmediate","time":"2016-03-05T16:00:45.865Z","v":0}
```
<a name="api"></a>
## API
* <a href="#constructor"><code><b>pino()</b></code></a>
* <a href="#level"><code>logger.<b>level</b></code></a>
* <a href="#fatal"><code>logger.<b>fatal()</b></code></a>
* <a href="#error"><code>logger.<b>error()</b></code></a>
* <a href="#warn"><code>logger.<b>warn()</b></code></a>
* <a href="#info"><code>logger.<b>info()</b></code></a>
* <a href="#debug"><code>logger.<b>debug()</b></code></a>
* <a href="#trace"><code>logger.<b>trace()</b></code></a>
<a name="constructor"></a>
### pino([opts], [stream])
Returns a new logger. Allowed options are:
* `safe`: avoid error causes by circular references in the object tree,
default `true`
* `name`: the name of the logger, default `undefined`
`stream` is a Writable stream, defaults to `process.stdout`.
<a name="level"></a>
### logger.level
Property to read and write the current level of the logger.
In order of priotity, avaliable levels are:
1. <a href="#fatal">`'fatal'`</a>
2. <a href="#error">`'error'`</a>
3. <a href="#warn">`'warn'`</a>
4. <a href="#info">`'info'`</a>
5. <a href="#debug">`'debug'`</a>
6. <a href="#trace">`'trace'`</a>
By setting a given level (e.g. `logger.level = 'info'`) you enable all
the levels including and above the passed one (in the example, info,
warn, error and fatal). The default is `info`.
<a name="fatal"></a>
### logger.fatal([obj], msg, [...])
Log at `'fatal'` level the given `msg`. If the first argument is an
object, all its properties will be included in the JSON line.
If more args follows `msg`, these will be used to format `msg` using
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
<a name="error"></a>
### logger.error([obj], msg, [...])
Log at `'error'` level the given `msg`. If the first argument is an
object, all its properties will be included in the JSON line.
If more args follows `msg`, these will be used to format `msg` using
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
<a name="warn"></a>
### logger.warn([obj], msg, [...])
Log at `'warn'` level the given `msg`. If the first argument is an
object, all its properties will be included in the JSON line.
If more args follows `msg`, these will be used to format `msg` using
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
<a name="info"></a>
### logger.info([obj], msg, [...])
Log at `'info'` level the given `msg`. If the first argument is an
object, all its properties will be included in the JSON line.
If more args follows `msg`, these will be used to format `msg` using
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
<a name="debug"></a>
### logger.debug([obj], msg, [...])
Log at `'debug'` level the given `msg`. If the first argument is an
object, all its properties will be included in the JSON line.
If more args follows `msg`, these will be used to format `msg` using
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
<a name="trace"></a>
### logger.trace([obj], msg, [...])
Log at `'trace'` level the given `msg`. If the first argument is an
object, all its properties will be included in the JSON line.
If more args follows `msg`, these will be used to format `msg` using
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
## Benchmarks
As far as I know, it is the fastest logger in town:

Просмотреть файл

@ -15,7 +15,11 @@ var levels = {
'trace': 10
}
function pino (stream, opts) {
function pino (opts, stream) {
if (opts && opts._writableState) {
stream = opts
opts = null
}
stream = stream || process.stdout
opts = opts || {}
@ -57,7 +61,7 @@ function pino (stream, opts) {
}
})
result.level = 'info'
result.level = opts.level || 'info'
return result

46
test.js
Просмотреть файл

@ -150,10 +150,10 @@ test('does not explode with a circular ref', function (t) {
})
test('explode with a circular ref with safe = false', function (t) {
var instance = pino(sink(function (chunk, enc, cb) {
var instance = pino({ safe: false }, sink(function (chunk, enc, cb) {
// nothing to check
cb()
}), { safe: false })
}))
var b = {}
var a = {
hello: b
@ -164,3 +164,45 @@ test('explode with a circular ref with safe = false', function (t) {
})
t.end()
})
test('set the name', function (t) {
t.plan(2)
var instance = pino({
name: 'hello'
}, sink(function (chunk, enc, cb) {
t.ok(Date.parse(chunk.time) <= new Date(), 'time is greater than Date.now()')
delete chunk.time
t.deepEqual(chunk, {
pid: pid,
hostname: hostname,
level: 60,
name: 'hello',
msg: 'this is fatal',
v: 0
})
cb()
}))
instance.fatal('this is fatal')
})
test('set the level via constructor', function (t) {
t.plan(4)
var expected = [{
level: 50,
msg: 'this is an error'
}, {
level: 60,
msg: 'this is fatal'
}]
var instance = pino({ level: 'error' }, sink(function (chunk, enc, cb) {
var current = expected.shift()
check(t, chunk, current.level, current.msg)
cb()
}))
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')
})