mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Colorizing logs even more
This commit is contained in:
parent
38fc65b572
commit
f7ae8bc46a
1 changed files with 45 additions and 7 deletions
|
@ -53,20 +53,24 @@ function Log (s, c, b) {
|
||||||
function LogReq (req, toFile, sc) {
|
function LogReq (req, toFile, sc) {
|
||||||
try {
|
try {
|
||||||
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
|
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
|
logEntry += req.url
|
||||||
|
|
||||||
if (sc !== undefined && sc === 404) { logEntry += DELIM + sc }
|
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 (req.url.toLowerCase().includes('isadding')) { color = GetColor('yellow') }
|
||||||
if (!toFile) {
|
if (!toFile) {
|
||||||
Log(logEntry, color)
|
Log(logEntry + C())
|
||||||
} else {
|
} else {
|
||||||
let defLogs = GetDateString() + DELIM + logEntry
|
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) {
|
function GetColor (c) {
|
||||||
if (c === 'redbg') { return '\x1b[41m%s\x1b[0m' }
|
if (c === 'redbg') { return '\x1b[41m%s\x1b[0m' }
|
||||||
if (c === 'bluebg') { return '\x1b[44m%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 === 'blue') { return '\x1b[34m%s\x1b[0m' }
|
||||||
if (c === 'cyan') { return '\x1b[36m%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' }
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue