this repo has no description
at v1.3.9 1.9 kB view raw
1/* eslint-disable no-console */ 2import { LogLevel } from "@moonlight-mod/types/logger"; 3import { Config } from "@moonlight-mod/types"; 4 5const colors = { 6 [LogLevel.SILLY]: "#EDD3E9", 7 [LogLevel.TRACE]: "#000000", 8 [LogLevel.DEBUG]: "#555555", 9 [LogLevel.INFO]: "#8686d9", 10 [LogLevel.WARN]: "#5454d1", 11 [LogLevel.ERROR]: "#FF0000" 12}; 13 14let maxLevel = LogLevel.INFO; 15 16export default class Logger { 17 private name: string; 18 19 constructor(name: string) { 20 this.name = name; 21 } 22 23 silly(...args: any[]) { 24 this.log(LogLevel.SILLY, args); 25 } 26 27 trace(...args: any[]) { 28 this.log(LogLevel.TRACE, args); 29 } 30 31 debug(...args: any[]) { 32 this.log(LogLevel.DEBUG, args); 33 } 34 35 info(...args: any[]) { 36 this.log(LogLevel.INFO, args); 37 } 38 39 warn(...args: any[]) { 40 this.log(LogLevel.WARN, args); 41 } 42 43 error(...args: any[]) { 44 this.log(LogLevel.ERROR, args); 45 } 46 47 log(level: LogLevel, obj: any[]) { 48 let args = []; 49 const logLevel = LogLevel[level].toUpperCase(); 50 if (maxLevel > level) return; 51 52 if (MOONLIGHT_WEB_PRELOAD || MOONLIGHT_BROWSER) { 53 args = [`%c[${logLevel}]`, `background-color: ${colors[level]}; color: #FFFFFF;`, `[${this.name}]`, ...obj]; 54 } else { 55 args = [`[${logLevel}]`, `[${this.name}]`, ...obj]; 56 } 57 58 switch (level) { 59 case LogLevel.SILLY: 60 case LogLevel.TRACE: 61 console.trace(...args); 62 break; 63 64 case LogLevel.DEBUG: 65 console.debug(...args); 66 break; 67 68 case LogLevel.INFO: 69 console.info(...args); 70 break; 71 72 case LogLevel.WARN: 73 console.warn(...args); 74 break; 75 76 case LogLevel.ERROR: 77 console.error(...args); 78 break; 79 } 80 } 81} 82 83export function initLogger(config: Config) { 84 if (config.loggerLevel != null) { 85 const enumValue = LogLevel[config.loggerLevel.toUpperCase() as keyof typeof LogLevel]; 86 if (enumValue != null) { 87 maxLevel = enumValue; 88 } 89 } 90}