|
|
@ -1,17 +1,31 @@ |
|
|
|
import config from "config"; |
|
|
|
import {v4 as uuid} from "uuid"; |
|
|
|
import Log from "./models/Log"; |
|
|
|
import {bufferToUUID} from "./Utils"; |
|
|
|
import {bufferToUuid} from "./Utils"; |
|
|
|
import ModelFactory from "./db/ModelFactory"; |
|
|
|
|
|
|
|
export enum LogLevel { |
|
|
|
ERROR, |
|
|
|
WARN, |
|
|
|
INFO, |
|
|
|
DEBUG, |
|
|
|
DEV, |
|
|
|
} |
|
|
|
|
|
|
|
export type LogLevelKeys = keyof typeof LogLevel; |
|
|
|
|
|
|
|
/** |
|
|
|
* TODO: make logger not static |
|
|
|
*/ |
|
|
|
export default class Logger { |
|
|
|
private static logLevel: LogLevelKeys = <LogLevelKeys>config.get<string>('log.level'); |
|
|
|
private static dbLogLevel: LogLevelKeys = <LogLevelKeys>config.get<string>('log.db_level'); |
|
|
|
private static logLevel: LogLevel = LogLevel[<LogLevelKeys>config.get<string>('log.level')]; |
|
|
|
private static dbLogLevel: LogLevel = LogLevel[<LogLevelKeys>config.get<string>('log.db_level')]; |
|
|
|
private static verboseMode: boolean = config.get<boolean>('log.verbose'); |
|
|
|
|
|
|
|
public static verbose() { |
|
|
|
public static verbose(): void { |
|
|
|
this.verboseMode = true; |
|
|
|
this.logLevel = <LogLevelKeys>LogLevel[LogLevel[this.logLevel] + 1] || this.logLevel; |
|
|
|
this.dbLogLevel = <LogLevelKeys>LogLevel[LogLevel[this.dbLogLevel] + 1] || this.dbLogLevel; |
|
|
|
if (LogLevel[this.logLevel + 1]) this.logLevel++; |
|
|
|
if (LogLevel[this.dbLogLevel + 1]) this.dbLogLevel++; |
|
|
|
Logger.info('Verbose mode'); |
|
|
|
} |
|
|
|
|
|
|
@ -19,46 +33,45 @@ export default class Logger { |
|
|
|
return this.verboseMode; |
|
|
|
} |
|
|
|
|
|
|
|
public static silentError(error: Error, ...message: any[]): string { |
|
|
|
return this.log('ERROR', message, error, true) || ''; |
|
|
|
public static silentError(error: Error, ...message: unknown[]): string { |
|
|
|
return this.log(LogLevel.ERROR, message, error, true) || ''; |
|
|
|
} |
|
|
|
|
|
|
|
public static error(error: Error, ...message: any[]): string { |
|
|
|
return this.log('ERROR', message, error) || ''; |
|
|
|
public static error(error: Error, ...message: unknown[]): string { |
|
|
|
return this.log(LogLevel.ERROR, message, error) || ''; |
|
|
|
} |
|
|
|
|
|
|
|
public static warn(...message: any[]) { |
|
|
|
this.log('WARN', message); |
|
|
|
public static warn(...message: unknown[]): void { |
|
|
|
this.log(LogLevel.WARN, message); |
|
|
|
} |
|
|
|
|
|
|
|
public static info(...message: any[]) { |
|
|
|
this.log('INFO', message); |
|
|
|
public static info(...message: unknown[]): void { |
|
|
|
this.log(LogLevel.INFO, message); |
|
|
|
} |
|
|
|
|
|
|
|
public static debug(...message: any[]) { |
|
|
|
this.log('DEBUG', message); |
|
|
|
public static debug(...message: unknown[]): void { |
|
|
|
this.log(LogLevel.DEBUG, message); |
|
|
|
} |
|
|
|
|
|
|
|
public static dev(...message: any[]) { |
|
|
|
this.log('DEV', message); |
|
|
|
public static dev(...message: unknown[]): void { |
|
|
|
this.log(LogLevel.DEV, message); |
|
|
|
} |
|
|
|
|
|
|
|
private static log(level: LogLevelKeys, message: any[], error?: Error, silent: boolean = false): string | null { |
|
|
|
const levelIndex = LogLevel[level]; |
|
|
|
if (levelIndex <= LogLevel[this.logLevel]) { |
|
|
|
private static log(level: LogLevel, message: unknown[], error?: Error, silent: boolean = false): string | null { |
|
|
|
if (level <= this.logLevel) { |
|
|
|
if (error) { |
|
|
|
if (levelIndex > LogLevel.ERROR) this.warn(`Wrong log level ${level} with attached error.`); |
|
|
|
if (level > 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}.`); |
|
|
|
if (level <= 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: string, value: any) => { |
|
|
|
if (value instanceof Object) { |
|
|
|
if (value.type === 'Buffer') { |
|
|
|
return JSON.stringify(v, (key: string, value: string | unknown[] | Record<string, unknown>) => { |
|
|
|
if (!Array.isArray(value) && value instanceof Object) { |
|
|
|
if (value.type === 'Buffer' && typeof value.data === 'string') { |
|
|
|
return `Buffer<${Buffer.from(value.data).toString('hex')}>`; |
|
|
|
} else if (value !== v) { |
|
|
|
return `[object Object]`; |
|
|
@ -72,65 +85,56 @@ export default class Logger { |
|
|
|
} |
|
|
|
}).join(' '); |
|
|
|
|
|
|
|
const shouldSaveToDB = levelIndex <= LogLevel[this.dbLogLevel]; |
|
|
|
const shouldSaveToDB = level <= this.dbLogLevel; |
|
|
|
|
|
|
|
let output = `[${level}] `; |
|
|
|
const pad = output.length; |
|
|
|
|
|
|
|
const logID = Buffer.alloc(16); |
|
|
|
uuid({}, logID); |
|
|
|
let strLogID = bufferToUUID(logID); |
|
|
|
if (shouldSaveToDB) output += `${strLogID} - `; |
|
|
|
const logId = Buffer.alloc(16); |
|
|
|
uuid({}, logId); |
|
|
|
const strLogId = bufferToUuid(logId); |
|
|
|
if (shouldSaveToDB) output += `${strLogId} - `; |
|
|
|
|
|
|
|
output += computedMsg.replace(/\n/g, '\n' + ' '.repeat(pad)); |
|
|
|
|
|
|
|
switch (level) { |
|
|
|
case "ERROR": |
|
|
|
case LogLevel.ERROR: |
|
|
|
if (silent || !error) { |
|
|
|
console.error(output); |
|
|
|
} else { |
|
|
|
console.error(output, error); |
|
|
|
} |
|
|
|
break; |
|
|
|
case "WARN": |
|
|
|
case LogLevel.WARN: |
|
|
|
console.warn(output); |
|
|
|
break; |
|
|
|
case "INFO": |
|
|
|
case LogLevel.INFO: |
|
|
|
console.info(output); |
|
|
|
break; |
|
|
|
case "DEBUG": |
|
|
|
case "DEV": |
|
|
|
case LogLevel.DEBUG: |
|
|
|
case LogLevel.DEV: |
|
|
|
console.debug(output); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
if (shouldSaveToDB) { |
|
|
|
if (shouldSaveToDB && ModelFactory.has(Log)) { |
|
|
|
const log = Log.create({}); |
|
|
|
log.setLevel(level); |
|
|
|
log.message = computedMsg; |
|
|
|
log.setError(error); |
|
|
|
log.setLogID(logID); |
|
|
|
log.setLogId(logId); |
|
|
|
log.save().catch(err => { |
|
|
|
if (!silent && err.message.indexOf('ECONNREFUSED') < 0) { |
|
|
|
console.error({save_err: err, error}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
return strLogID; |
|
|
|
return strLogId; |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
private constructor() { |
|
|
|
// disable constructor
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export enum LogLevel { |
|
|
|
ERROR, |
|
|
|
WARN, |
|
|
|
INFO, |
|
|
|
DEBUG, |
|
|
|
DEV, |
|
|
|
} |
|
|
|
|
|
|
|
export type LogLevelKeys = keyof typeof LogLevel; |