Skip to content

Commit cbdfe00

Browse files
author
Hugo Duroux
committed
Merge pull request #42 from AnyFetch/bunyan
Using bunyan-logger
2 parents 1721c19 + 69fb84a commit cbdfe00

File tree

9 files changed

+57
-49
lines changed

9 files changed

+57
-49
lines changed

lib/handlers/hydrater.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*/
88

99
var restify = require('restify');
10+
11+
var log = require('../index.js').log;
12+
13+
1014
/**
1115
* This handler receives a document to hydrate on a POST request and processes it.
1216
*
@@ -15,7 +19,7 @@ var restify = require('restify');
1519
* @param {Object} server Current server. See implementation in index.js
1620
* @param {Function} next Callback to call once res has been populated.
1721
*/
18-
module.exports = function(req, res, server, logger, errLogger, next) {
22+
module.exports = function(req, res, server, next) {
1923
if(!req.params.callback) {
2024
return next(new restify.MissingParameterError('No specified callback'));
2125
}
@@ -40,10 +44,10 @@ module.exports = function(req, res, server, logger, errLogger, next) {
4044
var job = server.queue.createJob(task, {priority: task.priority});
4145
job.save(function(err) {
4246
if(err) {
43-
errLogger("Error while queuing: " + ((task.file_path) ? task.file_path : task.document.id) + "\nError :" + err.toString());
47+
log.warn(err, "Error while queuing " + (task.file_path) ? task.file_path : task.document.id);
4448
}
4549
else {
46-
logger("Queuing: " + ((task.file_path) ? task.file_path : task.document.id));
50+
log.info(task, "Queuing task");
4751
}
4852
});
4953

lib/handlers/status.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* @param {Object} server Current server. See implementation in index.js
1313
* @param {Function} next Callback to call once res has been populated.
1414
*/
15-
module.exports = function(req, res, server, logger, next) {
15+
module.exports = function(req, res, server, next) {
1616
server.queue.stats(function(err, stats) {
1717
if(err) {
1818
return next(err);

lib/helpers/hydrater.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,14 @@ var rarity = require('rarity');
1414
var util = require('util');
1515

1616
var lib = require('../index.js');
17+
var log = lib.log;
1718
var HydrationError = lib.HydrationError;
1819
var logError = require('../utils').logError;
1920

2021

2122

22-
module.exports = function(hydraterFunction, childs, logger, errLogger) {
23-
if(!errLogger) {
24-
errLogger = logger;
25-
}
23+
module.exports = function(hydraterFunction, childs) {
24+
2625

2726
/**
2827
* Handle a hydration task:
@@ -36,7 +35,7 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
3635
*/
3736
return function(job, done) {
3837
var task = job.data;
39-
logger("Starting task: " + ((task.file_path) ? task.file_path : task.document.id));
38+
log.info(task, "Starting task");
4039

4140
async.waterfall([
4241
function performHydration(cb) {
@@ -66,10 +65,10 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
6665
cb(err, changes);
6766
}
6867
if(stdout !== "") {
69-
logger(stdout);
68+
log.info({std: "out"}, stdout);
7069
}
7170
if(stderr !== "") {
72-
errLogger(stderr);
71+
log.info({std: "err"}, stderr);
7372
}
7473
clearTimeout(timeout);
7574
};
@@ -139,7 +138,7 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
139138
var changes = {};
140139
changes.hydration_errored = true;
141140
changes.hydration_error = "Task took too long.";
142-
errLogger('Killing task: ' + ((task.file_path) ? task.file_path : task.document.id));
141+
log.warn(task, 'Killing task');
143142
child.process.kill('SIGTERM');
144143
setTimeout(function() {
145144
if(child.process.connected) {
@@ -178,11 +177,11 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
178177
function patchDocument(changes, cb) {
179178
// Returning null means we won't complete the hydration, and are waiting for something else.
180179
if(changes === null) {
181-
logger("Skipped task: " + ((task.file_path) ? task.file_path : task.document.id));
180+
log.info(task, "Skipped task");
182181
return cb();
183182
}
184183

185-
logger("End of task: " + ((task.file_path) ? task.file_path : task.document.id));
184+
log.info(task, "End of task");
186185

187186
var apiUrl = url.parse(task.callback, false, true);
188187

@@ -198,11 +197,11 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
198197
var extra = JSON.parse(JSON.stringify(task));
199198
extra.changes = changes;
200199
logError(err, extra);
201-
errLogger("ERR hydrating " + ((task.file_path) ? task.file_path : task.document.id), err.toString());
200+
log.warn(err, "Unable to hydrate");
202201
}
203202

204203
if(res && res.statusCode && res.statusCode !== 204) {
205-
errLogger("ERR hydrating: server refused data! Code:" + res.statusCode);
204+
log.warn({code: res.statusCode}, "Server refused data!");
206205
}
207206

208207
cb(null);
@@ -228,7 +227,7 @@ module.exports = function(hydraterFunction, childs, logger, errLogger) {
228227
var extra = JSON.parse(JSON.stringify(task));
229228
extra.changes = changes;
230229
logError(internalErr, extra);
231-
errLogger("INTERNAL ERR", internalErr);
230+
log.error(internalErr, "Internal error");
232231
}
233232
done(err || internalErr, changes);
234233
});

lib/index.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ var util = require('util');
44
var restify = require('restify');
55
var yaqs = require('yaqs');
66
var Childs = require('./helpers/Childs');
7+
var Logger = require("bunyan");
8+
var restifyBunyanLogger = require('restify-bunyan-logger');
79

810
var utils = require('./utils.js');
11+
12+
13+
// Create bunyan logger
14+
var log = new Logger.createLogger({
15+
name: process.env.APP_NAME || 'hydrater',
16+
});
17+
module.exports.log = log;
18+
19+
920
/**
1021
* Create a new hydration server.
1122
* This server will use `config.hydrater_function` as its main function, to turn a file into metadata.
@@ -15,14 +26,12 @@ var utils = require('./utils.js');
1526
* hydrater_function, the actual function to use for hydration. This function takes as params the path to the file on the disk, the current data about the document and a callback to use after hydration. First param is the path to the file, second param the document (metadata, binary_document_type). Third param is the callback, send as first argument an error if any, then the new document data.
1627
* Optional:
1728
* concurrency, max number of simultaneous calls to your hydrater function (default is 1)
18-
* logger, function to use for logging. Defaults to console.log
29+
* opbeat, opbeat credentials
1930
*/
2031
module.exports.createServer = function(config) {
2132
if(!config.hydrater_function) {
2233
throw new Error("Specify `hydrater_function`");
2334
}
24-
config.logger = config.logger || console.log;
25-
config.errLogger = config.errLogger || console.error;
2635

2736
utils.logError.config = config;
2837

@@ -41,14 +50,19 @@ module.exports.createServer = function(config) {
4150

4251
var childs = new Childs(concurrency, tasksPerProcess);
4352

44-
var hydraterHelper = require('./helpers/hydrater.js')(config.hydrater_function, childs, config.logger, config.errLogger);
53+
var hydraterHelper = require('./helpers/hydrater.js')(config.hydrater_function, childs);
54+
55+
var server = restify.createServer({
56+
log: log
57+
});
58+
59+
server.on('after', restifyBunyanLogger());
4560

46-
var server = restify.createServer();
4761
// Middleware Goes Here
62+
server.use(restify.requestLogger());
4863
server.use(restify.acceptParser(server.acceptable));
4964
server.use(restify.queryParser());
5065
server.use(restify.bodyParser());
51-
server.use(require('./middlewares/logger.js'));
5266

5367
server.yaqsClient = yaqs({
5468
prefix: config.hydraterUrl,
@@ -63,10 +77,10 @@ module.exports.createServer = function(config) {
6377
function sigtermYaqs() {
6478
server.yaqsClient.stopAllQueues(function(err) {
6579
if(err) {
66-
config.errLogger(err);
80+
log.warn(err, "Unable to stop queues");
6781
}
6882
childs.stopAllChilds();
69-
config.logger('YAQS has stopped.');
83+
log.info('YAQS has stopped.');
7084
process.exit(0);
7185
});
7286
}
@@ -75,10 +89,10 @@ module.exports.createServer = function(config) {
7589

7690
// Load routes
7791
server.post('/hydrate', function(req, res, next) {
78-
hydraterEndpoint(req, res, server, config.logger, config.errLogger, next);
92+
hydraterEndpoint(req, res, server, next);
7993
});
8094
server.get('/status', function(req, res, next) {
81-
statusEndpoint(req, res, server, config.logger, next);
95+
statusEndpoint(req, res, server, next);
8296
});
8397

8498
// Expose the server

lib/middlewares/logger.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

lib/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ module.exports.logError = function logError(err, req, extra) {
2727
if(module.exports.logError.config) {
2828
extra.hydrater = module.exports.logError.config.hydraterUrl;
2929
}
30+
if(process.env.APP_NAME) {
31+
extra.hydrater_name = process.env.APP_NAME;
32+
}
3033

3134
if(module.exports.logError.opbeat) {
3235
var meta = {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
],
1919
"dependencies": {
2020
"async": "^0.9.0",
21+
"bunyan": "^1.2.3",
2122
"opbeat": "^1.0.5",
2223
"rarity": "^2.1.1",
2324
"redis": "^0.12.1",
2425
"restify": "^2.8.3",
25-
"restify-logger": "^2.0.1",
26+
"restify-bunyan-logger": "^2.0.4",
2627
"supertest": "^0.14.0",
2728
"yaqs": "^1.0.3"
2829
},

test/cleaning.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ concurrencies.forEach(function(concurrency) {
136136
this.timeout(10000);
137137

138138
var config = {
139-
hydrater_function: path.resolve(__dirname, '../hydraters/erroed-hydrater.js'),
139+
hydrater_function: path.resolve(__dirname, './hydraters/errored-hydrater.js'),
140140
concurrency: concurrency,
141141
logger: function() {},
142142
};

test/setup.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ before(function flushRedis(cb) {
66
var client = redis.createClient();
77
client.flushdb(cb);
88
});
9+
10+
11+
before(function removeLogging() {
12+
// Disable logging while testing
13+
var log = require('../lib/index.js').log;
14+
log.info = log.warn = log.error = function() {};
15+
});

0 commit comments

Comments
 (0)