我的Log4j2配置文件如下所示:
printSomething()
班级<Appenders>
<RollingRandomAccessFile name="APP_LOG_APPENDER" fileName="${sys:baseLogPath}/${appLogFileName}.log"
filePattern="${sys:baseLogPath}/backups/$${date:yyyy-MM}/${appLogFileName}-%d{yyyy-MM-dd}-%i.log.gz" immediateFlush="false">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [Thread: %t] %level [%c][%M] - %msg%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="APP_LOG_APPENDER" />
</Root>
</Loggers>
MyClass
日志消息显示为:
public class MyClass {
private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(MyClass.class);
public void someMethod() {
logger.debug("Some sample string at DEBUG level....");
logger.info("Some sample string at INFO level....");
logger.warn("Some sample string at WARN level....");
logger.error("Some sample string at ERROR level....");
}
}
我使用的是log4j2-2.5版本和相应的SLF4J API。
有人可以帮我理解为什么会这样吗?
答案 0 :(得分:0)
对我来说,下一个配置运行良好(未启用includeLocation="true"
选项):
src / main / resources / log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="PID">%5X{pid}</Property>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="LOG_PATTERN">%style{%d{yyyy-MM-dd HH:mm:ss.SSS}}{dim} %highlight{${LOG_LEVEL_PATTERN}} %style{${sys:PID}}{magenta} %style{---}{dim} %style{[%15.15t]}{dim} %style{%-40.40c{1.}}{cyan} %style{%22.22M}{magenta} %style{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}" />
</Console>
</Appenders>
<Loggers>
<logger name="com.github.daggerok" level="ALL" additivity="false">
<appender-ref ref="ConsoleAppender"/>
</logger>
<Root level="ALL">
<AppenderRef ref="ConsoleAppender"/>
</Root>
</Loggers>
</Configuration>
build.gradle
dependencies {
implementation(platform('org.apache.logging.log4j:log4j-bom:2.12.1'))
implementation 'org.apache.logging.log4j:log4j-api'
implementation 'org.apache.logging.log4j:log4j-core'
// ...
}
checkout this Travis CI build log output
> Task :test
com.github.daggerok.usermanagement.domain.user.UserEventSourcedRepositoryTest > should save user STANDARD_OUT
2019-08-17 18:23:53.358 DEBUG --- [ Test worker] u.d.u.UserInMemoryEventSourcedRepository save : about to save User(efb230a1-679e-4aac-b6c2-7eb25142de30) aggregate...
2019-08-17 18:23:53.375 DEBUG --- [ Test worker] u.d.u.UserInMemoryEventSourcedRepository save : cleared User(efb230a1-679e-4aac-b6c2-7eb25142de30) aggregate events.
2019-08-17 18:23:53.388 DEBUG --- [ Test worker] u.d.u.UserInMemoryEventSourcedRepository save : aggregate User(efb230a1-679e-4aac-b6c2-7eb25142de30) saved.
2019-08-17 18:23:53.392 DEBUG --- [ Test worker] u.d.u.UserInMemoryEventSourcedRepository find : start rebuild process for aggregate: efb230a1-679e-4aac-b6c2-7eb25142de30