Skip to content

Commit 3a6ae32

Browse files
committed
Implement exposing level integer value, and number/label mappings
1 parent 090148a commit 3a6ae32

6 files changed

Lines changed: 166 additions & 24 deletions

File tree

README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Then we simply pipe a log file through `pino`:
132132
cat log | pino
133133
```
134134

135-
There's also a transformer flag that converts Epoch timestamps to ISO timestamps.
135+
There's also a transformer flag that converts Epoch timestamps to ISO timestamps.
136136

137137
```sh
138138
cat log | pino -t
@@ -162,6 +162,9 @@ Into this:
162162
* <a href="#info"><code>logger.<b>info()</b></code></a>
163163
* <a href="#debug"><code>logger.<b>debug()</b></code></a>
164164
* <a href="#trace"><code>logger.<b>trace()</b></code></a>
165+
* <a href="#levelVal"><code>logger.<b>levelVal</b></code></a>
166+
* <a href="#levelValues"><code>pino.levels.<b>values</b></code></a>
167+
* <a href="#levelLabels"><code>pino.levels.<b>labels</b></code></a>
165168
* <a href="#reqSerializer"><code>pino.stdSerializers.<b>req</b></code></a>
166169
* <a href="#resSerializer"><code>pino.stdSerializers.<b>res</b></code></a>
167170
* <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
216219
the current log level of the parent at the time they are spawned.
217220

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

221224
For example
222225

@@ -331,10 +334,35 @@ object, all its properties will be included in the JSON line.
331334
If more args follows `msg`, these will be used to format `msg` using
332335
[`util.format`](https://nodejs.org/api/util.html#util_util_format_format)
333336

337+
<a name="levelVal"></a>
338+
### logger.levelVal
339+
340+
Returns the integer value for the logger instance's logging level.
341+
342+
<a name="levelValues"></a>
343+
### pino.levels.values
344+
345+
Returns the mappings of level names to their respective internal number
346+
representation. For example:
347+
348+
```js
349+
pino.levels.values.error === 50 // true
350+
```
351+
352+
<a name="levelLabels"></a>
353+
### pino.levels.labels
354+
355+
Returns the mappings of level internal level numbers to their string
356+
representations. For example:
357+
358+
```js
359+
pino.levels.labels[50] === 'error' // true
360+
```
361+
334362
<a name="reqSerializer"></a>
335363
### pino.stdSerializers.req
336364

337-
Generates a JSONifiable object from the HTTP `request` object passed to
365+
Generates a JSONifiable object from the HTTP `request` object passed to
338366
the `createServer` callback of Node's HTTP server.
339367

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

366-
Generates a JSONifiable object from the HTTP `response` object passed to
394+
Generates a JSONifiable object from the HTTP `response` object passed to
367395
the `createServer` callback of Node's HTTP server.
368396

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

582-
Use a separate tool for log rotation.
610+
Use a separate tool for log rotation.
583611

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

@@ -678,8 +706,8 @@ $ cat my-log | pino -t
678706
679707
This equates to the same log output that Bunyan supplies.
680708
681-
One of Pino's performance tricks is to avoid building objects and stringifying
682-
them, so we're building strings instead. This is why duplicate keys between
709+
One of Pino's performance tricks is to avoid building objects and stringifying
710+
them, so we're building strings instead. This is why duplicate keys between
683711
parents and children will end up in log output.
684712
685713
<a name="team"></a>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pino",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"description": "fast and simple logger",
55
"main": "pino.js",
66
"browser": {

pino.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ function pino (opts, stream) {
7070
return logger
7171
}
7272

73+
Object.defineProperty(pino, 'levels', {
74+
get: function getLevels () {
75+
return {
76+
values: levels,
77+
labels: nums
78+
}
79+
}
80+
})
81+
7382
function Pino (level, stream, serializers, stringify, end, name, hostname, slowtime, chindings, cache, formatOpts) {
7483
this.stream = stream
7584
this.serializers = serializers
@@ -91,26 +100,35 @@ Pino.prototype.info = genLog(levels.info)
91100
Pino.prototype.debug = genLog(levels.debug)
92101
Pino.prototype.trace = genLog(levels.trace)
93102

103+
Object.defineProperty(Pino.prototype, 'levelVal', {
104+
get: function getLevelVal () {
105+
return this._levelVal
106+
},
107+
set: function setLevelVal (num) {
108+
if (typeof num === 'string') { return this._setLevel(num) }
109+
this._levelVal = num
110+
111+
for (var key in levels) {
112+
if (num > levels[key]) {
113+
this[key] = noop
114+
continue
115+
}
116+
this[key] = Pino.prototype[key]
117+
}
118+
}
119+
})
120+
94121
Pino.prototype._setLevel = function _setLevel (level) {
95122
if (typeof level === 'number') { level = nums[level] }
96-
this._level = levels[level]
97123

98-
if (!this._level) {
124+
if (!levels[level]) {
99125
throw new Error('unknown level ' + level)
100126
}
101-
102-
var num = levels[level]
103-
for (var key in levels) {
104-
if (num > levels[key]) {
105-
this[key] = noop
106-
} else if (this[key] === noop) {
107-
this[key] = Pino.prototype[key]
108-
}
109-
}
127+
this.levelVal = levels[level]
110128
}
111129

112130
Pino.prototype._getLevel = function _getLevel (level) {
113-
return nums[this._level]
131+
return nums[this.levelVal]
114132
}
115133

116134
Object.defineProperty(Pino.prototype, 'level', {

pretty.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,22 @@ function pretty (opts) {
115115
module.exports = pretty
116116

117117
if (require.main === module) {
118-
process.stdin.pipe(pretty({
119-
timeTransOnly: ~process.argv.indexOf('-t')
120-
})).pipe(process.stdout)
118+
if (process.argv.length < 3 || arg('-h') || arg('--help')) {
119+
usage().pipe(process.stdout)
120+
} else if (arg('-v') || arg('--version')) {
121+
console.log(require('./package.json').version)
122+
} else {
123+
process.stdin.pipe(pretty({
124+
timeTransOnly: arg('-t')
125+
})).pipe(process.stdout)
126+
}
127+
}
128+
129+
function usage () {
130+
return require('fs')
131+
.createReadStream(require('path').join(__dirname, 'usage.txt'))
132+
}
133+
134+
function arg (s) {
135+
return !!~process.argv.indexOf(s)
121136
}

test.js

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ levelTest('info', 30)
166166
levelTest('debug', 20)
167167
levelTest('trace', 10)
168168

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

190+
test('set the level by number', function (t) {
191+
t.plan(4)
192+
var expected = [{
193+
level: 50,
194+
msg: 'this is an error'
195+
}, {
196+
level: 60,
197+
msg: 'this is fatal'
198+
}]
199+
var instance = pino(sink(function (chunk, enc, cb) {
200+
var current = expected.shift()
201+
check(t, chunk, current.level, current.msg)
202+
cb()
203+
}))
204+
205+
instance.levelVal = 50
206+
instance.info('hello world')
207+
instance.error('this is an error')
208+
instance.fatal('this is fatal')
209+
})
210+
211+
test('set the level by number via string method', function (t) {
212+
t.plan(4)
213+
var expected = [{
214+
level: 50,
215+
msg: 'this is an error'
216+
}, {
217+
level: 60,
218+
msg: 'this is fatal'
219+
}]
220+
var instance = pino(sink(function (chunk, enc, cb) {
221+
var current = expected.shift()
222+
check(t, chunk, current.level, current.msg)
223+
cb()
224+
}))
225+
226+
instance.level = 50
227+
instance.info('hello world')
228+
instance.error('this is an error')
229+
instance.fatal('this is fatal')
230+
})
231+
232+
test('exposes level string mappings', function (t) {
233+
t.plan(1)
234+
t.equal(pino.levels.values.error, 50)
235+
})
236+
237+
test('exposes level number mappings', function (t) {
238+
t.plan(1)
239+
t.equal(pino.levels.labels[50], 'error')
240+
})
241+
242+
test('returns level integer', function (t) {
243+
t.plan(1)
244+
var instance = pino({ level: 'error' })
245+
t.equal(instance.levelVal, 50)
246+
})
247+
248+
test('child returns level integer', function (t) {
249+
t.plan(1)
250+
var parent = pino({ level: 'error' })
251+
var child = parent.child({ foo: 'bar' })
252+
t.equal(child.levelVal, 50)
253+
})
254+
190255
test('does not explode with a circular ref', function (t) {
191256
var instance = pino(sink(function (chunk, enc, cb) {
192257
// nothing to check

usage.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Pino
3+
4+
To prettify logs, simply pipe a log file through pino:
5+
6+
cat log | pino
7+
8+
To converts Epoch timestamps to ISO timestamps use the -t flag
9+
10+
cat log | pino -t
11+
12+
Flags
13+
-h | --help Display Help
14+
-v | --version Display Version
15+
-t Convert Epoch timestamps to ISO
16+

0 commit comments

Comments
 (0)