【发布时间】:2018-06-12 07:08:39
【问题描述】:
我是 Spring Boot 的新手。我有一个正在运行的 Spring Boot 项目。我想使用log4j2(由于项目限制,我必然会使用log4j2本身)将所有不同级别的日志重定向到一个名为'test.log'的日志文件
问题是 - 尽管包含了所有正确的代码,但我无法在我的 test.log 中记录 INFO 级别的日志(我显然想记录错误和调试级别日志也是如此,但至少第一个 INFO 级别日志应该可以正常工作)
--- 我已排除默认日志记录并在 pom.xml 中包含 log4j2 依赖项:
<!-- Exclude Spring Boot's Default Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Add Log4j2 Dependency -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
--- 我在 application.properties 中包含了 logging.config:
logging.file=logs/test.log
logging.level.*=INFO
logging.config=src/main/resources/log4j2.properties
--- 我的log4j2.properties 如下所示:
#status = error // do i need this actually??
dest = logs/test.log
name = PropertiesConfig
property.filename = logs/test.log
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = logs/test.log
appender.rolling.filePattern = logs/test1-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d %-5p %-17c{2} (%30F:%L) %3x - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5
logger.rolling.name = com.thp.clinic.allergiesConditions
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
#rootCategory=INFO,rolling,stdout
#logging.level.root=info
#rootLogger.level = debug //do i necessarily need root Logger????
#rootLogger.appenderRefs = RollingFile
#rootLogger.appenderRef.stdout.ref = STDOUT
logger.rolling.name=org.hibernate.SQL
logger.rolling.level = debug
--- 我的测试 API 的控制器也有以下测试日志行:
//Logger logger = LogManager.getLogger //is included
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
--- 据我了解,我已经在代码中包含了所有必需的内容。但是我正面临这个问题 - 当我点击 API 时,只有休眠调试记录器被添加到 test.log;我包含在控制器中的五个测试记录器(甚至使用的其他信息级别日志)没有登录到 test.log;
控制台如下所示(五个记录器中的两个显示在控制台上,但这里也缺少 INFO 级别记录器):
20:05:42.989 [http-nio-8000-exec-1] ERROR com.test.app.appController - This is an error message
20:05:42.994 [http-nio-8000-exec-1] FATAL com.test.app.appController - This is a fatal message
Hibernate: //used hibernate queries are displayed to console as needed
如果有人能指出我需要改变的地方,那将是很大的帮助 在代码中。因为对log4j2理解不当,我猜 log4j2.properties 中的某些内容需要更改
请帮忙!! 提前致谢
【问题讨论】:
标签: spring spring-boot logging log4j log4j2