一个Tomcat两个spring应用程序(war)两个单独的日志记录配置

时间:2016-08-11 07:20:55

标签: spring logging configuration log4j logback

如标题中所述,我有两个具有两种不同日志记录配置的应用程序。一旦我使用spring logging.file设置我就无法分开两个应用程序的配置。

问题恶化,因为一个应用程序正在使用logback.xml,而一个应用程序正在使用log4j.properties。

我尝试在一个应用程序中引入一个新配置参数,我可以在其中设置logback.xml的路径,但是我无法使新设置适用于应用程序中的所有日志记录。

 public static void main(String[] args) {
    reconfigureLogging();
    SpringApplication.run(IndexerApplication.class, args);
}

private static void reconfigureLogging() {
    if (System.getProperty("IndexerLogging") != null && !System.getProperty("IndexerLogging").isEmpty()) {
        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(context);
            // Call context.reset() to clear any previous configuration, e.g. default
            // configuration. For multi-step configuration, omit calling context.reset().
            System.out.println("SETTING: " + System.getProperty("IndexerLogging"));
            System.out.println("SETTING: " + System.getProperty("INDEXER_LOG_FILE"));
            context.reset();
            configurator.doConfigure(System.getProperty("IndexerLogging"));

        } catch (JoranException je) {
            System.out.println("FEHLER IN CONFIG");

        }
        logger.info("Entering application.");
    }
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    reconfigureLogging();
    return application.sources(applicationClass);
}

以上代码以某种方式工作。但是,写入配置中指定的日志文件的唯一日志条目是$ {IndexerLogging}指向的日志文件,是来自logger.info的条目("输入应用程序。"); :(

我真的不想将该代码附加到在应用程序中进行某些日志记录的每个类。

应用程序必须可以作为tomcat部署运行,但也可以作为使用集成tomcat的spring启动应用程序运行。

在第一次配置该应用程序中的日志记录时,是否可以设置如何将$ {IndexerLogging}的路径设置为读取配置文件的路径?

1 个答案:

答案 0 :(得分:0)

查看https://github.com/qos-ch/logback-extensions/wiki/Spring您可以配置要使用的logback配置文件。

相关问题