404 logging change

dont write to db on every 404
dont log 404 if route is empty
v1.18.x
Barış Soner Uşaklı 8 years ago
parent 1d4923a606
commit 029e2ea8da

@ -1,7 +1,9 @@
'use strict';
var async = require('async');
var winston = require('winston');
var validator = require('validator');
var cronJob = require('cron').CronJob;
var db = require('../database');
var analytics = require('../analytics');
@ -9,11 +11,39 @@ var analytics = require('../analytics');
module.exports = function (Meta) {
Meta.errors = {};
var counters = {};
new cronJob('0 * * * * *', function () {
Meta.errors.writeData();
}, null, true);
Meta.errors.writeData = function () {
var dbQueue = [];
if (Object.keys(counters).length > 0) {
for (var key in counters) {
if (counters.hasOwnProperty(key)) {
dbQueue.push(async.apply(db.sortedSetIncrBy, 'errors:404', counters[key], key));
}
}
counters = {};
async.series(dbQueue, function (err) {
if (err) {
winston.error(err);
}
});
}
};
Meta.errors.log404 = function (route, callback) {
callback = callback || function () {};
if (!route) {
return setImmediate(callback);
}
route = route.replace(/\/$/, ''); // remove trailing slashes
analytics.increment('errors:404');
db.sortedSetIncrBy('errors:404', 1, route, callback);
counters[route] = counters[route] || 0;
counters[route] += 1;
setImmediate(callback);
};
Meta.errors.get = function (escape, callback) {

Loading…
Cancel
Save