最好是使用SpringBoot默认支持的Logback, 但是也有其他的业务需要使用Log4j.
SpringBoot版本:2.1.7.RELEASE , 1.5.19.RELEASE
使用实例
添加依赖
<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>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
log4j xml配置
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="DEBUG_LOGGER" class="org.apache.log4j.ConsoleAppender">
<!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 test dev -->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %5p [%F:%L] - %m%n"/>
</layout>
</appender>
<appender name="DRUID_LOGGER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${webapp.root}/logs2/druid/druid.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="encoding" value="utf-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %5p [%F:%L] - %m%n"/>
</layout>
</appender>
<logger name="druid.sql" additivity="false">
<level value="DEBUG"/>
<appender-ref ref="DRUID_LOGGER"/>
</logger>
<root>
<level value="DEBUG"/>
<appender-ref ref="DEBUG_LOGGER"/>
</root>
</log4j:configuration>
代码配置
@SpringBootApplication
public class DemoApplication {
static {
//这里只是一个例子,根据具体情况进行修改
System.setProperty("user.home", "/Users/admin/open/demo");
System.setProperty("webapp.root", System.getProperty("user.home") + "/code");
System.setProperty("org.springframework.boot.logging.LoggingSystem", Log4jLoggingSystem.class.getName());
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Log4jLoggingSystem
package com.example.demo.log;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.boot.logging.LogFile;
import org.springframework.boot.logging.LoggingInitializationContext;
import org.springframework.boot.logging.Slf4JLoggingSystem;
import org.springframework.util.ResourceUtils;
import java.io.FileNotFoundException;
import java.net.URL;
/**
* @author chenshun00@gmail.com
* @since 2019-09-24 13:44
*/
public class Log4jLoggingSystem extends Slf4JLoggingSystem {
public Log4jLoggingSystem(ClassLoader classLoader) {
super(classLoader);
}
@Override
public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
super.initialize(initializationContext, configLocation, logFile);
}
@Override
protected void reinitialize(LoggingInitializationContext initializationContext) {
try {
String selfInitializationConfig = getSelfInitializationConfig();
URL url = ResourceUtils.getURL(selfInitializationConfig);
DOMConfigurator.configure(url);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) {
try {
URL url = ResourceUtils.getURL(location == null ? initializationContext.getEnvironment().getProperty("logging.config") : location);
DOMConfigurator.configure(url);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
@Override
protected String[] getStandardConfigLocations() {
return new String[]{"log4j.xml", "log4j-prod.xml", "log4j-test.xml", "log4j-dev.xml"};
}
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
try {
URL url = ResourceUtils.getURL(initializationContext.getEnvironment().getProperty("logging.config"));
DOMConfigurator.configure(url);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}