Skip to content

Commit 23db199

Browse files
optimize: log-or-console,将控制台日志写入日志文件中。
1 parent b671c79 commit 23db199

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed
Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,47 @@
11
let log = console
22

3+
// 将console中的日志缓存起来,当setLogger时,将控制台的日志写入日志文件
4+
let backups = []
5+
6+
function backup (fun, args) {
7+
if (backups === null) {
8+
return
9+
}
10+
11+
try {
12+
backups.push({
13+
fun,
14+
args,
15+
})
16+
17+
// 最多缓存 100 条
18+
if (backups.length > 100) {
19+
backups = backups.slice(1)
20+
}
21+
} catch {
22+
}
23+
}
24+
25+
function printBackups () {
26+
if (backups === null) {
27+
return
28+
}
29+
30+
try {
31+
const backups0 = backups
32+
backups = null
33+
34+
for (const item of backups0) {
35+
log[item.fun](...[`[console]`, ...item.args])
36+
}
37+
} catch {
38+
}
39+
}
40+
341
function _doLog (fun, args) {
442
if (log === console) {
543
log[fun](...[`[${fun.toUpperCase()}]`, ...args])
44+
backup(fun, args) // 控制台日志备份起来
645
} else {
746
log[fun](...args)
847
}
@@ -11,22 +50,26 @@ function _doLog (fun, args) {
1150
module.exports = {
1251
setLogger (logger) {
1352
log = logger
53+
54+
try {
55+
if (backups && backups.length > 0) {
56+
log.info('[util.log-or-console.js] logger已设置,现将控制台的日志记录到日志文件中')
57+
printBackups()
58+
}
59+
} catch {
60+
}
1461
},
1562

16-
debug () {
17-
// eslint-disable-next-line prefer-rest-params
18-
_doLog('debug', arguments)
63+
debug (...args) {
64+
_doLog('debug', args)
1965
},
20-
info () {
21-
// eslint-disable-next-line prefer-rest-params
22-
_doLog('info', arguments)
66+
info (...args) {
67+
_doLog('info', args)
2368
},
24-
warn () {
25-
// eslint-disable-next-line prefer-rest-params
26-
_doLog('warn', arguments)
69+
warn (...args) {
70+
_doLog('warn', args)
2771
},
28-
error () {
29-
// eslint-disable-next-line prefer-rest-params
30-
_doLog('error', arguments)
72+
error (...args) {
73+
_doLog('error', args)
3174
},
3275
}

packages/core/src/utils/util.logger.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const path = require('node:path')
22
const log4js = require('log4js')
33
const logOrConsole = require('./util.log-or-console')
4-
const configFromFiles = require('../config/index.js').configFromFiles
4+
const defaultConfig = require('../config/index.js')
5+
const configFromFiles = defaultConfig.configFromFiles
56

67
// 日志级别
78
const level = process.env.NODE_ENV === 'development' ? 'debug' : 'info'
@@ -28,8 +29,8 @@ const appenderConfig = {
2829
pattern: 'yyyy-MM-dd',
2930
compress: true, // 压缩日志文件
3031
keepFileExt: true, // 保留日志文件扩展名为 .log
31-
backups: Math.ceil(configFromFiles.app.keepLogFileCount) || 15, // 保留日志文件数
32-
maxLogSize: Math.ceil((configFromFiles.app.maxLogFileSize || 0) * 1024 * 1024 * (configFromFiles.app.maxLogFileSizeUnit === 'GB' ? 1024 : 1)), // 目前单位只有GB和MB
32+
backups: Math.ceil(configFromFiles.app.keepLogFileCount) || defaultConfig.app.keepLogFileCount, // 保留日志文件数
33+
maxLogSize: Math.ceil((configFromFiles.app.maxLogFileSize || defaultConfig.app.maxLogFileSize) * 1024 * 1024 * (configFromFiles.app.maxLogFileSizeUnit === 'GB' ? 1024 : 1)), // 目前单位只有GB和MB
3334
}
3435

3536
let log = null

0 commit comments

Comments
 (0)