From 4980480fddfaddd8cba57cec0e0a29c93db87d49 Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 14 Sep 2023 18:33:43 +0800 Subject: [PATCH] chore(prod-server): remove duplicated logger module (#4650) --- .changeset/rotten-bees-wash.md | 8 + .../server/prod-server/src/libs/logger.ts | 150 ------------------ .../server/prod-server/src/workerServer.ts | 2 +- packages/toolkit/utils/src/cli/logger.ts | 2 +- 4 files changed, 10 insertions(+), 152 deletions(-) create mode 100644 .changeset/rotten-bees-wash.md delete mode 100644 packages/server/prod-server/src/libs/logger.ts diff --git a/.changeset/rotten-bees-wash.md b/.changeset/rotten-bees-wash.md new file mode 100644 index 000000000000..7411cd277a75 --- /dev/null +++ b/.changeset/rotten-bees-wash.md @@ -0,0 +1,8 @@ +--- +'@modern-js/prod-server': patch +'@modern-js/utils': patch +--- + +chore(prod-server): remove duplicated logger module + +chore(prod-server): 移除重复的 logger 模块 diff --git a/packages/server/prod-server/src/libs/logger.ts b/packages/server/prod-server/src/libs/logger.ts deleted file mode 100644 index 7237b9b40bf0..000000000000 --- a/packages/server/prod-server/src/libs/logger.ts +++ /dev/null @@ -1,150 +0,0 @@ -type LogLevel = 'debug' | 'info' | 'warn' | 'error'; - -type LogMsg = number | string | Error | null; - -interface LoggerConfiguration { - label?: string; - level?: LogLevel; -} - -interface InstanceConfiguration { - displayLabel?: boolean; - uppercaseLabel?: boolean; -} - -interface ConstructorOptions { - config?: InstanceConfiguration; - level?: string; - types?: Record; -} - -type LoggerFunction = (message?: LogMsg, ...args: any[]) => void; - -const LOG_LEVEL: Record = { - error: 0, - warn: 1, - info: 2, - debug: 3, - log: 4, -}; - -const LOG_TYPES = { - error: { - color: 'red', - label: 'error', - level: 'error', - }, - info: { - color: 'cyan', - label: 'info', - level: 'info', - }, - success: { - color: 'green', - label: 'Success', - level: 'info', - }, - warn: { - color: 'yellow', - label: 'warn', - level: 'warn', - }, - debug: { - color: 'red', - label: 'debug', - level: 'debug', - }, - log: { level: 'log' }, -}; - -const DEFAULT_CONFIG = { - displayLabel: true, - uppercaseLabel: false, -}; - -class Logger { - private readonly level: string; - - private readonly config: InstanceConfiguration; - - private readonly types: Record; - - private readonly longestLabel: string; - - [key: string]: any; - - constructor(options: ConstructorOptions = {}) { - this.level = options.level || LOG_TYPES.log.level; - this.config = { ...DEFAULT_CONFIG, ...(options.config || {}) }; - this.types = { - ...(LOG_TYPES as Record), - ...(options.types || {}), - }; - this.longestLabel = this.getLongestLabel(); - - Object.keys(this.types).forEach(type => { - this[type] = this._log.bind(this, type); - }); - } - - private _log(type: string, message?: LogMsg, ...args: string[]) { - if (message === undefined || message === null) { - console.log(); - return; - } - - if (LOG_LEVEL[type] > LOG_LEVEL[this.level]) { - return; - } - - let label = ''; - let text = ''; - const logType = this.types[type]; - - if (this.config.displayLabel && logType.label) { - label = this.config.uppercaseLabel - ? logType.label.toUpperCase() - : logType.label; - label = label.padEnd(this.longestLabel.length); - } - - if (message instanceof Error) { - if (message.stack) { - const [name, ...rest] = message.stack.split('\n'); - text = `${name}\n${rest.join('\n')}`; - } else { - text = message.message; - } - } else { - text = `${message}`; - } - - const log = label.length > 0 ? `${label} ${text}` : text; - - console.log(log, ...args); - } - - private getLongestLabel() { - let longestLabel = ''; - Object.keys(this.types).forEach(type => { - const { label = '' } = this.types[type]; - if (label.length > longestLabel.length) { - longestLabel = label; - } - }); - return longestLabel; - } -} - -type LoggerInterface = { - [key in keyof typeof LOG_TYPES]: LoggerFunction; -}; - -const logger = new Logger() as Logger & LoggerInterface; - -logger.Logger = Logger; - -export { Logger }; - -export { logger }; -export type { LoggerInterface }; diff --git a/packages/server/prod-server/src/workerServer.ts b/packages/server/prod-server/src/workerServer.ts index 8ae818ad67e5..6b85cee9a3b3 100644 --- a/packages/server/prod-server/src/workerServer.ts +++ b/packages/server/prod-server/src/workerServer.ts @@ -7,13 +7,13 @@ import { Reporter, } from '@modern-js/types'; import { createAsyncPipeline } from '@modern-js/plugin'; +import { Logger, LoggerInterface } from '@modern-js/utils/logger'; import { WorkerServerContext, createAfterMatchContext, createAfterRenderContext, createMiddlewareContext, } from './libs/hook-api/index.worker'; -import { Logger, LoggerInterface } from './libs/logger'; import { ModernRouteInterface, RouteMatchManager } from './libs/route'; import { metrics as defaultMetrics } from './libs/metrics'; import { defaultReporter } from './libs/reporter'; diff --git a/packages/toolkit/utils/src/cli/logger.ts b/packages/toolkit/utils/src/cli/logger.ts index 858f135f92a0..ebf277ffbef2 100644 --- a/packages/toolkit/utils/src/cli/logger.ts +++ b/packages/toolkit/utils/src/cli/logger.ts @@ -44,7 +44,7 @@ const LOG_TYPES = { }, success: { color: 'green', - label: 'Success', + label: 'success', level: 'info', }, warn: {