我正在尝试在Spring Boot 2.2.1中使用Apache Log4j 2.12.1,但运行时没有任何关于缺少log4j 2配置文件的警告。以下是用于记录日志的代码(简单的测试类以测试它是否可行):
然而,如果我将相同的配置用于简单的Java项目,它可以正常工作。
package com.example.Log4jdemoSpringboot;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Log4jDemoSpringBootApplication {
static Logger logger = LogManager.getLogger(Log4jDemoSpringBootApplication.class);
public static void main(String[] args) {
SpringApplication.run(Log4jDemoSpringBootApplication.class, args);
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.debug("debug");
logger.fatal("fatal");
}
}
以下是输出结果:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.1.RELEASE)
2019-11-18 12:19:20.844 INFO 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : Starting Log4jDemoSpringBootApplication on Gurpreet-PC with PID 13136 (C:\Development\eclipse-workspace\Log4j-demo-Spring-boot\target\classes started by Gurpreet in C:\Development\eclipse-workspace\Log4j-demo-Spring-boot)
2019-11-18 12:19:20.872 INFO 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : No active profile set, falling back to default profiles: default
2019-11-18 12:19:26.946 INFO 13136 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-11-18 12:19:27.009 INFO 13136 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-11-18 12:19:27.011 INFO 13136 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.27]
2019-11-18 12:19:27.442 INFO 13136 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-11-18 12:19:27.444 INFO 13136 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 6320 ms
2019-11-18 12:19:28.213 INFO 13136 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-18 12:19:28.925 INFO 13136 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-11-18 12:19:28.937 INFO 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : Started Log4jDemoSpringBootApplication in 10.398 seconds (JVM running for 13.269)
2019-11-18 12:19:28.945 INFO 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : info
2019-11-18 12:19:28.949 WARN 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : warn
2019-11-18 12:19:28.952 ERROR 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : error
2019-11-18 12:19:28.953 ERROR 13136 --- [ main] c.e.L.Log4jDemoSpringBootApplication : fatal
然而,如果我将相同的配置用于简单的Java项目,它可以正常工作。