mrfrys-node-server/utils/logger.js
2019-10-07 17:24:43 +02:00

128 lines
3.7 KiB
JavaScript

/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/question-node-server>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------- */
module.exports = {
GetDateString: GetDateString,
Log: Log,
GetColor: GetColor,
LogReq: LogReq
}
const DELIM = '|'
const utils = require('../utils/utils.js')
const locLogFile = './stats/logs'
const logFile = '/nlogs/nlogs'
const allLogFile = '/nlogs/log'
function GetDateString () {
const 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) {
if (c !== undefined) { console.log(c, GetDateString() + DELIM + s) } else { console.log(GetDateString() + DELIM + s) }
if (b) { utils.Beep() }
utils.AppendToFile(GetDateString() + DELIM + s, logFile)
}
function LogReq (req, toFile, sc) {
try {
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
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 }
if (req.url.toLowerCase().includes('isadding')) { color = GetColor('yellow') }
if (!toFile) {
Log(logEntry + C())
} else {
let defLogs = GetDateString() + DELIM + logEntry
utils.AppendToFile(defLogs, locLogFile)
utils.AppendToFile(defLogs, allLogFile)
}
} catch (e) {
console.log(e)
Log('Error at logging lol', GetColor('redbg'), true)
}
}
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' }
if (c === 'red') { return '\x1b[31m%s\x1b[0m' }
if (c === 'green') { 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' }
}
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' }
}