|
| 1 | +type LogType = 'warning' | 'info' | 'error'; |
| 2 | + |
| 3 | +interface LoggerOptions { |
| 4 | + warningTitleBgColor?: string; |
| 5 | + warningKeyColor?: string; |
| 6 | + infoTitleBgColor?: string; |
| 7 | + infoKeyColor?: string; |
| 8 | + errorTitleBgColor?: string; |
| 9 | + errorKeyColor?: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface LogStyles { |
| 13 | + title: string; |
| 14 | + text: string; |
| 15 | + key: string; |
| 16 | + end: string; |
| 17 | +} |
| 18 | + |
| 19 | +export class Logger { |
| 20 | + private productName: string; |
| 21 | + private styles: Record<LogType, LogStyles>; |
| 22 | + |
| 23 | + constructor(productName: string, options: LoggerOptions = {}) { |
| 24 | + this.productName = productName; |
| 25 | + |
| 26 | + this.styles = { |
| 27 | + warning: { |
| 28 | + title: `background-color: ${ |
| 29 | + options.warningTitleBgColor || 'orange' |
| 30 | + }; color: white; font-weight: bold; padding: 2px 4px; border-radius: 4px 0 0 4px;`, |
| 31 | + text: 'background-color: black; color: white; font-weight: bold; padding: 2px;', |
| 32 | + key: `background-color: black; color: ${options.warningKeyColor || 'yellow'}; font-weight: bold; padding: 2px;`, |
| 33 | + end: 'background-color: black; color: white; font-weight: bold; padding: 2px; border-radius: 0 4px 4px 0;', |
| 34 | + }, |
| 35 | + info: { |
| 36 | + title: `background-color: ${ |
| 37 | + options.infoTitleBgColor || 'green' |
| 38 | + }; color: white; font-weight: bold; padding: 2px 4px; border-radius: 4px 0 0 4px;`, |
| 39 | + text: 'background-color: black; color: white; padding: 2px;', |
| 40 | + key: `background-color: black; color: ${options.infoKeyColor || 'lightgreen'}; padding: 2px;`, |
| 41 | + end: 'background-color: black; color: white; padding: 2px; border-radius: 0 4px 4px 0;', |
| 42 | + }, |
| 43 | + error: { |
| 44 | + title: `background-color: ${ |
| 45 | + options.errorTitleBgColor || 'purple' |
| 46 | + }; color: white; font-weight: bold; padding: 2px 4px; border-radius: 4px 0 0 4px;`, |
| 47 | + text: 'background-color: black; color: white; padding: 2px;', |
| 48 | + key: `background-color: black; color: ${options.errorKeyColor || 'red'}; font-weight: bold; padding: 2px;`, |
| 49 | + end: 'background-color: black; color: white; padding: 2px; border-radius: 0 4px 4px 0;', |
| 50 | + }, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + private log(type: LogType, message: string): void { |
| 55 | + if (!this.styles[type]) { |
| 56 | + console.error(`Logger: Unknown log type "${type}".`); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + const style = this.styles[type]; |
| 61 | + |
| 62 | + // 查找所有使用模板语法的占位符,并分隔出普通文本和关键字 |
| 63 | + const messageParts = message.split(/(\{{[^}]+}})/).filter(Boolean); |
| 64 | + const formattedMessage: string[] = []; |
| 65 | + const formattedStyles: string[] = []; |
| 66 | + |
| 67 | + messageParts.forEach((part) => { |
| 68 | + if (part.startsWith('{{') && part.endsWith('}}')) { |
| 69 | + // 如果是关键字占位符,应用关键字样式 |
| 70 | + formattedMessage.push(`%c${part.slice(2, -2)}`); |
| 71 | + formattedStyles.push(style.key); |
| 72 | + } else { |
| 73 | + // 否则,应用普通文本样式 |
| 74 | + formattedMessage.push(`%c${part}`); |
| 75 | + formattedStyles.push(style.text); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + console.log( |
| 80 | + `%c${this.productName} - ${type.toUpperCase()}%c ${formattedMessage.join('')}`, |
| 81 | + style.title, |
| 82 | + style.text, |
| 83 | + ...formattedStyles |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + public warning(message: string): void { |
| 88 | + this.log('warning', message); |
| 89 | + } |
| 90 | + |
| 91 | + public info(message: string): void { |
| 92 | + this.log('info', message); |
| 93 | + } |
| 94 | + |
| 95 | + public error(message: string): void { |
| 96 | + this.log('error', message); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export default new Logger('Pictode'); |
0 commit comments