Skip to content

Commit 2f5bf87

Browse files
authored
Prefer Pino logger over Bunyan (#1841)
BREAKING CHANGE: removes `RequestCaptureStream` and replaces `Bunyan` with `Pino`
1 parent 12be9e2 commit 2f5bf87

File tree

23 files changed

+154
-602
lines changed

23 files changed

+154
-602
lines changed

docs/guides/8to9guide.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ permalink: /docs/8to9/
55

66
## Introduction
77

8-
restify `9.x` comes with `async/await` support!
8+
Restify `9.x` comes with `async/await` support, `pino` and more!
99

1010
## Breaking Changes
1111

@@ -94,3 +94,18 @@ server.use(async (req, res, next) => {
9494
});
9595
});
9696
````
97+
98+
### Remove `RequestCaptureStream`
99+
100+
Removes `RequestCaptureStream` from Restify core.
101+
102+
### Use `Pino` as default logger (removes dependency on `Bunyan`)
103+
104+
[Pino](https://github.com/pinojs/pino) is well maintained, performance-focused,
105+
compatible API. It does have a few key differences from `Bunyan`:
106+
107+
- As a performance optimization, it stores bindings a single serialized `string`,
108+
while `Bunyan` stores them as object keys;
109+
- It uses a `setter` to set the log level, `Bunyan` uses a method;
110+
- It only accepts one stream. If you need the multi-stream functionality, you
111+
must use [pino-multistream](https://github.com/pinojs/pino-multi-stream).

docs/guides/client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ Options:
120120
|dtrace|Object|node-dtrace-provider handle|
121121
|gzip|Object|Will compress data when sent using `content-encoding: gzip`|
122122
|headers|Object|HTTP headers to set in all requests|
123-
|log|Object|[bunyan](https://github.com/trentm/node-bunyan) instance|
123+
|log|Object|[pino](https://github.com/pinojs/pino) instance|
124124
|retry|Object|options to provide to node-retry;"false" disables retry; defaults to 4 retries|
125125
|signRequest|Function|synchronous callback for interposing headers before request is sent|
126126
|url|String|Fully-qualified URL to connect to|

examples/dtrace/demo.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
// 512 | 0
7070

7171
var restify = require('../../lib');
72-
var Logger = require('bunyan');
72+
var Logger = require('pino');
7373

7474
///--- Globals
7575

@@ -80,8 +80,7 @@ var NAME = 'exampleapp';
8080
var log = new Logger({
8181
name: NAME,
8282
level: 'trace',
83-
service: NAME,
84-
serializers: restify.bunyan.serializers
83+
base: { service: NAME }
8584
});
8685

8786
var server = restify.createServer({

examples/http2/http2.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var path = require('path');
22
var fs = require('fs');
3-
var bunyan = require('bunyan');
3+
var pino = require('pino');
44
var restify = require('../../lib');
55

66
var srv = restify.createServer({
@@ -21,10 +21,10 @@ srv.on(
2121
restify.plugins.auditLogger({
2222
event: 'after',
2323
body: true,
24-
log: bunyan.createLogger({
25-
name: 'audit',
26-
stream: process.stdout
27-
})
24+
log: pino(
25+
{ name: 'audit' },
26+
process.stdout
27+
)
2828
})
2929
);
3030

examples/spdy/spdy.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var path = require('path');
22
var fs = require('fs');
3-
var bunyan = require('bunyan');
3+
var pino = require('pino');
44
var restify = require('../../lib');
55

66
var srv = restify.createServer({
@@ -21,10 +21,7 @@ srv.on(
2121
restify.plugins.auditLogger({
2222
event: 'after',
2323
body: true,
24-
log: bunyan.createLogger({
25-
name: 'audit',
26-
stream: process.stdout
27-
})
24+
log: pino({name: 'audit'})
2825
})
2926
);
3027

examples/todoapp/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ First, this has a `package.json`, so you'll need to run `npm install` in the
3737
directory. Once you've done that, to get started _and_ see audit logs on your
3838
terminal, run it like this:
3939

40-
$ node main.js 2>&1 | bunyan
40+
$ node main.js 2>&1 | npx pino-pretty
4141

4242
If you want to see all the built in restify tracing:
4343

44-
$ node main.js -vv 2>&1 | bunyan
44+
$ node main.js -vv 2>&1 | npx pino-pretty
4545

4646
By default, this program writes to a directory in `/tmp`, but you can override
4747
with a `-d` option. Additionally, by default it does not require
4848
authentication, but you can require that with:
4949

50-
$ node main.js -u admin -z secret 2>&1 | bunyan
50+
$ node main.js -u admin -z secret 2>&1 | npx pino-pretty
5151

52-
Lastly, re: the `2>&1 | bunyan` bit. In production, you assuredly would *not*
53-
want to pipe to the [bunyan](https://github.com/trentm/node-bunyan) CLI, but
52+
Lastly, re: the `2>&1 | npx pino-pretty` bit. In production, you assuredly would *not*
53+
want to pipe to the [pino-pretty](https://github.com/pinojs/pino-pretty) CLI, but
5454
rather capture the audit records in their raw form, so they would be easy to
5555
post process and perform analytics on. Like all UNIX programs should, this
5656
example writes "informational" messages to `stderr`, and `audit` records to
@@ -61,7 +61,7 @@ stdout. It's up to you to redirect them as appropriate.
6161

6262
Let's get the full magilla (i.e., with auth) running:
6363

64-
$ node main.js -u admin -z secret 2>&1 | bunyan
64+
$ node main.js -u admin -z secret 2>&1 | npx pino-pretty
6565

6666
Also, before we go any further, install the
6767
[json](https://github.com/trentm/json) tool as all the examples below use that.

examples/todoapp/lib/server.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var path = require('path');
55
var util = require('util');
66

77
var assert = require('assert-plus');
8-
var bunyan = require('bunyan');
8+
var pino = require('pino');
99
var restify = require('restify');
1010
var errors = require('restify-errors');
1111

@@ -326,7 +326,7 @@ function createServer(options) {
326326
// Handles annoying user agents (curl)
327327
server.pre(restify.pre.userAgentConnection());
328328

329-
// Set a per request bunyan logger (with requestid filled in)
329+
// Set a per request pino logger (with requestid filled in)
330330
server.use(restify.requestLogger());
331331

332332
// Allow 5 requests/second by IP, and burst to 10
@@ -422,11 +422,7 @@ function createServer(options) {
422422
'after',
423423
restify.auditLogger({
424424
body: true,
425-
log: bunyan.createLogger({
426-
level: 'info',
427-
name: 'todoapp-audit',
428-
stream: process.stdout
429-
})
425+
log: pino({ level: 'info', name: 'todoapp-audit' })
430426
})
431427
);
432428
}

examples/todoapp/main.js

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var fs = require('fs');
44
var path = require('path');
55

6-
var bunyan = require('bunyan');
6+
var pino = require('pino');
77
var getopt = require('posix-getopt');
88
var restify = require('restify');
99

@@ -13,32 +13,7 @@ var todo = require('./lib');
1313

1414
var NAME = 'todoapp';
1515

16-
// In true UNIX fashion, debug messages go to stderr, and audit records go
17-
// to stdout, so you can split them as you like in the shell
18-
var LOG = bunyan.createLogger({
19-
name: NAME,
20-
streams: [
21-
{
22-
level: process.env.LOG_LEVEL || 'info',
23-
stream: process.stderr
24-
},
25-
{
26-
// This ensures that if we get a WARN or above all debug records
27-
// related to that request are spewed to stderr - makes it nice
28-
// filter out debug messages in prod, but still dump on user
29-
// errors so you can debug problems
30-
level: 'debug',
31-
type: 'raw',
32-
stream: new restify.bunyan.RequestCaptureStream({
33-
level: bunyan.WARN,
34-
maxRecords: 100,
35-
maxRequestIds: 1000,
36-
stream: process.stderr
37-
})
38-
}
39-
],
40-
serializers: restify.bunyan.serializers
41-
});
16+
var LOG = pino({ name: NAME });
4217

4318
///--- Helpers
4419

@@ -49,7 +24,7 @@ var LOG = bunyan.createLogger({
4924
* the 'verbose' or '-v' option afflicts the log level, repeatedly. So you
5025
* can run something like:
5126
*
52-
* node main.js -p 80 -vv 2>&1 | bunyan
27+
* node main.js -p 80 -vv 2>&1 | npx pino-pretty
5328
*
5429
* And the log level will be set to TRACE.
5530
*/
@@ -79,9 +54,9 @@ function parseOptions() {
7954
case 'v':
8055
// Allows us to set -vvv -> this little hackery
8156
// just ensures that we're never < TRACE
82-
LOG.level(Math.max(bunyan.TRACE, LOG.level() - 10));
57+
LOG.level(Math.max(pino.levels.values.trace, LOG.level - 10));
8358

84-
if (LOG.level() <= bunyan.DEBUG) {
59+
if (LOG.level <= pino.levels.values.debug) {
8560
LOG = LOG.child({ src: true });
8661
}
8762
break;

examples/todoapp/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
"main": "main.js",
66
"dependencies": {
77
"assert-plus": "0.2.0",
8-
"bunyan": "1.8.13",
8+
"pino": "^6.3.2",
99
"posix-getopt": "1.2.0",
10-
"restify-errors": "^3.1.0",
10+
"restify-errors": "^4.0.0",
1111
"restify": "^4.0.3"
1212
},
1313
"devDependencies": {
14-
"nodeunit": "0.9.1"
14+
"nodeunit": "0.9.1",
15+
"pino-pretty": "^4.0.0"
1516
},
1617
"scripts": {
17-
"start": "node main.js 2>&1 | bunyan",
18+
"start": "node main.js 2>&1 | pino-pretty",
1819
"test": "nodeunit test"
1920
},
2021
"author": "Mark Cavage",

examples/todoapp/test/todo.test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var fs = require('fs');
44

5-
var bunyan = require('bunyan');
5+
var pino = require('pino');
66
var restify = require('restify');
77

88
var todo = require('../lib');
@@ -17,11 +17,9 @@ var SOCK = '/tmp/.todo_sock';
1717
///--- Tests
1818

1919
exports.setup = function(t) {
20-
var log = bunyan.createLogger({
20+
var log = pino({
2121
name: 'todo_unit_test',
22-
level: process.env.LOG_LEVEL || 'info',
23-
serializers: restify.bunyan.serializers,
24-
stream: process.stdout
22+
level: process.env.LOG_LEVEL || 'info'
2523
});
2624

2725
fs.mkdir(DIR, function(err) {

0 commit comments

Comments
 (0)