我在SpringBoot应用程序中使用Log4J 2.
执行日志记录的类是:
package guru.springframework.blog.log4jproperties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class PropertiesConfigurationDemo {
private static Logger logger =LogManager.getLogger("PropertiesConfigurationDemo.class");
public void performSomeTask(){
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");
}
}
我的log4j2.xml位于src-> main-> resources。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<File name="A1" fileName="${log-path}/A1.log" >
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="guru.springframework.blog.log4jproperties" level="debug" additivity="false">
<AppenderRef ref="A1" level="debug"/>
</Logger>
<Root level="info" additivity="false">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
当我在PropertiesConfigurationDemo上调用performSomeTask()方法时,日志消息将作为以下内容发送到Console:
7:39:12.619 [main] DEBUG PropertiesConfigurationDemo.class - This is a debug message
07:39:12.643 [main] INFO PropertiesConfigurationDemo.class - This is an info message
07:39:12.647 [main] WARN PropertiesConfigurationDemo.class - This is a warn message
07:39:12.653 [main] ERROR PropertiesConfigurationDemo.class - This is an error message
07:39:12.655 [main] ERROR PropertiesConfigurationDemo.class - This is a fatal message
为什么日志消息没有发送到文件?即使如果我将根记录器的AppenderRef设置为A1,仍然会将消息发送到控制台。'
<Root level="info" additivity="false">
<AppenderRef ref="A1"/>
</Root>
看来我的log4j2.xml存在一些问题或根本没有被提取。任何帮助都将得到诚挚的承认。
顺便说一句,我在MAVEN POM中有以下log4j依赖项。
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
由于 Simanta
答案 0 :(得分:0)
我完成了。问题在于POM依赖性。我排除了默认的logback classic并添加了log4j2。这就是我修改过的。
<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-log4j2</artifactId>
</dependency>
我的log4j2.xml位于src-&gt; main-&gt;资源现在。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<File name="File" fileName="${log-path}/log4j2.log" >
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>
</PatternLayout>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<pattern>
[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
</pattern>>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="guru.springframework.blog.log4jproperties" level="debug" additivity="false">
<AppenderRef ref="File" level="info"/>
<AppenderRef ref="STDOUT" level="debug"/>
</Logger>
<Root level="all">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
在我的应用程序类中,我将Logger检索为。
private static Logger logger = LogManager.getLogger("guru.springframework.blog.log4jproperties");
现在,日志记录将被定向到控制台和文件。