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