ethstats-server/lib/utils/logger.js

165 lines
3.0 KiB
JavaScript
Raw Normal View History

2015-06-03 04:06:09 +02:00
'use strict';
var util = require('util');
var chalk = require('chalk');
var sections = [
'API',
'COL',
'SYS'
];
var types = [
'CON',
'CHR',
2015-06-03 04:22:53 +02:00
'UPD',
2015-06-03 04:06:09 +02:00
'BLK',
'TXS',
'STA',
'HIS',
'PIN'
];
var typeColors = {
'CON': chalk.reset.bold.yellow,
'CHR': chalk.reset.bold.red,
2015-06-03 04:22:53 +02:00
'UPD': chalk.reset.bold.green,
2015-06-03 04:06:09 +02:00
'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,
};
2015-10-28 22:23:17 +01:00
var verbosity = [
[],
['error'],
['error', 'warn', 'success'],
['info', 'error', 'warn', 'success', 'time', 'timeEnd']
];
var ENV_VERBOSITY = process.env.VERBOSITY || 2;
2015-06-03 04:06:09 +02:00
[
{
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 ()
{
2016-01-08 14:41:01 +01:00
if(verbosity[ENV_VERBOSITY].indexOf(item.name) === -1)
2015-10-28 22:23:17 +01:00
return false;
2015-06-03 04:06:09 +02:00
var args = Array.prototype.slice.call(arguments);
2015-06-03 04:19:00 +02:00
var type = null,
2015-06-03 04:06:09 +02:00
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);
}
2015-06-03 04:19:00 +02:00
sign = item.signColor.bold( '[' ) + chalk.reset.bold.white( section ) + item.signColor.bold( ']' );
if(type !== null)
2015-06-03 04:32:18 +02:00
sign = sign + " " + item.signColor.bold( '[' ) + typeColors[type](type) + item.signColor.bold( ']' );
2015-06-03 04:06:09 +02:00
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) );
}
});