Permalink
Browse files

Implement exposing level integer value, and number/label mappings

  • Loading branch information...
jsumners committed May 2, 2016
1 parent 090148a commit 3a6ae328a9a47538608180574149dad57b4bd4ae
Showing with 166 additions and 24 deletions.
  1. +35 βˆ’7 README.md
  2. +1 βˆ’1 package.json
  3. +30 βˆ’12 pino.js
  4. +18 βˆ’3 pretty.js
  5. +66 βˆ’1 test.js
  6. +16 βˆ’0 usage.txt
@@ -132,7 +132,7 @@ Then we simply pipe a log file through `pino`:
cat log | pino
```

There's also a transformer flag that converts Epoch timestamps to ISO timestamps.
There's also a transformer flag that converts Epoch timestamps to ISO timestamps.

```sh
cat log | pino -t
@@ -162,6 +162,9 @@ Into this:
* <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 href="#levelVal"><code>logger.<b>levelVal</b></code></a>
* <a href="#levelValues"><code>pino.levels.<b>values</b></code></a>
* <a href="#levelLabels"><code>pino.levels.<b>labels</b></code></a>
* <a href="#reqSerializer"><code>pino.stdSerializers.<b>req</b></code></a>
* <a href="#resSerializer"><code>pino.stdSerializers.<b>res</b></code></a>
* <a href="#errSerializer"><code>pino.stdSerializers.<b>err</b></code></a>
@@ -216,7 +219,7 @@ Child loggers use the same output stream as the parent and inherit
the current log level of the parent at the time they are spawned.

From v2.x.x the log level of a child is mutable (whereas in
v1.x.x it was immutable), and can be set independently of the parent.
v1.x.x it was immutable), and can be set independently of the parent.

For example

@@ -331,10 +334,35 @@ 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="levelVal"></a>
### logger.levelVal

Returns the integer value for the logger instance's logging level.

<a name="levelValues"></a>
### pino.levels.values

Returns the mappings of level names to their respective internal number
representation. For example:

```js
pino.levels.values.error === 50 // true
```

<a name="levelLabels"></a>
### pino.levels.labels

Returns the mappings of level internal level numbers to their string
representations. For example:

```js
pino.levels.labels[50] === 'error' // true
```

<a name="reqSerializer"></a>
### pino.stdSerializers.req

Generates a JSONifiable object from the HTTP `request` object passed to
Generates a JSONifiable object from the HTTP `request` object passed to
the `createServer` callback of Node's HTTP server.

It returns an object in the form:
@@ -363,7 +391,7 @@ It returns an object in the form:
<a name="resSerializer"></a>
### pino.stdSerializers.res

Generates a JSONifiable object from the HTTP `response` object passed to
Generates a JSONifiable object from the HTTP `response` object passed to
the `createServer` callback of Node's HTTP server.

It returns an object in the form:
@@ -579,7 +607,7 @@ See the [koa-pino-logger v2 readme](https://github.com/davidmarkclements/koa-pin
<a name="rotate"></a>
## How do I rotate log files

Use a separate tool for log rotation.
Use a separate tool for log rotation.

We recommend [logrotate](https://github.com/logrotate/logrotate)

@@ -678,8 +706,8 @@ $ cat my-log | pino -t
This equates to the same log output that Bunyan supplies.
One of Pino's performance tricks is to avoid building objects and stringifying
them, so we're building strings instead. This is why duplicate keys between
One of Pino's performance tricks is to avoid building objects and stringifying
them, so we're building strings instead. This is why duplicate keys between
parents and children will end up in log output.
<a name="team"></a>
@@ -1,6 +1,6 @@
{
"name": "pino",
"version": "2.2.1",
"version": "2.2.2",
"description": "fast and simple logger",
"main": "pino.js",
"browser": {
42 pino.js
@@ -70,6 +70,15 @@ function pino (opts, stream) {
return logger
}

Object.defineProperty(pino, 'levels', {
get: function getLevels () {
return {
values: levels,
labels: nums
}
}
})

function Pino (level, stream, serializers, stringify, end, name, hostname, slowtime, chindings, cache, formatOpts) {
this.stream = stream
this.serializers = serializers
@@ -91,26 +100,35 @@ Pino.prototype.info = genLog(levels.info)
Pino.prototype.debug = genLog(levels.debug)
Pino.prototype.trace = genLog(levels.trace)

Object.defineProperty(Pino.prototype, 'levelVal', {
get: function getLevelVal () {
return this._levelVal
},
set: function setLevelVal (num) {
if (typeof num === 'string') { return this._setLevel(num) }
this._levelVal = num

for (var key in levels) {
if (num > levels[key]) {
this[key] = noop
continue
}
this[key] = Pino.prototype[key]
}
}
})

Pino.prototype._setLevel = function _setLevel (level) {
if (typeof level === 'number') { level = nums[level] }
this._level = levels[level]

if (!this._level) {
if (!levels[level]) {
throw new Error('unknown level ' + level)
}

var num = levels[level]
for (var key in levels) {
if (num > levels[key]) {
this[key] = noop
} else if (this[key] === noop) {
this[key] = Pino.prototype[key]
}
}
this.levelVal = levels[level]
}

Pino.prototype._getLevel = function _getLevel (level) {
return nums[this._level]
return nums[this.levelVal]
}

Object.defineProperty(Pino.prototype, 'level', {
@@ -115,7 +115,22 @@ function pretty (opts) {
module.exports = pretty

if (require.main === module) {
process.stdin.pipe(pretty({
timeTransOnly: ~process.argv.indexOf('-t')
})).pipe(process.stdout)
if (process.argv.length < 3 || arg('-h') || arg('--help')) {
usage().pipe(process.stdout)
} else if (arg('-v') || arg('--version')) {
console.log(require('./package.json').version)
} else {
process.stdin.pipe(pretty({
timeTransOnly: arg('-t')
})).pipe(process.stdout)
}
}

function usage () {
return require('fs')
.createReadStream(require('path').join(__dirname, 'usage.txt'))
}

function arg (s) {
return !!~process.argv.indexOf(s)
}
67 test.js
@@ -166,7 +166,7 @@ levelTest('info', 30)
levelTest('debug', 20)
levelTest('trace', 10)

test('set the level', function (t) {
test('set the level by string', function (t) {
t.plan(4)
var expected = [{
level: 50,
@@ -187,6 +187,71 @@ test('set the level', function (t) {
instance.fatal('this is fatal')
})

test('set the level by number', function (t) {
t.plan(4)
var expected = [{
level: 50,
msg: 'this is an error'
}, {
level: 60,
msg: 'this is fatal'
}]
var instance = pino(sink(function (chunk, enc, cb) {
var current = expected.shift()
check(t, chunk, current.level, current.msg)
cb()
}))

instance.levelVal = 50
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')
})

test('set the level by number via string method', function (t) {
t.plan(4)
var expected = [{
level: 50,
msg: 'this is an error'
}, {
level: 60,
msg: 'this is fatal'
}]
var instance = pino(sink(function (chunk, enc, cb) {
var current = expected.shift()
check(t, chunk, current.level, current.msg)
cb()
}))

instance.level = 50
instance.info('hello world')
instance.error('this is an error')
instance.fatal('this is fatal')
})

test('exposes level string mappings', function (t) {
t.plan(1)
t.equal(pino.levels.values.error, 50)
})

test('exposes level number mappings', function (t) {
t.plan(1)
t.equal(pino.levels.labels[50], 'error')
})

test('returns level integer', function (t) {
t.plan(1)
var instance = pino({ level: 'error' })
t.equal(instance.levelVal, 50)
})

test('child returns level integer', function (t) {
t.plan(1)
var parent = pino({ level: 'error' })
var child = parent.child({ foo: 'bar' })
t.equal(child.levelVal, 50)
})

test('does not explode with a circular ref', function (t) {
var instance = pino(sink(function (chunk, enc, cb) {
// nothing to check
@@ -0,0 +1,16 @@

Pino

To prettify logs, simply pipe a log file through pino:

cat log | pino

To converts Epoch timestamps to ISO timestamps use the -t flag

cat log | pino -t

Flags
-h | --help Display Help
-v | --version Display Version
-t Convert Epoch timestamps to ISO

0 comments on commit 3a6ae32

Please sign in to comment.