gpt4 book ai didi

java - 应用程序在外部 Tomcat 上启动之前进行 Spring Boot 日志记录

转载 作者:行者123 更新时间:2023-11-30 02:13:04 26 4
gpt4 key购买 nike

每当 Spring Boot 应用程序即将启动时,我都会尝试进行一些日志记录。

目前,我有一个实现 ApplicationEnvironmentPreparedEvent 的类,并且我正在 SpringBootApplication 的主函数中将其注册为监听器。

应用程序.java:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Logger appLogger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
System.out.println("INIT");
appLogger.info("Initializing");

SpringApplication sa = new SpringApplication();
sa.addListeners(new InitializationLogger());
sa.setSources(new HashSet<>(Collections.singletonList(Application.class)));
sa.run(args);

appLogger.info("--Application Started--");
}
}

初始化Logger.java

public class InitializationLogger implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger appLogger = LoggerFactory.getLogger(InitializationLogger.class);

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
System.out.println(event.getClass());
appLogger.info(this.getClass().getName());
}
}

问题是,每当我通过 Intellij 的 SpringBoot 运行程序或命令行作为 fat jar 运行应用程序时,它都工作得很好,但是当我将其部署到外部 Tomcat 时,这些日志不会显示(所有其他应用程序)日志工作正常)。

我该怎么做?

最佳答案

How can I do this?

覆盖SpringBootServletInitializer.configure ,类似:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
private static final Logger appLogger = LoggerFactory.getLogger(Application.class);

// for JAR deploy:
public static void main(String[] args) {
SpringApplicationBuilder builder = configureSpringBuilder(new SpringApplicationBuilder());
builder.application().run(args);
appLogger.info("--Application Started--");
}

// for WAR deploy:
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return super.configure(configureSpringBuilder(builder));
}

// Common:
private static SpringApplicationBuilder configureSpringBuilder(SpringApplicationBuilder builder) {
System.out.println("INIT");
appLogger.info("Initializing");

builder.application().addListeners(new InitializationLogger());
builder.application().setSources(new HashSet<>(Collections.singletonList(Application.class)));

return builder;
}
}

引用文献:

  1. Class SpringBootServletInitializer

    To configure the application either override the configure(SpringApplicationBuilder) method (...)

  2. Tutorial: Use Spring Boot to Build and Deploy WAR Files - Setting Up a Servlet

    To set the app up as a servlet we extend the main class with SpringBootServletInitializer and override the configure method using SpringApplicationBuilder.

  3. Deploying Spring Boot Applications - What about the Java EE Application Server?

    Modify your Application entry-point class thusly (...)

关于java - 应用程序在外部 Tomcat 上启动之前进行 Spring Boot 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49539673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com