@@ -2,6 +2,7 @@ const path = require('node:path')
22const log4js = require ( 'log4js' )
33const configFromFiles = require ( '../config/index' ) . configFromFiles
44
5+ // 日志级别
56const level = process . env . NODE_ENV === 'development' ? 'debug' : 'info'
67
78function getDefaultConfigBasePath ( ) {
@@ -17,39 +18,68 @@ function getDefaultConfigBasePath () {
1718 }
1819}
1920
20- // 日志文件名
21- const coreLogFilename = path . join ( getDefaultConfigBasePath ( ) , '/core.log' )
22- const guiLogFilename = path . join ( getDefaultConfigBasePath ( ) , '/gui.log' )
23- const serverLogFilename = path . join ( getDefaultConfigBasePath ( ) , '/server.log' )
21+ // 日志文件目录
22+ const basePath = getDefaultConfigBasePath ( )
2423
25- // 日志相关配置
26- const backups = configFromFiles . app . keepLogFileCount // 保留日志文件数
24+ // 通用日志配置
2725const appenderConfig = {
2826 type : 'file' ,
2927 pattern : 'yyyy-MM-dd' ,
30- keepFileExt : true , // 保留日志文件扩展名
3128 compress : true , // 压缩日志文件
29+ keepFileExt : true , // 保留日志文件扩展名为 .log
30+ backups : configFromFiles . app . keepLogFileCount , // 保留日志文件数
31+ }
32+
33+ let log = null
34+
35+ // 设置一组日志配置
36+ function log4jsConfigure ( categories ) {
37+ const config = {
38+ appenders : {
39+ std : { type : 'stdout' } ,
40+ } ,
41+ categories : {
42+ default : { appenders : [ 'std' ] , level } ,
43+ } ,
44+ }
45+
46+ for ( const category of categories ) {
47+ config . appenders [ category ] = { ...appenderConfig , filename : path . join ( basePath , `/${ category } .log` ) }
48+ config . categories [ category ] = { appenders : [ category , 'std' ] , level }
49+ }
50+
51+ log4js . configure ( config )
52+
53+ // 拿第一个日志类型来logger并设置到log变量中
54+ log = log4js . getLogger ( categories [ 0 ] )
3255
33- // 以下三个配置都设置,兼容新旧版本
34- backups,
35- numBackups : backups ,
36- daysToKeep : backups ,
56+ log . info ( '设置日志配置完成:' , config )
3757}
3858
39- // 设置日志配置
40- log4js . configure ( {
41- appenders : {
42- std : { type : 'stdout' } ,
43- core : { ...appenderConfig , filename : coreLogFilename } ,
44- gui : { ...appenderConfig , filename : guiLogFilename } ,
45- server : { ...appenderConfig , filename : serverLogFilename } ,
46- } ,
47- categories : {
48- default : { appenders : [ 'std' ] , level } ,
49- core : { appenders : [ 'core' , 'std' ] , level } ,
50- gui : { appenders : [ 'gui' , 'std' ] , level } ,
51- server : { appenders : [ 'server' , 'std' ] , level } ,
52- } ,
53- } )
59+ module . exports = {
60+ getLogger ( category ) {
61+ if ( ! category ) {
62+ if ( log ) {
63+ log . error ( '未指定日志类型,无法配置并获取日志对象!!!' )
64+ }
65+ throw new Error ( '未指定日志类型,无法配置并获取日志对象!!!' )
66+ }
67+
68+ if ( category === 'core' || category === 'gui' ) {
69+ // core 和 gui 的日志配置,因为它们在同一进程中,所以一起配置,且只能配置一次
70+ if ( log == null ) {
71+ log4jsConfigure ( [ 'core' , 'gui' ] )
72+ }
5473
55- module . exports = log4js
74+ return log4js . getLogger ( category )
75+ } else {
76+ if ( log == null ) {
77+ log4jsConfigure ( [ category ] )
78+ } else {
79+ log . error ( `当前进程已经设置过日志配置,无法设置 "${ category } " 的配置,先临时返回 "${ log . category } " 的 log 进行日志记录。如果与其他类型的日志在同一进程中写入,请参照 core 和 gui 一起配置` )
80+ }
81+
82+ return log
83+ }
84+ } ,
85+ }
0 commit comments