Colorizing logs even more

This commit is contained in:
MrFry 2019-10-07 17:20:39 +02:00
parent 38fc65b572
commit f7ae8bc46a

View file

@ -53,20 +53,24 @@ function Log (s, c, b) {
function LogReq (req, toFile, sc) {
try {
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
let logEntry = ip + DELIM + req.hostname + DELIM + req.headers['user-agent'] + DELIM + req.method + DELIM
let rColor = GetRandomColor(ip)
let logEntry = C(rColor) + ip + C()
let color = 'green'
if (req.url.includes('lred')) {
color = 'red'
}
logEntry += C(color) + DELIM + req.hostname + DELIM + req.headers['user-agent'] + DELIM + req.method + DELIM
logEntry += req.url
if (sc !== undefined && sc === 404) { logEntry += DELIM + sc }
let color = GetColor('green')
if (req.url.includes('lred')) {
color = GetColor('red')
}
if (req.url.toLowerCase().includes('isadding')) { color = GetColor('yellow') }
if (!toFile) {
Log(logEntry, color)
Log(logEntry + C())
} else {
let defLogs = GetDateString() + DELIM + logEntry
@ -79,6 +83,27 @@ function LogReq (req, toFile, sc) {
}
}
const colors = [
'red',
'green',
'yellow',
'blue',
'cyan'
]
function GetRandomColor (ip) {
if (!ip) {
return 'red'
}
let res = ip.split('').reduce((res, x) => {
if (!isNaN(x)) {
return res + parseInt(x)
}
}, 0)
return colors[res % colors.length]
}
function GetColor (c) {
if (c === 'redbg') { return '\x1b[41m%s\x1b[0m' }
if (c === 'bluebg') { return '\x1b[44m%s\x1b[0m' }
@ -88,3 +113,16 @@ function GetColor (c) {
if (c === 'blue') { return '\x1b[34m%s\x1b[0m' }
if (c === 'cyan') { return '\x1b[36m%s\x1b[0m' }
}
function C (c) {
if (c !== undefined) { c = c.toLowerCase() }
if (c === 'redbg') { return '\x1b[41m' }
if (c === 'bluebg') { return '\x1b[44m' }
if (c === 'red') { return '\x1b[31m' }
if (c === 'green') { return '\x1b[32m' }
if (c === 'yellow') { return '\x1b[33m' }
if (c === 'blue') { return '\x1b[34m' }
if (c === 'cyan') { return '\x1b[36m' }
if (c === undefined || c === 'clear' || c === 'c') { return '\x1b[0m' }
}