3d-interface/server/lib/NodeLog.js

86 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-07-03 13:27:05 +02:00
var Log = {};
var Colors = Object.freeze({
DEFAULT: '\033[0m',
BLACK: '\033[30m',
RED: '\033[31m',
GREEN: '\033[32m',
YELLOW: '\033[33m',
BLUE: '\033[34m',
MAGENTA: '\033[35m',
CYAN: '\033[36m',
2015-09-28 11:00:52 +02:00
ORANGE: '\033[38;5;202m',
2015-07-03 13:27:05 +02:00
});
var isDev = require('express')().get('env') === 'development';
var log;
if (isDev) {
log = function(elt, color) {
console.log(color + elt + Colors.DEFAULT);
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
} else {
log = function(elt, color) {
console.log(elt);
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
}
Log.ready = function(msg) {
log('[RDY] ' + new Date() + ' ' + msg, Colors.GREEN);
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
Log.request = function(req, res, time) {
2015-07-03 15:03:26 +02:00
if (req.headers['x-forwarded-for'] !== undefined || isDev) {
2015-07-03 13:44:21 +02:00
log(
'[REQ] ' + new Date() + ' ' +
(req.headers['x-forwarded-for'] || req.connection.remoteAddress) +
(time !== undefined ? (' in ' + (" " + time).slice(-6) + ' ms') : '') +
2015-09-28 11:00:52 +02:00
' : ' + (req.static && req.url !== '/favicon.ico' ? '/static' + req.url : req.url),
req.static ? Colors.YELLOW : Colors.CYAN
2015-07-03 13:44:21 +02:00
);
}
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
Log.socket = {};
Log.socket.connection = function(socket) {
log(
'[SOK] ' + new Date() + ' ' +
2015-07-07 16:25:58 +02:00
(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address) + ' connection',
2015-07-07 09:27:03 +02:00
Colors.MAGENTA
2015-07-03 13:27:05 +02:00
);
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
Log.socket.disconnect = function(socket) {
log(
'[SOK] ' + new Date() + ' ' +
2015-07-07 16:25:58 +02:00
(socket.handshake.headers['x-forwarded-for'] || socket.handshake.address) + ' disconnect',
2015-07-07 09:27:03 +02:00
Colors.MAGENTA
2015-07-03 13:27:05 +02:00
);
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
Log.dberror = function(error) {
log(
'[DBE] ' + new Date() + ' ' + error,
Colors.RED
);
2015-07-06 11:26:19 +02:00
};
2015-07-03 13:27:05 +02:00
2015-07-17 14:53:45 +02:00
Log.mailerror = function(error) {
log(
'[MLE] ' + new Date() + ' ' + error,
Colors.RED
);
2015-07-17 16:28:55 +02:00
};
2015-07-17 14:53:45 +02:00
2015-09-29 09:20:07 +02:00
Log.debug = function(info, force) {
if (isDev || force === true) {
2015-07-07 09:27:03 +02:00
log(
'[DBG] ' + (info !== undefined ? info : ''),
2015-09-28 11:00:52 +02:00
Colors.ORANGE
2015-07-07 09:27:03 +02:00
);
2015-09-29 09:20:07 +02:00
}
};
2015-07-07 09:27:03 +02:00
2015-07-03 13:27:05 +02:00
module.exports = Log;