简体   繁体   English

如何在 spring 引导服务器启动期间查看自动配置日志 output

[英]How to view autoconfigure log output during spring boot server start

How to view autoconfigure log output during spring boot server start如何在 spring 引导服务器启动期间查看自动配置日志 output

I have created a spring boot application.我创建了一个 spring 引导应用程序。 It uses a shared library (Spring boot jar via maven dependency).它使用共享库(Spring boot jar 通过 maven 依赖项)。 Shared library class is loaded via META-INF/spring.factories共享库 class 通过 META-INF/spring.factories 加载

I have mentioned the classes from the library in spring.factories.我在 spring.factories 中提到了库中的类。 The job of shared library is to read Vault role id and Vault secret id value from application.properties and call a REST API and fetch secrets from Vault.共享库的工作是从 application.properties 中读取 Vault 角色 id 和 Vault secret id 值,并调用 REST API 并从 Vault 中获取机密。 After fetching the secret it sets the value again in system property.获取秘密后,它会在系统属性中再次设置该值。

        for (Map.Entry<String, String> entry : allSecrets.entrySet())
        {
            System.setProperty(entry.getKey(), entry.getValue());
        }

Everything is working as expected.一切都按预期工作。 But I am not able to see logs from shared library in my logs.但我无法在我的日志中看到来自共享库的日志。 shared library's package structure is com.myorg.abc.共享库的 package 结构为 com.myorg.abc。 My spring boot package structure is com.myorg.xyz我的 spring 启动 package 结构是 com.myorg.xyz

I tried the following in application properties.我在应用程序属性中尝试了以下内容。

logging.level.root= DEBUG
logging.level.com.myorg.xyz: DEBUG
logging.level.com.myorg.abc: DEBUG
logging.level.org.springframework.boot.autoconfigure.logging=DEBUG

I am able to get logs only from my application but not from shared library.我只能从我的应用程序中获取日志,但不能从共享库中获取日志。 But when I change the shared library Logger.error to System.out, then I am getting the message in my application.但是当我将共享库 Logger.error 更改为 System.out 时,我会在我的应用程序中收到消息。 How to view shared library's log in my application.如何在我的应用程序中查看共享库的日志。

Spring boot initializes logging at least 3 times. Spring 引导初始化日志记录至少 3 次。 The first happens when SpringApplication is loaded.第一次发生在加载 SpringApplication 时。 It creates an SLF4J Logger before anything in Spring is accessed.它在访问 Spring 中的任何内容之前创建一个 SLF4J 记录器。 This causes whatever logging implementation you have chosen to initialize.这会导致您选择初始化的任何日志记录实现。 By default, it will use the logging configuration in the Spring jar.默认情况下,它将使用 Spring jar 中的日志记录配置。 With Log4j 2 you can override this by setting log4j.configurationFile to the location of your desired configuration either as a system property or in a log4j.component.properties file.使用 Log4j 2 您可以通过将 log4j.configurationFile 作为系统属性或在 log4j.component.proper

Everything Spring does will be logged using this configuration until it initializes the logging configuration again, which is controlled by bootstrap.yml. Spring 所做的一切都将使用此配置记录,直到它再次初始化记录配置,这由 bootstrap.yml 控制。 Finally, your application's logging configuration is initialized which is configured either from application.yml or again from bootstrap.yml.最后,您的应用程序的日志配置被初始化,该配置可以从 application.yml 或再次从 bootstrap.yml 配置。

I replaced org.springframework.boot.env.EnvironmentPostProcessor with org.springframework.context.ApplicationListener in Spring.factories and it fixed the issue.我用Spring.factories中的 org.springframework.context.ApplicationListener 替换了 org.springframework.boot.env.EnvironmentPostProcessor 并解决了这个问题。 I was able to get logs from shared library in invoking application.我能够在调用应用程序时从共享库中获取日志。

Spring.factories Spring.factories

org.springframework.context.ApplicationListener=com.mypackage.MyClassName

MyClassName.java MyClassName.java

public class MyClassName implements ApplicationListener<ApplicationPreparedEvent>
{
    private static final Logger LOGGER = LoggerFactory.getLogger(MyClassName.class);

    @Override
    public void onApplicationEvent(ApplicationPreparedEvent applicationPreparedEvent)
    {
        ConfigurableEnvironment configurableEnvironment = applicationPreparedEvent.getApplicationContext()
                .getEnvironment();
        String roleId = configurableEnvironment.getProperty(Constants.VAULT_ROLE_ID_LITERAL);
        String secretId = configurableEnvironment.getProperty(Constants.VAULT_SECRET_ID_LITERAL);
        ...

            Optional<String> errorMessage = ServiceUtil.validateSystemProperty(roleId, secretId);

            if (!errorMessage.isPresent())
            {
                Map<String, String> secret = ServiceUtil.getSecret(roleId, secretId);
                    for (Map.Entry<String, String> entry : secret.entrySet())
                    {
                        System.setProperty(entry.getKey(), entry.getValue());
                    }
                    LOGGER.info("Successfully populated secrets from Vault in system property");
            }
            else
            {
                LOGGER.error("Failed to populate secrets from Vault in system property. Error:{}", errorMessage.get());
            }
    }
}

application.properties应用程序属性

logging.level.com.myorg.abc: DEBUG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM