From 89d7b1f1465e67ba59cc0311962c04961980e2f8 Mon Sep 17 00:00:00 2001 From: Sanakey <sanakey@qq.com> Date: 星期一, 22 七月 2024 18:10:31 +0800 Subject: [PATCH] feat: 添加logger方法 --- src/utils/http/axios/index.ts | 2 src/utils/logger.ts | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.ts | 4 ++ 3 files changed, 105 insertions(+), 1 deletions(-) diff --git a/src/main.ts b/src/main.ts index 1c4c808..33611f9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,6 +18,10 @@ import App from './App.vue'; +import Logger from '@/utils/logger'; + +Logger.info('start'); + async function bootstrap() { const app = createApp(App); diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index 9c3a86b..8e4a17a 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -232,7 +232,7 @@ // authentication schemes锛宔.g: Bearer // authenticationScheme: 'Bearer', authenticationScheme: '', - timeout: 10 * 1000, + timeout: 60 * 1000, // 鍩虹鎺ュ彛鍦板潃 // baseURL: globSetting.apiUrl, diff --git a/src/utils/logger.ts b/src/utils/logger.ts new file mode 100644 index 0000000..7942b12 --- /dev/null +++ b/src/utils/logger.ts @@ -0,0 +1,100 @@ +锘�// type logger = { +// level: string; // 榛樿涓篋EBUG绾у埆 +// setLevel(newLevel:string): void; +// shouldLog(level: string): boolean; +// formatStack(stack: string): string; +// log(level: string, message: string, error?: Error): void; +// debug(message: string): void; +// info(message: string): void; +// warn(message: string): void; +// error(message: string, error?: Error): void; +// fatal(message: string, error?: Error): void; +// } + +type Level = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL'; + +class Logger { + static level = 'DEBUG'; // 榛樿涓篋EBUG绾у埆 + + static setLevel(newLevel: Level) { + this.level = newLevel; + } + + // 鏍规嵁鏃ュ織绛夌骇鍒ゆ柇鏄惁搴旇鎵撳嵃鏃ュ織 + static shouldLog(level: Level) { + const levels = ['DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']; + return levels.indexOf(level) >= levels.indexOf(this.level); + } + + static formatStack(stack: string | undefined) { + if (!stack) return ''; + // 鏍煎紡鍖栭敊璇爢鏍堢殑閫昏緫 + return stack + .split('\n') + .map((line) => ` at ${line}`) + .join('\n'); + } + + static log(level: Level, message: string, error?: Error) { + if (!this.shouldLog(level)) { + return; + } + const timestamp = new Date().toISOString(); + const stack = error ? error.stack : ''; + let formattedMessage = `[${timestamp}] [${level}] ${message} ${stack}`; + + // 鏍煎紡鍖栭敊璇爢鏍� + if (error) { + formattedMessage += `\n${this.formatStack(error.stack)}`; + } + + switch (level) { + case 'DEBUG': + console.debug(formattedMessage); + break; + case 'INFO': + console.info(formattedMessage); + break; + case 'WARN': + console.warn(formattedMessage); + break; + case 'ERROR': + case 'FATAL': + console.error(formattedMessage); + break; + default: + console.log(formattedMessage); + } + } + + static debug(message: string) { + this.log('DEBUG', message); + } + + static info(message: string) { + this.log('INFO', message); + } + + static warn(message: string) { + this.log('WARN', message); + } + + static error(message: string, error: Error) { + this.log('ERROR', message, error); + } + + static fatal(message: string, error: Error) { + this.log('FATAL', message, error); + } +} + +// 鐢熶骇鐜涓缃棩蹇楃瓑绾� +if (process.env.NODE_ENV === 'production') { + Logger.setLevel('WARN'); +} + +// // 浣跨敤绀轰緥 +// Logger.info('Application is starting...'); +// Logger.error('Failed to load user data', new Error('Network Error')); + +export default Logger; -- Gitblit v1.8.0