From 4372adc703b9a4c785664054ab2a533626d815a8 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Mon, 1 Sep 2025 18:10:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=B5=81=E6=B0=B4=E7=BA=BF=E6=89=A7=E8=A1=8C=E6=97=B6=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=98=BE=E7=A4=BA=E9=94=99=E4=B9=B1=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/basic/src/utils/util.log.ts | 124 +++++++++++------- .../views/certd/pipeline/pipeline/index.vue | 2 +- 2 files changed, 78 insertions(+), 48 deletions(-) diff --git a/packages/core/basic/src/utils/util.log.ts b/packages/core/basic/src/utils/util.log.ts index 531f2e0f..ce93a481 100644 --- a/packages/core/basic/src/utils/util.log.ts +++ b/packages/core/basic/src/utils/util.log.ts @@ -1,22 +1,4 @@ -import log4js, { LoggingEvent, Logger, Level, CallStack } from "log4js"; - -const OutputAppender = { - configure: (config: any, layouts: any, findAppender: any, levels: any) => { - let layout = layouts.basicLayout; - if (config.layout) { - layout = layouts.layout(config.layout.type, config.layout); - } - function customAppender(layout: any, timezoneOffset: any) { - return (loggingEvent: LoggingEvent) => { - if (loggingEvent.context.outputHandler?.write) { - const text = `${layout(loggingEvent, timezoneOffset)}\n`; - loggingEvent.context.outputHandler.write(text); - } - }; - } - return customAppender(layout, config.timezoneOffset); - }, -}; +import log4js, { CallStack, Level, Logger } from "log4js"; let logFilePath = "./logs/app.log"; export function resetLogConfigure() { @@ -24,7 +6,6 @@ export function resetLogConfigure() { log4js.configure({ appenders: { std: { type: "stdout" }, - output: { type: OutputAppender }, file: { type: "dateFile", filename: logFilePath, @@ -33,7 +14,7 @@ export function resetLogConfigure() { numBackups: 3, }, }, - categories: { default: { appenders: ["std", "file"], level: "info" }, pipeline: { appenders: ["std", "file", "output"], level: "info" } }, + categories: { default: { appenders: ["std", "file"], level: "info" }, pipeline: { appenders: ["std", "file"], level: "info" } }, }); } resetLogConfigure(); @@ -44,15 +25,49 @@ export function resetLogFilePath(filePath: string) { resetLogConfigure(); } export function buildLogger(write: (text: string) => void) { - const logger = log4js.getLogger("pipeline"); - const _secrets: string[] = []; - //@ts-ignore - logger.addSecret = (secret: string) => { - _secrets.push(secret); - }; - logger.addContext("outputHandler", { - write: (text: string) => { - for (const item of _secrets) { + return new PipelineLogger("pipeline", write); +} + +export type ILogger = Logger; + +const locale = Intl.DateTimeFormat().resolvedOptions().locale; +const formatter = new Intl.DateTimeFormat(locale, { + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + hour12: false, +}); +function formatDateIntl(date = new Date()) { + const milliseconds = date.getMilliseconds(); // 获取毫秒 + const formattedMilliseconds = milliseconds.toString().padStart(3, "0"); + return formatter.format(date) + "." + formattedMilliseconds; +} + +// @ts-ignore +export class PipelineLogger implements ILogger { + callStackLinesToSkip: number = 3; + readonly category: string = "pipeline"; + level: Level | string = "info"; + _secrets: string[] = []; + logger: ILogger; + customWriter!: (text: string) => void; + + constructor(name: string, write: (text: string) => void) { + this.customWriter = write; + this.logger = log4js.getLogger(name); + } + + addSecret(secret: string) { + this._secrets.push(secret); + } + + _doLog(level: string, ...args: any[]) { + let text = args.join(" "); + if (this.customWriter) { + for (const item of this._secrets) { if (item == null) { continue; } @@ -66,18 +81,12 @@ export function buildLogger(write: (text: string) => void) { text = text.replaceAll(item, "*".repeat(item.length)); } } - write(text); - }, - }); - return logger; -} - -export type ILogger = Logger; - -export class PipelineLogger implements ILogger { - callStackLinesToSkip: number = 1; - readonly category: string = "pipeline"; - level: Level | string = "info"; + text = `[${formatDateIntl()}] [${level.toUpperCase()}] - ${text} \n`; + this.customWriter(text); + } + // @ts-ignore + this.logger[level](...args); + } _log(level: Level, data: any): void {} @@ -87,18 +96,39 @@ export class PipelineLogger implements ILogger { debug(message: any, ...args: any[]): void { if (this.isDebugEnabled()) { + this._doLog("debug", message, ...args); } } - error(message: any, ...args: any[]): void {} + error(message: any, ...args: any[]): void { + if (this.isErrorEnabled()) { + this._doLog("error", message, ...args); + } + } - fatal(message: any, ...args: any[]): void {} + fatal(message: any, ...args: any[]): void { + if (this.isFatalEnabled()) { + this._doLog("fatal", message, ...args); + } + } - info(message: any, ...args: any[]): void {} + info(message: any, ...args: any[]): void { + if (this.isInfoEnabled()) { + this._doLog("info", message, ...args); + } + } - trace(message: any, ...args: any[]): void {} + trace(message: any, ...args: any[]): void { + if (this.isTraceEnabled()) { + this._doLog("trace", message, ...args); + } + } - warn(message: any, ...args: any[]): void {} + warn(message: any, ...args: any[]): void { + if (this.isWarnEnabled()) { + this._doLog("warn", message, ...args); + } + } isDebugEnabled(): boolean { return logger.isDebugEnabled(); diff --git a/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue b/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue index b26a305e..d78bce6e 100644 --- a/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue +++ b/packages/ui/certd-client/src/views/certd/pipeline/pipeline/index.vue @@ -758,7 +758,7 @@ export default defineComponent({ //检查输出的stepid是否存在 let hasError = false; - let errorMessages = []; + let errorMessages: any = []; let errorIndex = 1; eachSteps(pp, (step: any, task: any, stage: any) => { if (step.disabled !== true) {