gpt4 book ai didi

java - Log4j2 - RootLogger 附加程序不工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:58:54 27 4
gpt4 key购买 nike

我有一个使用 log4j2 进行日志记录的 Maven 项目。我想覆盖 RootLogger 的模式。

这是我的 log4j2.properties 文件:

status = debug
name= properties_configuration

# Give directory path where log files should get stored
property.basePath = ./log/

appenders =console, rolling

# ConsoleAppender will print logs on console
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout

# Specify the pattern of the logs
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t]
[%c] [%M] [%l] - %msg %xEx %n


# RollingFileAppender will print logs in file which can be rotated based on
time or size
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName=${basePath}/app.log
appender.rolling.filePattern=${basePath}app_%d{yyyyMMdd}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t]
[%c] [%M] [%l] - %msg %xEx %n
appender.rolling.policies.type = Policies

# Rotate log file each day
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.delete.type = Delete
appender.rolling.strategy.delete.basePath = ${basePath}
appender.rolling.strategy.delete.maxDepth = 1
appender.rolling.strategy.delete.ifLastModified.type = IfLastModified
# Delete files older than 30 days
appender.rolling.strategy.delete.ifLastModified.age = 30d


logger.example.name = com.parohia
logger.example.level = debug
logger.example.appenderRef.rolling.ref = fileLogger
logger.example.appenderRef.console.ref = consoleLogger

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = consoleLogger

这是我的 log4j 的 pom.xml 文件:

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>

我这样称呼我的记录器:

      private final Logger logger = LogManager.getLogger(PersoaneController.class);
try{
.....
}
catch(Exception ex){
logger.error(ex.getMessage(),ex);
}

我的配置有两个问题。首先是根记录器不使用我的 consoleAppender(警告等以默认格式出现),并且在记录错误时我只得到第一行而不是整个堆栈跟踪)。

最佳答案

如果您的模式实际上在属性文件中分成多行,如您的问题所示:

appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] 
[%c] [%M] [%l] - %msg %xEx %n

appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t]
[%c] [%M] [%l] - %msg %xEx %n

那么您需要更改它们,使模式完全在一行上:

appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg %xEx %n

appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %level [%t] [%c] [%M] [%l] - %msg %xEx %n

关于java - Log4j2 - RootLogger 附加程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47113583/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com