From 352009465fcac9c38aa7ef20b0f058c24ce6a7f5 Mon Sep 17 00:00:00 2001 From: devin Date: Wed, 13 Dec 2023 17:31:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(common):=20=E5=AE=8C=E5=96=84log?= =?UTF-8?q?=E5=B0=81=E8=A3=85=EF=BC=8C=E6=94=AF=E6=8C=81=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E4=BB=A3=E7=A0=81=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/opendesign/src/_utils/log.ts | 48 +++++++++++--------------- packages/opendesign/src/flex/OFlex.vue | 4 +-- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/packages/opendesign/src/_utils/log.ts b/packages/opendesign/src/_utils/log.ts index 903fd9e6..b9241770 100644 --- a/packages/opendesign/src/_utils/log.ts +++ b/packages/opendesign/src/_utils/log.ts @@ -1,48 +1,42 @@ +/** + * 封装日志打印 + */ const logFunction = { info: console.info, warn: console.warn, error: console.error, }; -export type LogLevel = keyof typeof logFunction; +type LogLevel = keyof typeof logFunction; -function printLog(level: LogLevel, ...msg: any[]) { - const fn = logFunction[level]; - if (fn) { - fn(...msg); +function getLogFunction(level: LogLevel, prefix?: string) { + if (prefix) { + return console.warn.bind(window.console, prefix); + } else { + return console.warn.bind(window.console); } } -export class Log { - prefix: string; - constructor(namespace?: string) { - this.prefix = ''; - if (namespace) { - this.prefix = `[${namespace}]`; +export class Logger { + private prefix: string = ''; + constructor(prefix?: string) { + if (prefix) { + this.prefix = `[${prefix}]`; } } - info(...msg: any[]) { - if (this.prefix) { - msg.unshift(this.prefix); - } - printLog('info', ...msg); + get info() { + return getLogFunction('info', this.prefix); } - warn(...msg: any[]) { - if (this.prefix) { - msg.unshift(this.prefix); - } - printLog('warn', ...msg); + get warn() { + return getLogFunction('warn', this.prefix); } - error(...msg: any[]) { - if (this.prefix) { - msg.unshift(this.prefix); - } - printLog('error', ...msg); + get error() { + return getLogFunction('error', this.prefix); } } -const logger = new Log(); +const logger = new Logger(); export default logger; diff --git a/packages/opendesign/src/flex/OFlex.vue b/packages/opendesign/src/flex/OFlex.vue index 276ba6b6..78b6ebf7 100644 --- a/packages/opendesign/src/flex/OFlex.vue +++ b/packages/opendesign/src/flex/OFlex.vue @@ -1,5 +1,5 @@