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