Skip to content

Commit 0dd6d7e

Browse files
committed
fix: swap out bunyan for pino
1 parent 5528f05 commit 0dd6d7e

File tree

4 files changed

+244
-402
lines changed

4 files changed

+244
-402
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
},
3737
"dependencies": {
3838
"bluebird": "^3.7.1",
39-
"bunyan": "^1.8.12",
4039
"callback-queue": "^3.0.0",
4140
"denque": "^1.4.1",
4241
"ioredis-lock": "^4.0.0",
4342
"lodash": "^4.17.15",
43+
"pino": "^6.3.2",
4444
"serialize-error": "^7.0.1"
4545
},
4646
"devDependencies": {

src/callback-queue.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function call(queueName, args, logger) {
1818
}
1919

2020
// these are async anyways - gonna schedule them
21-
logger.debug('Calling %s callback with args', queueName, args);
21+
logger.debug('Calling %s callback with args %j', queueName, args);
2222
callback(...args);
2323

2424
// clean local queue
@@ -89,7 +89,7 @@ exports.createConsumer = function createConsumer(redis, pubsubChannel, logger) {
8989
logger.info('Subscribed to channel %s', pubsubChannel);
9090
})
9191
.catch((err) => {
92-
logger.fatal('Failed to subsctibe to pubsub channel:', err);
92+
logger.fatal({ err }, 'Failed to subsctibe to pubsub channel');
9393
return Promise.delay(250).then(connect);
9494
});
9595

@@ -108,7 +108,7 @@ exports.createConsumer = function createConsumer(redis, pubsubChannel, logger) {
108108

109109
const [key, args] = message;
110110
if (!key || !isArray(args)) {
111-
logger.warn('Malformed message passed: no key or args.', message);
111+
logger.warn({ redisMsg: message }, 'Malformed message passed: no key or args');
112112
return;
113113
}
114114

src/distributed-callback-queue.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Promise = require('bluebird');
22
const redislock = require('ioredis-lock');
33
const Redis = require('ioredis');
4-
const bunyan = require('bunyan');
4+
const pino = require('pino');
55
const assert = require('assert');
66

77
// may only use redis with bluebird promise
@@ -40,9 +40,9 @@ const isTimeoutError = (e) => e === TimeoutError;
4040
* @param {Number} timeout - defaults to 10000
4141
* @param {Number} retries - defaults to 0
4242
* @param {Number} delay - defaults to 100
43-
* @param {Object|Boolean} log: sets up logger. If set to false supresses all warnings
44-
* @param {String} name: name to use when reporting
45-
* @param {String|Object} preset - either name of preset or streams obj for bunyan
43+
* @param {Pino|Boolean} [log] sets up logger. If set to false supresses all warnings
44+
* @param {String} [name] name to use when reporting
45+
* @param {Boolean} [debug=false] show additional diagnostic information
4646
* @param {String} lockPrefix - used for creating locks in redis
4747
*/
4848
class DistributedCallbackQueue {
@@ -82,31 +82,30 @@ class DistributedCallbackQueue {
8282
this.logger.info('Initialized...');
8383
}
8484

85+
static isCompatibleLogger(logger) {
86+
for (const level of ['debug', 'info', 'warn', 'error', 'fatal'].values()) {
87+
if (typeof logger[level] !== 'function') {
88+
return false;
89+
}
90+
}
91+
92+
return true;
93+
}
94+
8595
static initLogger(options) {
8696
const { log: logger, debug, name } = options;
8797
const loggerEnabled = typeof logger === 'undefined' ? !!debug : logger;
8898

89-
if (loggerEnabled && logger instanceof bunyan) {
99+
if (loggerEnabled && DistributedCallbackQueue.isCompatibleLogger(logger)) {
90100
return logger;
91101
}
92102

93-
const streams = [{
94-
level: 'trace',
95-
type: 'raw',
96-
stream: new bunyan.RingBuffer({ limit: 100 }),
97-
}];
98-
103+
let level = 'silent';
99104
if (loggerEnabled) {
100-
streams.push({
101-
stream: process.stdout,
102-
level: debug ? 'debug' : 'info',
103-
});
105+
level = debug ? 'debug' : 'info';
104106
}
105107

106-
return bunyan.createLogger({
107-
name: name || pkg.name,
108-
streams,
109-
});
108+
return pino({ name: name || pkg.name, level }, pino.destination(1));
110109
}
111110

112111
/**
@@ -415,7 +414,7 @@ class DistributedCallbackQueue {
415414
await lock.extend();
416415
} catch (error) {
417416
// because a job may take too much time, other listeners must implement timeout/retry strategy
418-
this.logger.warn('failed to release lock and publish results', error);
417+
this.logger.warn({ err: error }, 'failed to release lock and publish results');
419418
return null;
420419
}
421420

0 commit comments

Comments
 (0)