this repo has no description
at v1.2.1 2.0 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 = [ 54 `%c[${logLevel}]`, 55 `background-color: ${colors[level]}; color: #FFFFFF;`, 56 `[${this.name}]`, 57 ...obj 58 ]; 59 } else { 60 args = [`[${logLevel}]`, `[${this.name}]`, ...obj]; 61 } 62 63 switch (level) { 64 case LogLevel.SILLY: 65 case LogLevel.TRACE: 66 console.trace(...args); 67 break; 68 69 case LogLevel.DEBUG: 70 console.debug(...args); 71 break; 72 73 case LogLevel.INFO: 74 console.info(...args); 75 break; 76 77 case LogLevel.WARN: 78 console.warn(...args); 79 break; 80 81 case LogLevel.ERROR: 82 console.error(...args); 83 break; 84 } 85 } 86} 87 88export function initLogger(config: Config) { 89 if (config.loggerLevel != null) { 90 const enumValue = 91 LogLevel[config.loggerLevel.toUpperCase() as keyof typeof LogLevel]; 92 if (enumValue != null) { 93 maxLevel = enumValue; 94 } 95 } 96}