【问题标题】:Logs are not recorded when using external log configuration使用外部日志配置时不记录日志
【发布时间】:2022-08-23 22:59:47
【问题描述】:

我在 tomcat 上部署了 Spring MVC Web 应用程序。 (不是 Spring Boot)
我想在运行时使用外部日志文件来访问它。 Log4j2 库。
log4j.configurationFile 参数用于启用外部配置。
我还有外部.yaml 文件存储在服务器的文件系统上。

现在的行为:
如果我将相同的 log4j2.xml 放入我的资源中,一切正常。
使用外部 log4j2 配置,.log 文件在应用程序启动后创建,其中包含一些关于应用程序初始化的日志。但没有进一步记录。
此外,如果我在运行时创建额外的附加程序 - 不会创建文件。所以自动重新配置也不起作用。

经过一些搜索,我来到了这个配置以应用外部.yaml

@Configuration
public class PropertyConfig {

    public static final String ENV_HOME_VARIABLE = \"APP_HOME\";
    public static final String MAIN_CONFIG_FILE_NAME = \"app-config.yaml\";

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties(@Autowired ConfigurableEnvironment conf) throws MalformedURLException {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        String configPath = \"file:\" + System.getenv(ENV_HOME_VARIABLE) + \"/\" + MAIN_CONFIG_FILE_NAME;

        yaml.setResources(new UrlResource(configPath));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        Map map = conf.getSystemProperties();
        yaml.getObject().forEach((key, value) -> {
            map.put(key, value);
        });
        return propertySourcesPlaceholderConfigurer;
    }
}

这里还有log4j.configurationFileapp-config.yaml 中的初始化:

log4j:
 configurationFile: file:/home/user/appHome/app-log4j2.xml

和 app-log4j2.xml:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<Configuration monitorinterval=\"30\" status=\"WARN\" >

    <Appenders>
        <!-- Rolling File Appender -->
        <RollingFile name=\"rollingFileApplication\" bufferSize=\"0\" bufferedIO=\"false\" append=\"true\">
           <FileName>/home/user/appHome/log/app.log</FileName>
           <FilePattern>/home/user/appHome/log/archive/app%d{yyyy-MM-dd}.log.gz</FilePattern>
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval=\"1\" modulate=\"true\" />
            </Policies>

            <DefaultRolloverStrategy>
                <Delete basePath=\"/home/user/appHome/log/archive/\" maxDepth=\"1\">
                    <IfAll>
                        <IfFileName glob=\"app_*.log*\" />
                        <IfLastModified age=\"7d\" />
                    </IfAll>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level=\"TRACE\">
            <AppenderRef ref=\"rollingFileApplication\" level=\"TRACE\"/>
        </Root>

        <Logger name=\"ua\" additivity=\"true\" level=\"TRACE\">
            <appender-ref ref=\"rollingFileApplication\"/>
        </Logger>

    </Loggers>
</Configuration>

启动后app.log中的记录:

2022-07-28 12:53:39 PM [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
2022-07-28 12:53:40 PM [localhost-startStop-1] TRACE org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution
2022-07-28 12:54:10 PM [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
2022-07-28 12:54:10 PM [localhost-startStop-1] TRACE org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution
2022-07-28 12:54:25 PM [localhost-startStop-1] INFO  org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
2022-07-28 12:54:25 PM [localhost-startStop-1] TRACE org.springframework.core.io.support.PathMatchingResourcePatternResolver - Found Equinox FileLocator for OSGi bundle URL resolution

所以问题是,为什么你认为它不起作用以及如何使它起作用)。
准备提供任何其他信息。
将不胜感激任何建议或评论。

    标签: java spring log4j2


    【解决方案1】:

    经过一番搜索,我找到了解决方案。 看起来更像是解决方法,但它仍然有效:

        @Bean
        public static PropertySourcesPlaceholderConfigurer properties(@Autowired ConfigurableEnvironment conf) throws MalformedURLException {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
            String configPath = "file:" + System.getenv(ENV_HOME_VARIABLE) + "/" + MAIN_CONFIG_FILE_NAME;
    
            yaml.setResources(new UrlResource(configPath));
            propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
            Map map = conf.getSystemProperties();
            yaml.getObject().forEach((key, value) -> {
                map.put(key, value);
            });
    
       // solution
            LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
            File file = new File(System.getenv(ENV_HOME_VARIABLE) + "/app-log4j2.xml");
    
            // this will force a reconfiguration
            context.setConfigLocation(file.toURI());
       // solution
    
            return propertySourcesPlaceholderConfigurer;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多