swaf/dist/Logger.js

111 lines
14 KiB
JavaScript
Raw Normal View History

2020-04-22 15:52:17 +02:00
import config from "config";
import { v4 as uuid } from "uuid";
import Log from "./models/Log";
const LOG_LEVEL = config.get('log_level');
const DB_LOG_LEVEL = config.get('db_log_level');
export default class Logger {
static silentError(error, ...message) {
return this.log('ERROR', message, error, true) || '';
}
static error(error, ...message) {
return this.log('ERROR', message, error) || '';
}
static warn(...message) {
this.log('WARN', message);
}
static info(...message) {
this.log('INFO', message);
}
static debug(...message) {
this.log('DEBUG', message);
}
static dev(...message) {
this.log('DEV', message);
}
static log(level, message, error, silent = false) {
const levelIndex = LogLevel[level];
if (levelIndex <= LogLevel[LOG_LEVEL]) {
if (error) {
if (levelIndex > LogLevel.ERROR)
this.warn(`Wrong log level ${level} with attached error.`);
}
else {
if (levelIndex <= LogLevel.ERROR)
this.warn(`No error attached with log level ${level}.`);
}
const computedMsg = message.map(v => {
if (typeof v === 'string') {
return v;
}
else {
return JSON.stringify(v, (key, value) => {
if (value instanceof Object) {
if (value.type === 'Buffer') {
return `Buffer<${Buffer.from(value.data).toString('hex')}>`;
}
else if (value !== v) {
return `[object Object]`;
}
}
if (typeof value === 'string' && value.length > 96) {
return value.substr(0, 96) + '...';
}
return value;
}, 4);
}
}).join(' ');
const log = new Log({});
log.setLevel(level);
log.message = computedMsg;
log.setError(error);
let logID = Buffer.alloc(16);
uuid({}, logID);
log.setLogID(logID);
let output = `[${level}] `;
let pad = output.length;
if (levelIndex <= LogLevel[DB_LOG_LEVEL])
output += `${log.getLogID()} - `;
output += computedMsg.replace(/\n/g, '\n' + ' '.repeat(pad));
switch (level) {
case "ERROR":
if (silent || !error) {
console.error(output);
}
else {
console.error(output, error);
}
break;
case "WARN":
console.warn(output);
break;
case "INFO":
console.info(output);
break;
case "DEBUG":
case "DEV":
console.debug(output);
break;
}
if (levelIndex <= LogLevel[DB_LOG_LEVEL]) {
log.save().catch(err => {
if (!silent && err.message.indexOf('ECONNREFUSED') < 0) {
console.error({ save_err: err, error });
}
});
}
return log.getLogID();
}
return null;
}
constructor() {
}
}
export var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["ERROR"] = 0] = "ERROR";
LogLevel[LogLevel["WARN"] = 1] = "WARN";
LogLevel[LogLevel["INFO"] = 2] = "INFO";
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
LogLevel[LogLevel["DEV"] = 4] = "DEV";
})(LogLevel || (LogLevel = {}));
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiTG9nZ2VyLmpzIiwic291cmNlUm9vdCI6Ii4vIiwic291cmNlcyI6WyJMb2dnZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxNQUFNLE1BQU0sUUFBUSxDQUFDO0FBQzVCLE9BQU8sRUFBQyxFQUFFLElBQUksSUFBSSxFQUFDLE1BQU0sTUFBTSxDQUFDO0FBQ2hDLE9BQU8sR0FBRyxNQUFNLGNBQWMsQ0FBQztBQUUvQixNQUFNLFNBQVMsR0FBK0IsTUFBTSxDQUFDLEdBQUcsQ0FBUyxXQUFXLENBQUMsQ0FBQztBQUM5RSxNQUFNLFlBQVksR0FBK0IsTUFBTSxDQUFDLEdBQUcsQ0FBUyxjQUFjLENBQUMsQ0FBQztBQUVwRixNQUFNLENBQUMsT0FBTyxPQUFPLE1BQU07SUFDaEIsTUFBTSxDQUFDLFdBQVcsQ0FBQyxLQUFZLEVBQUUsR0FBRyxPQUFjO1FBQ3JELE9BQU8sSUFBSSxDQUFDLEdBQUcsQ0FBQyxPQUFPLEVBQUUsT0FBTyxFQUFFLEtBQUssRUFBRSxJQUFJLENBQUMsSUFBSSxFQUFFLENBQUM7SUFDekQsQ0FBQztJQUVNLE1BQU0sQ0FBQyxLQUFLLENBQUMsS0FBWSxFQUFFLEdBQUcsT0FBYztRQUMvQyxPQUFPLElBQUksQ0FBQyxHQUFHLENBQUMsT0FBTyxFQUFFLE9BQU8sRUFBRSxL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