optimize: log-or-console,将控制台日志写入日志文件中。
parent
b671c79621
commit
23db1994c3
|
@ -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)
|
||||
},
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue