From 23db1994c3428f235f94b26c2f8e0b64246f03c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Wed, 12 Feb 2025 11:06:14 +0800 Subject: [PATCH] =?UTF-8?q?optimize:=20log-or-console=EF=BC=8C=E5=B0=86?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=8F=B0=E6=97=A5=E5=BF=97=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E4=B8=AD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/src/utils/util.log-or-console.js | 67 +++++++++++++++---- packages/core/src/utils/util.logger.js | 7 +- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/packages/core/src/utils/util.log-or-console.js b/packages/core/src/utils/util.log-or-console.js index ea92884..910b0fd 100644 --- a/packages/core/src/utils/util.log-or-console.js +++ b/packages/core/src/utils/util.log-or-console.js @@ -1,8 +1,47 @@ let log = console +// 将console中的日志缓存起来,当setLogger时,将控制台的日志写入日志文件 +let backups = [] + +function backup (fun, args) { + if (backups === null) { + return + } + + try { + backups.push({ + fun, + args, + }) + + // 最多缓存 100 条 + if (backups.length > 100) { + backups = backups.slice(1) + } + } catch { + } +} + +function printBackups () { + if (backups === null) { + return + } + + try { + const backups0 = backups + backups = null + + for (const item of backups0) { + log[item.fun](...[`[console]`, ...item.args]) + } + } catch { + } +} + function _doLog (fun, args) { if (log === console) { log[fun](...[`[${fun.toUpperCase()}]`, ...args]) + backup(fun, args) // 控制台日志备份起来 } else { log[fun](...args) } @@ -11,22 +50,26 @@ function _doLog (fun, args) { module.exports = { setLogger (logger) { log = logger + + try { + if (backups && backups.length > 0) { + log.info('[util.log-or-console.js] logger已设置,现将控制台的日志记录到日志文件中') + printBackups() + } + } catch { + } }, - debug () { - // eslint-disable-next-line prefer-rest-params - _doLog('debug', arguments) + debug (...args) { + _doLog('debug', args) }, - info () { - // eslint-disable-next-line prefer-rest-params - _doLog('info', arguments) + info (...args) { + _doLog('info', args) }, - warn () { - // eslint-disable-next-line prefer-rest-params - _doLog('warn', arguments) + warn (...args) { + _doLog('warn', args) }, - error () { - // eslint-disable-next-line prefer-rest-params - _doLog('error', arguments) + error (...args) { + _doLog('error', args) }, } diff --git a/packages/core/src/utils/util.logger.js b/packages/core/src/utils/util.logger.js index 1cb1134..bcf43c2 100644 --- a/packages/core/src/utils/util.logger.js +++ b/packages/core/src/utils/util.logger.js @@ -1,7 +1,8 @@ const path = require('node:path') const log4js = require('log4js') const logOrConsole = require('./util.log-or-console') -const configFromFiles = require('../config/index.js').configFromFiles +const defaultConfig = require('../config/index.js') +const configFromFiles = defaultConfig.configFromFiles // 日志级别 const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info' @@ -28,8 +29,8 @@ const appenderConfig = { pattern: 'yyyy-MM-dd', compress: true, // 压缩日志文件 keepFileExt: true, // 保留日志文件扩展名为 .log - backups: Math.ceil(configFromFiles.app.keepLogFileCount) || 15, // 保留日志文件数 - maxLogSize: Math.ceil((configFromFiles.app.maxLogFileSize || 0) * 1024 * 1024 * (configFromFiles.app.maxLogFileSizeUnit === 'GB' ? 1024 : 1)), // 目前单位只有GB和MB + backups: Math.ceil(configFromFiles.app.keepLogFileCount) || defaultConfig.app.keepLogFileCount, // 保留日志文件数 + maxLogSize: Math.ceil((configFromFiles.app.maxLogFileSize || defaultConfig.app.maxLogFileSize) * 1024 * 1024 * (configFromFiles.app.maxLogFileSizeUnit === 'GB' ? 1024 : 1)), // 目前单位只有GB和MB } let log = null