SpringBoot和log4j2-当SpringApplication.run()

时间:2019-02-08 14:04:27

标签: spring spring-boot log4j2

我想使用Spring Boot和log4j2。

我有这个pom:

    <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>

这是我的主要课程:

@Component("batchLauncher")
@Import({ MyConfiguration.class })
public class MyLauncher implements CommandLineRunner {

    private static Logger log = LogManager.getLogger(MyLauncher.class);

    @Autowired
    MyController myController;

    public static void main(String[] args) {
        log.info("STARTING");
        SpringApplication.run(MyLauncher.class, args);
        log.info("FINISHED");
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("START Batch");
        MyController.start();
        log.info("END Batch");      
    }
}

我使用以下选项启动jar:

-Dlog4j.configurationFile=C:\log4j2.properties

应用程序启动时,控制台会向我显示:

    DEBUG StatusLogger Reconfiguration complete for context[name=18b4aac2] at URI C:\log4j2.properties (org.apache.logging.log4j.core.LoggerContext@72057ecf) with optional ClassLoader: null
DEBUG StatusLogger Shutdown hook enabled. Registering a new one.
DEBUG StatusLogger LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@72057ecf] started OK.
2019-02-08 14:57:31.047 INFO [main] [it.batch.MyLauncher] [main] [it.batch.MyLauncher.main] - STARTING THE APPLICATION


DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@6c80d78a
DEBUG StatusLogger Not in a ServletContext environment, thus not loading WebLookup plugin.
DEBUG StatusLogger Loaded configuration from 
...
DEBUG StatusLogger LoggerContext[name=18b4aac2, org.apache.logging.log4j.core.LoggerContext@72057ecf] started OK with configuration XmlConfiguration[location=jar:file:/C:/Users/G0426/.m2/repository/org/springframework/boot/spring-boot/2.1.2.RELEASE/spring-boot-2.1.2.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml].


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-02-08 14:57:31.753  INFO 13496 --- [           main] i.f.c.c.o.b.Info

从日志中可以看到,log4j2加载我的log4j属性文件直到行

SpringApplication.run(MyLauncher.class, args);

将上一行写到日志文件中,然后运行SpringApplication.run(...),然后加载第二个实例/上下文,并使用位于以下位置的默认配置log4j2开始记录日志:

file:/C:/Users/G0426/.m2/repository/org/springframework/boot/spring-boot/2.1.2.RELEASE/spring-boot-2.1.2.RELEASE.jar!/org/springframework/boot/logging/log4j2/log4j2.xml

我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

SpringApplication.run(MyLauncher.class, args);是Spring应用程序的起点。 Spring无法控制执行之前记录的任何内容。

因此考虑到基于Spring的日志记录配置仅在其上下文加载后才启动,因此该行为是适当的。另外,Spring Boot使用logging.config属性来加载日志配置。

您可以尝试设置-Dlogging.config=C:\log4j.properties

有关更多详细信息,请参见Spring Boot Documentation