问题描述
我在springboot中使用了log4j2,已经创建了日志文件,但是未将日志写入文件中.
I have used log4j2 with springboot, log file has been created but logs are not written in file.
log4j2.properties
name=PropertiesConfig
property.filename = /export/home/apps/logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/app-frontend.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
pom.xml
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
使用记录器的方法
private static Logger logger = LogManager.getLogger();
@RequestMapping(value="/check", method = RequestMethod.GET)
public String healthCheck() {
logger.debug("Health-check invoked");
return "Hey, I am fine";
}
上面我提到了我使用的代码.仍然找不到解决方法.日志甚至没有出现在控制台中.
Above I have mention the codes I have used. Still can not find a way to solve. Logs are even not appearing in the console.
推荐答案
在spring-boot中使用"log4j2.properties"进行配置有一定的局限性. Log4J 2最初发布时不支持通过属性文件进行配置.如文档
Usage of "log4j2.properties" for configuration in spring-boot has certain limitations. Log4J 2 did not support configuration through the properties file when it was initially released. It was from Log4J 2.4 that support for the properties file was again added, but with a completely different syntax.As mentioned in the documentation
从2.6版开始,不再需要此标识符列表,因为会在首次使用时推断出名称,但是,如果您希望使用更复杂的标识符,则仍必须使用该列表.如果存在该列表,则将使用它.
As of version 2.6, this list of identifiers is no longer required as names are inferred upon first usage, however if you wish to use more complex identifies you must still use the list. If the list is present it will be used.
从Spring Boot版本1.4.0开始,使用的log4j2 api版本为2.6.2.请注意,spring-boot使用slf4j api支持多个基础Logging Framework.将基于属性的配置用于log4j2时,似乎没有问题,而无法解决slf4j绑定的类路径依赖项.
As of spring boot release 1.4.0, the log4j2 api version used is 2.6.2. Note that spring-boot uses the slf4j api to support multiple underlying Logging Framework. There seems to be an issue when using properties based configuration for log4j2 without juggling with classpath dependencies for slf4j bindings.
使用基于XML(或yaml/json)的配置来实现相同的配置并启用log4j2能够使用的所有功能是很有意义的.
It would make sense to use the XML (or yaml/json) based configuration to achieve the same and to enable the usage of all the features log4j2 is capable of.
这是具有相同属性的基于xml的配置.
Here is an xml based configuration for the same properties.
log4j2.xml
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
<Properties>
<Property name="filename">export/home/apps/logs</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="LOGFILE"
fileName="${filename}/app-frontend.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="guru.springframework.blog.log4j2properties" level="debug">
<AppenderRef ref="LOGFILE"
level="debug" />
</Logger>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
这篇关于已创建Log4j2文件,但未写入日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!