mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
logger.js refactoring
This commit is contained in:
parent
8a92e5651f
commit
9cacee321c
1 changed files with 59 additions and 73 deletions
104
utils/logger.js
104
utils/logger.js
|
@ -23,82 +23,68 @@ module.exports = {
|
||||||
Log: Log,
|
Log: Log,
|
||||||
GetColor: GetColor,
|
GetColor: GetColor,
|
||||||
LogReq: LogReq
|
LogReq: LogReq
|
||||||
};
|
|
||||||
|
|
||||||
const DELIM = "|";
|
|
||||||
|
|
||||||
var utils = require('./utils.js');
|
|
||||||
const nlogFile = "stats/nlogs";
|
|
||||||
const logFile = "/nlogs/nlogs";
|
|
||||||
const locLogFile = "stats/logs";
|
|
||||||
const allLogFile = "/nlogs/log";
|
|
||||||
|
|
||||||
function GetDateString() {
|
|
||||||
var m = new Date();
|
|
||||||
return m.getFullYear() + "/" +
|
|
||||||
("0" + (m.getMonth() + 1)).slice(-2) + "/" +
|
|
||||||
("0" + m.getDate()).slice(-2) + " " +
|
|
||||||
("0" + m.getHours()).slice(-2) + ":" +
|
|
||||||
("0" + m.getMinutes()).slice(-2) + ":" +
|
|
||||||
("0" + m.getSeconds()).slice(-2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Log(s, c, b) {
|
const DELIM = '|'
|
||||||
if (c != undefined)
|
|
||||||
console.log(c, GetDateString() + DELIM + s);
|
|
||||||
else
|
|
||||||
console.log(GetDateString() + DELIM + s);
|
|
||||||
|
|
||||||
if (b)
|
var utils = require('../utils/utils.js')
|
||||||
utils.Beep();
|
const nlogFile = '../stats/nlogs'
|
||||||
|
const locLogFile = '../stats/logs'
|
||||||
|
const logFile = '/nlogs/nlogs'
|
||||||
|
const allLogFile = '/nlogs/log'
|
||||||
|
|
||||||
utils.AppendToFile(GetDateString() + DELIM + s, nlogFile);
|
function GetDateString () {
|
||||||
utils.AppendToFile(GetDateString() + DELIM + s, logFile);
|
var m = new Date()
|
||||||
|
return m.getFullYear() + '/' +
|
||||||
|
('0' + (m.getMonth() + 1)).slice(-2) + '/' +
|
||||||
|
('0' + m.getDate()).slice(-2) + ' ' +
|
||||||
|
('0' + m.getHours()).slice(-2) + ':' +
|
||||||
|
('0' + m.getMinutes()).slice(-2) + ':' +
|
||||||
|
('0' + m.getSeconds()).slice(-2)
|
||||||
}
|
}
|
||||||
|
|
||||||
function LogReq(req, toFile, sc) {
|
function Log (s, c, b) {
|
||||||
|
if (c != undefined) { console.log(c, GetDateString() + DELIM + s) } else { console.log(GetDateString() + DELIM + s) }
|
||||||
|
|
||||||
|
if (b) { utils.Beep() }
|
||||||
|
|
||||||
|
utils.AppendToFile(GetDateString() + DELIM + s, nlogFile)
|
||||||
|
utils.AppendToFile(GetDateString() + DELIM + s, logFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
function LogReq (req, toFile, sc) {
|
||||||
try {
|
try {
|
||||||
var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress;
|
var ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
|
||||||
var logEntry = ip + DELIM + req.hostname + DELIM + req.headers['user-agent'] +
|
var logEntry = ip + DELIM + req.hostname + DELIM + req.headers['user-agent'] +
|
||||||
DELIM + req.method + DELIM;
|
DELIM + req.method + DELIM
|
||||||
|
|
||||||
logEntry += req.url;
|
logEntry += req.url
|
||||||
|
|
||||||
if (sc != undefined && sc == 404)
|
if (sc != undefined && sc == 404) { logEntry += DELIM + sc }
|
||||||
logEntry += DELIM + sc;
|
var color = GetColor('green')
|
||||||
var color = GetColor("green");
|
|
||||||
|
|
||||||
if (req.url.toLowerCase().includes("isadding"))
|
if (req.url.toLowerCase().includes('isadding')) { color = GetColor('yellow') }
|
||||||
color = GetColor("yellow");
|
|
||||||
if (!toFile) {
|
if (!toFile) {
|
||||||
Log(logEntry, color);
|
Log(logEntry, color)
|
||||||
} else {
|
} else {
|
||||||
var defLogs = GetDateString() + DELIM + logEntry;
|
var defLogs = GetDateString() + DELIM + logEntry
|
||||||
var extraLogs = "\n\t" + JSON.stringify(req.headers) + "\n\t" + JSON.stringify(req.body) + "\n";
|
var extraLogs = '\n\t' + JSON.stringify(req.headers) + '\n\t' + JSON.stringify(req.body) + '\n'
|
||||||
|
|
||||||
utils.AppendToFile(defLogs, locLogFile);
|
utils.AppendToFile(defLogs, locLogFile)
|
||||||
utils.AppendToFile(defLogs, allLogFile);
|
utils.AppendToFile(defLogs, allLogFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e)
|
||||||
Log("Error at logging lol", GetColor("redbg"), true);
|
Log('Error at logging lol', GetColor('redbg'), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function GetColor(c) {
|
function GetColor (c) {
|
||||||
if (c == "redbg")
|
if (c == 'redbg') { return '\x1b[41m%s\x1b[0m' }
|
||||||
return '\x1b[41m%s\x1b[0m';
|
if (c == 'bluebg') { return '\x1b[44m%s\x1b[0m' }
|
||||||
if (c == "bluebg")
|
if (c == 'red') { return '\x1b[31m%s\x1b[0m' }
|
||||||
return '\x1b[44m%s\x1b[0m';
|
if (c == 'green') { return '\x1b[32m%s\x1b[0m' }
|
||||||
if (c == "red")
|
if (c == 'yellow') { return '\x1b[33m%s\x1b[0m' }
|
||||||
return '\x1b[31m%s\x1b[0m';
|
if (c == 'blue') { return '\x1b[34m%s\x1b[0m' }
|
||||||
if (c == "green")
|
if (c == 'cyan') { return '\x1b[36m%s\x1b[0m' }
|
||||||
return '\x1b[32m%s\x1b[0m';
|
|
||||||
if (c == "yellow")
|
|
||||||
return '\x1b[33m%s\x1b[0m';
|
|
||||||
if (c == "blue")
|
|
||||||
return '\x1b[34m%s\x1b[0m';
|
|
||||||
if (c == "cyan")
|
|
||||||
return '\x1b[36m%s\x1b[0m';
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue