adejs/utils/log.js

108 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-09-23 11:02:29 +02:00
var express = require('express');
var http = require('http');
var log = {};
log.Color = {
DEFAULT: 0,
BLACK : 1,
RED : 2,
GREEN : 3,
YELLOW : 4,
BLUE : 5,
MAGENTA: 6,
CYAN : 7,
ORANGE : 8,
};
log.getColorCode = function(c) {
switch (c) {
case log.Color.DEFAULT: return '\u001b[0m';
case log.Color.BLACK: return '\u001b[30m';
case log.Color.RED: return '\u001b[31m';
case log.Color.GREEN: return '\u001b[32m';
case log.Color.YELLOW: return '\u001b[33m';
case log.Color.BLUE: return '\u001b[34m';
case log.Color.MAGENTA: return '\u001b[35m';
case log.Color.CYAN: return '\u001b[36m';
case log.Color.ORANGE: return '\u001b[38;5;202m';
}
}
log.write = function(elt, color) {
console.log(log.getColorCode(color) + elt + log.getColorCode(log.Color.DEFAULT));
}
log.ready = function(msg) {
log.write('[RDY] ' + new Date() + ' ' + msg, log.Color.GREEN);
}
log.request = function(req, res, time) {
var isStatic = req.url.substr(0, 7) === '/static' || req.url === '/favicon.ico';
let mess = '[REQ] ' + new Date() + ' ';
mess += (req.headers['x-forwarded-for'] || req.connection.remoteAddress);
if (time !== undefined) {
mess += ' in ' + (" " + time).slice(-6) + ' ms';
}
mess += ': ';
if (isStatic && req.url === '/favicon.ico') {
mess += '/static';
}
mess += req.url;
log.write(mess, isStatic ? log.Color.YELLOW : log.Color.CYAN);
}
log.socket = {};
log.socket.connect = function(socket) {
let mess = '[SOK] ' + new Date() + ' ';
mess += (req.headers['x-forwarded-for'] || req.connection.remoteAddress);
mess += ' disconnect';
log.write(mess, log.Color.MAGENTA);
}
log.socket.disconnect = function(socket) {
let mess = '[SOK] ' + new Date() + ' ';
mess += (req.headers['x-forwarded-for'] || req.connection.remoteAddress);
mess += ' connect';
log.write(mess, log.Color.MAGENTA);
}
log.dberror = function(error) {
log.write('[DBE] ' + new Date() + ' ' + error, log.Color.RED);
}
log.prefetcherror = function(error) {
log.write('[PFE] ' + new Date() + ' ' + error, log.Color.RED);
}
log.mailerror = function(error) {
log.write('[MLE] ' + new Date() + ' ' + error, log.Color.RED);
}
log.debug = function(info) {
log.write('[DBG] ' + (info !== undefined ? info : ''), log.Color.YELLOW);
}
log.pugerror = function(error) {
log.write('[PER] ' + new Date() + ' ' + error, log.Color.RED);
}
log.warning = function(message) {
log.write('[WRN] ' + new Date() + ' ' + message, log.Color.ORANGE);
}
module.exports = log;