logging improvements

This commit is contained in:
cubedro 2015-06-03 05:06:09 +03:00
parent 1d5eed21b5
commit 7bd8d450ae
6 changed files with 183 additions and 30 deletions

62
app.js
View File

@ -1,4 +1,6 @@
var _ = require('lodash'); var _ = require('lodash');
var logger = require('./lib/utils/logger');
var chalk = require('chalk');
var askedForHistory = false; var askedForHistory = false;
var askedForHistoryTime = 0; var askedForHistoryTime = 0;
@ -9,7 +11,7 @@ var Primus = require('primus'),
var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret"; var WS_SECRET = process.env.WS_SECRET || "eth-net-stats-has-a-secret";
var Collection = require('./models/collection'); var Collection = require('./lib/collection');
var Nodes = new Collection(); var Nodes = new Collection();
var server; var server;
@ -86,17 +88,17 @@ client.use('emit', require('primus-emit'));
api.on('connection', function (spark) api.on('connection', function (spark)
{ {
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Open:', spark.address.ip); console.info('API', 'CON', 'Open:', spark.address.ip);
spark.on('hello', function (data) spark.on('hello', function (data)
{ {
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Hello', data['id']); console.info('API', 'CON', 'Hello', data['id']);
if( _.isUndefined(data.secret) || data.secret !== WS_SECRET ) if( _.isUndefined(data.secret) || data.secret !== WS_SECRET )
{ {
spark.end(undefined, { reconnect: false }); spark.end(undefined, { reconnect: false });
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Closed - wrong auth', data); console.error('API', 'CON', 'Closed - wrong auth', data);
return false; return false;
} }
@ -110,22 +112,22 @@ api.on('connection', function (spark)
var info = Nodes.add( data ); var info = Nodes.add( data );
spark.emit('ready'); spark.emit('ready');
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connected', data.id); console.success('API', 'CON', 'Connected', data.id);
client.write({ client.write({
action: 'add', action: 'add',
data: info data: info
}); });
var time = (new Date()).toJSON(); var time = chalk.reset.cyan((new Date()).toJSON()) + " ";
console.time(time + ' [COL] [CHR] Got charts in'); console.time(time, 'COL', 'CHR', 'Got charts in');
client.write({ client.write({
action: 'charts', action: 'charts',
data: Nodes.getCharts() data: Nodes.getCharts()
}); });
console.timeEnd(time + ' [COL] [CHR] Got charts in'); console.timeEnd(time, 'COL', 'CHR', 'Got charts in');
} }
}); });
@ -143,23 +145,23 @@ api.on('connection', function (spark)
data: stats data: stats
}); });
console.log((new Date()).toJSON(), '[API]', '[UPD]', 'Update from:', data.id, 'for:', data.stats); console.info('API', 'UPD', 'Update from:', data.id, 'for:', data.stats);
var time = (new Date()).toJSON(); var time = chalk.reset.cyan((new Date()).toJSON()) + " ";
console.time(time + ' [COL] [CHR] Got charts in'); console.time(time, 'COL', 'CHR', 'Got charts in');
client.write({ client.write({
action: 'charts', action: 'charts',
data: Nodes.getCharts() data: Nodes.getCharts()
}); });
console.timeEnd(time + ' [COL] [CHR] Got charts in'); console.timeEnd(time, 'COL', 'CHR', 'Got charts in');
} }
} }
else else
{ {
console.log((new Date()).toJSON(), '[API]', '[UPD]', 'Update error:', data); console.error('API', 'UPD', 'Update error:', data);
} }
}); });
@ -177,22 +179,22 @@ api.on('connection', function (spark)
data: stats data: stats
}); });
console.log((new Date()).toJSON(), '[API]', '[BLK]', 'Block:', data.block['number'], 'from:', data.id); console.success('API', 'BLK', 'Block:', data.block['number'], 'from:', data.id);
var time = (new Date()).toJSON(); var time = chalk.reset.cyan((new Date()).toJSON()) + " ";
console.time(time + ' [COL] [CHR] Got charts in'); console.time(time, 'COL', 'CHR', 'Got charts in');
client.write({ client.write({
action: 'charts', action: 'charts',
data: Nodes.getCharts() data: Nodes.getCharts()
}); });
console.timeEnd(time + ' [COL] [CHR] Got charts in'); console.timeEnd(time, 'COL', 'CHR', 'Got charts in');
} }
} }
else else
{ {
console.log((new Date()).toJSON(), '[API]', '[BLK]', 'Block error:', data); console.error('API', 'BLK', 'Block error:', data);
} }
}); });
@ -211,11 +213,11 @@ api.on('connection', function (spark)
}); });
} }
console.log((new Date()).toJSON(), '[API]', '[TXS]', 'Pending:', data.stats['pending'], 'from:', data.id); console.success('API', 'TXS', 'Pending:', data.stats['pending'], 'from:', data.id);
} }
else else
{ {
console.log((new Date()).toJSON(), '[API]', '[TXS]', 'Pending error:', data); console.error('API', 'TXS', 'Pending error:', data);
} }
}); });
@ -235,28 +237,28 @@ api.on('connection', function (spark)
}); });
} }
console.log((new Date()).toJSON(), '[API]', '[STA]', 'Stats from:', data.id); console.success('API', 'STA', 'Stats from:', data.id);
} }
else else
{ {
console.log((new Date()).toJSON(), '[API]', '[STA]', 'Stats error:', data); console.error('API', 'STA', 'Stats error:', data);
} }
}); });
spark.on('history', function (data) spark.on('history', function (data)
{ {
console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Got from:', data.id); console.success('API', 'HIS', 'Got from:', data.id);
var time = (new Date()).toJSON(); var time = chalk.reset.cyan((new Date()).toJSON()) + " ";
console.time(time + ' [COL] [CHR] Got charts in'); console.time(time, 'COL', 'CHR', 'Got charts in');
client.write({ client.write({
action: 'charts', action: 'charts',
data: Nodes.addHistory(data.id, data.history) data: Nodes.addHistory(data.id, data.history)
}); });
console.timeEnd(time + ' [COL] [CHR] Got charts in'); console.timeEnd(time, 'COL', 'CHR', 'Got charts in');
askedForHistory = false; askedForHistory = false;
@ -272,7 +274,7 @@ api.on('connection', function (spark)
serverTime: _.now() serverTime: _.now()
}); });
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Ping from:', data['id']); console.info('API', 'PIN', 'Ping from:', data['id']);
}); });
@ -290,14 +292,14 @@ api.on('connection', function (spark)
}); });
} }
console.log((new Date()).toJSON(), '[API]', '[PIN]', 'Latency:', latency, 'from:', data.id); console.info('API', 'PIN', 'Latency:', latency, 'from:', data.id);
if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) ) if( Nodes.requiresUpdate(data.id) && (!askedForHistory || _.now() - askedForHistoryTime > 200000) )
{ {
var range = Nodes.getHistory().getHistoryRequestRange(); var range = Nodes.getHistory().getHistoryRequestRange();
spark.emit('history', range); spark.emit('history', range);
console.log((new Date()).toJSON(), '[API]', '[HIS]', 'Asked:', data.id, 'for history:', range.min, '-', range.max); console.info('API', 'HIS', 'Asked:', data.id, 'for history:', range.min, '-', range.max);
askedForHistory = true; askedForHistory = true;
askedForHistoryTime = _.now(); askedForHistoryTime = _.now();
@ -315,7 +317,7 @@ api.on('connection', function (spark)
data: stats data: stats
}); });
console.log((new Date()).toJSON(), '[API]', '[CON]', 'Connection with:', spark.id, 'ended:', data); console.warn('API', 'CON', 'Connection with:', spark.id, 'ended:', data);
}); });
}); });

150
lib/utils/logger.js Normal file
View File

@ -0,0 +1,150 @@
'use strict';
var util = require('util');
var chalk = require('chalk');
var sections = [
'API',
'COL',
'SYS'
];
var types = [
'CON',
'CHR',
'UDP',
'BLK',
'TXS',
'STA',
'HIS',
'PIN'
];
var typeColors = {
'CON': chalk.reset.bold.yellow,
'CHR': chalk.reset.bold.red,
'UDP': chalk.reset.bold.green,
'BLK': chalk.reset.bold.blue,
'TXS': chalk.reset.bold.cyan,
'STA': chalk.reset.bold.red,
'HIS': chalk.reset.bold.magenta,
'PIN': chalk.reset.bold.yellow,
};
[
{
name: "info",
sign: '=i=',
signColor: chalk.blue,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "success",
inherit: 'log',
sign: '=✓=',
signColor: chalk.green,
messageColor: chalk.bold.green,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "warn",
sign: '=!=',
signColor: chalk.yellow,
messageColor: chalk.bold.yellow,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "error",
sign: '=✘=',
signColor: chalk.red,
messageColor: chalk.bold.red,
formatter: function (sign, message)
{
return [sign, message];
}
},
{
name: "time",
sign: '=T=',
signColor: chalk.cyan,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [util.format.apply(util, [sign, message])];
}
},
{
name: "timeEnd",
sign: '=T=',
signColor: chalk.cyan,
messageColor: chalk.bold,
formatter: function (sign, message)
{
return [util.format.apply(util, [sign, message])];
}
}
].forEach( function (item)
{
if(item.inherit !== undefined)
console[item.name] = console[item.inherit];
var fn = console[item.name];
console[item.name] = function ()
{
var args = Array.prototype.slice.call(arguments);
var type,
sign,
time;
var section = 'API';
var message = '';
if (args[0].indexOf(new Date().getFullYear()) >= 0)
{
time = args.splice(0, 1);
}
if (sections.indexOf(args[0]) >= 0)
{
section = args.splice(0, 1);
}
if (types.indexOf(args[0]) >= 0)
{
type = args.splice(0, 1);
}
sign = item.signColor.bold( '[' ) + chalk.reset.bold.white( section ) + item.signColor.bold( ']' ) + " " + item.signColor.bold( '[' ) + typeColors[type](type) + item.signColor.bold( ']' );
if(item.name !== "time" && item.name !== "timeEnd")
{
time = (new Date()).toJSON().replace("T", " ").replace("Z", " ");
}
else
{
time = time.toString().replace("T", " ").replace("Z", "");
}
sign = chalk.reset.magenta(time) + sign;
if (typeof args[0] === 'object')
{
message = util.inspect( args[0], { depth: null, colors: true } );
}
else {
message = item.messageColor( util.format.apply(util, args) );
}
return fn.apply( this, item.formatter(sign, message) );
}
});

View File

@ -12,6 +12,7 @@
}, },
"dependencies": { "dependencies": {
"body-parser": "1.12.4", "body-parser": "1.12.4",
"chalk": "^1.0.0",
"d3": "^3.5.5", "d3": "^3.5.5",
"debug": "2.2.0", "debug": "2.2.0",
"express": "4.12.4", "express": "4.12.4",