Logging improvements

This commit is contained in:
mrfry 2021-02-26 10:08:50 +01:00
parent 0336860a9d
commit 21f5ba8af5

View file

@ -111,11 +111,20 @@ function Log(msg: string | object, color?: string): void {
) )
} }
function expandWithSpaces(text, count) {
while (text.length < count) {
text += ' '
}
return text
}
function LogReq(req: Request, toFile?: boolean, statusCode?: string): void { function LogReq(req: Request, toFile?: boolean, statusCode?: string): void {
try { try {
const ip: any = let ip: any =
req.headers['cf-connecting-ip'] || req.connection.remoteAddress req.headers['cf-connecting-ip'] || req.connection.remoteAddress
if (!toFile) {
ip = expandWithSpaces(ip, 35)
}
const nolog = noLogips.some((noLogip) => { const nolog = noLogips.some((noLogip) => {
return ip.includes(noLogip) return ip.includes(noLogip)
}) })
@ -139,20 +148,34 @@ function LogReq(req: Request, toFile?: boolean, statusCode?: string): void {
GetColor('redbg') GetColor('redbg')
) )
} }
if (!toFile) {
hostname = expandWithSpaces(hostname, 7)
}
logEntry += dl + logHashed(hostname) + dl logEntry += dl + logHashed(hostname) + dl
if (toFile) { if (toFile) {
logEntry += req.headers['user-agent'] + dl logEntry += req.headers['user-agent'] + dl
} }
logEntry += req.method + dl let method = req.method
if (!toFile) {
if (req.session && req.session.user) { method = expandWithSpaces(method, 4)
logEntry += C('cyan') + req.session.user.id + C() + dl
} else if (req.session && req.session.isException === true) {
logEntry += C('cyan') + 'EX' + C() + dl
} else {
logEntry += C('red') + 'NOUSER' + C() + dl
} }
logEntry += method + dl
let uid = ''
if (req.session && req.session.user) {
uid = req.session.user.id.toString()
} else if (req.session && req.session.isException === true) {
uid = 'EX'
} else {
uid = 'NOUSR'
}
if (!toFile) {
uid = expandWithSpaces(uid, 5)
}
logEntry += C('cyan') + uid + C() + dl
logEntry += GetRandomColor(req.url.split('?')[0]) + req.url logEntry += GetRandomColor(req.url.split('?')[0]) + req.url
if (statusCode !== undefined) { if (statusCode !== undefined) {