最好是使用SpringBoot默认支持的Logback, 但是也有其他的业务需要使用Log4j.
SpringBoot版本:2.1.7.RELEASE , 1.5.19.RELEASE

使用实例

添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter</artifactId>
  4. <exclusions>
  5. <exclusion>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-logging</artifactId>
  8. </exclusion>
  9. </exclusions>
  10. </dependency>
  11. <dependency>
  12. <groupId>log4j</groupId>
  13. <artifactId>log4j</artifactId>
  14. <version>1.2.17</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.slf4j</groupId>
  18. <artifactId>slf4j-log4j12</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>commons-logging</groupId>
  22. <artifactId>commons-logging</artifactId>
  23. <version>1.2</version>
  24. </dependency>

log4j xml配置

log4j.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
  4. <appender name="DEBUG_LOGGER" class="org.apache.log4j.ConsoleAppender">
  5. <!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 test dev -->
  6. <layout class="org.apache.log4j.PatternLayout">
  7. <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %5p [%F:%L] - %m%n"/>
  8. </layout>
  9. </appender>
  10. <appender name="DRUID_LOGGER" class="org.apache.log4j.DailyRollingFileAppender">
  11. <param name="File" value="${webapp.root}/logs2/druid/druid.log"/>
  12. <param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
  13. <param name="encoding" value="utf-8"/>
  14. <layout class="org.apache.log4j.PatternLayout">
  15. <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %5p [%F:%L] - %m%n"/>
  16. </layout>
  17. </appender>
  18. <logger name="druid.sql" additivity="false">
  19. <level value="DEBUG"/>
  20. <appender-ref ref="DRUID_LOGGER"/>
  21. </logger>
  22. <root>
  23. <level value="DEBUG"/>
  24. <appender-ref ref="DEBUG_LOGGER"/>
  25. </root>
  26. </log4j:configuration>

代码配置

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. static {
  4. //这里只是一个例子,根据具体情况进行修改
  5. System.setProperty("user.home", "/Users/admin/open/demo");
  6. System.setProperty("webapp.root", System.getProperty("user.home") + "/code");
  7. System.setProperty("org.springframework.boot.logging.LoggingSystem", Log4jLoggingSystem.class.getName());
  8. }
  9. public static void main(String[] args) {
  10. SpringApplication.run(DemoApplication.class, args);
  11. }
  12. }

Log4jLoggingSystem

  1. package com.example.demo.log;
  2. import org.apache.log4j.xml.DOMConfigurator;
  3. import org.springframework.boot.logging.LogFile;
  4. import org.springframework.boot.logging.LoggingInitializationContext;
  5. import org.springframework.boot.logging.Slf4JLoggingSystem;
  6. import org.springframework.util.ResourceUtils;
  7. import java.io.FileNotFoundException;
  8. import java.net.URL;
  9. /**
  10. * @author chenshun00@gmail.com
  11. * @since 2019-09-24 13:44
  12. */
  13. public class Log4jLoggingSystem extends Slf4JLoggingSystem {
  14. public Log4jLoggingSystem(ClassLoader classLoader) {
  15. super(classLoader);
  16. }
  17. @Override
  18. public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) {
  19. super.initialize(initializationContext, configLocation, logFile);
  20. }
  21. @Override
  22. protected void reinitialize(LoggingInitializationContext initializationContext) {
  23. try {
  24. String selfInitializationConfig = getSelfInitializationConfig();
  25. URL url = ResourceUtils.getURL(selfInitializationConfig);
  26. DOMConfigurator.configure(url);
  27. } catch (FileNotFoundException e) {
  28. throw new IllegalArgumentException(e);
  29. }
  30. }
  31. @Override
  32. protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) {
  33. try {
  34. URL url = ResourceUtils.getURL(location == null ? initializationContext.getEnvironment().getProperty("logging.config") : location);
  35. DOMConfigurator.configure(url);
  36. } catch (FileNotFoundException e) {
  37. throw new IllegalArgumentException(e);
  38. }
  39. }
  40. @Override
  41. protected String[] getStandardConfigLocations() {
  42. return new String[]{"log4j.xml", "log4j-prod.xml", "log4j-test.xml", "log4j-dev.xml"};
  43. }
  44. @Override
  45. protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
  46. try {
  47. URL url = ResourceUtils.getURL(initializationContext.getEnvironment().getProperty("logging.config"));
  48. DOMConfigurator.configure(url);
  49. } catch (FileNotFoundException e) {
  50. throw new IllegalArgumentException(e);
  51. }
  52. }
  53. }