Spring-XD不读logback.xml

Spring-XD does not read logback.xml

我正在尝试 运行 Spring-XD 中的一份工作,位于以下路径下:

/spring-xd/xd/modules/job/MyJobName (I'll call this path MyJobName below)

我的 jar,位于 MyJobName/lib 下,在其根路径中包含文件 logback.xml。不幸的是,Spring-XD 似乎完全忽略了该文件。当我 运行 通过我的 IDE (IntelliJ) 工作时,日志记录工作正常,但是当我 运行 它使用 Spring-XD 时,它完全忽略了我的 SiftingAppender。

这是我的 logback.xml 文件的样子:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%5p %-25logger{25} %m %n</pattern>
        </encoder>
    </appender>

    <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>publication.run.id</key>
            <defaultValue>unknown</defaultValue>
        </discriminator>
        <sift>
            <appender name="FILE-${publication.run.id}" class="ch.qos.logback.core.FileAppender">
                <file>/data/${publication.run.id}/logs/process.log</file>
                <append>true</append>
                <layout class="ch.qos.logback.classic.PatternLayout">
                    <pattern>%5p %-25logger{25} %m %n</pattern>
                </layout>
            </appender>
        </sift>
    </appender>

    <logger name="com.bitwiseor">
        <level value="INFO" />
    </logger>

    <logger name="org.springframework">
        <level value="INFO" />
    </logger>

    <root>
        <level value="INFO" />
        <appender-ref ref="SIFT" />
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

我想把这个 logback.xml 文件放在 /spring-xd/xd/config 下,或者放在另一个配置文件夹下,但我试过都没用。我尝试查看 Spring-XD 文档,但一无所获。

如有任何见解,我们将不胜感激。

您的 logback 配置似乎有一些小问题。

尝试将以下配置放入您的应用程序类路径;应该可以解决问题。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%5p %-25logger{25} %m %n</pattern>
        </encoder>
    </appender>
    <appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
        <discriminator>
            <key>publication.run.id</key>
            <defaultValue>unknown</defaultValue>
        </discriminator>
        <sift>
            <appender name="FILE-${publication.run.id}" class="ch.qos.logback.core.FileAppender">
                <file>/data/${publication.run.id}/logs/process.log</file>
                <append>true</append>
                <layout class="ch.qos.logback.classic.PatternLayout">
                    <pattern>%5p %-25logger{25} %m %n</pattern>
                </layout>
            </appender>
        </sift>
    </appender>
    <logger name="com.bitwiseor" level="INFO" />
    <logger name="org.springframework" level="INFO" />
    <root level="INFO">
        <appender-ref ref="SIFT" />
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

注意以下部分根据logback配置参考更改:

<logger name="com.bitwiseor" level="INFO" />
<logger name="org.springframework" level="INFO" />
<root level="INFO">
    <appender-ref ref="SIFT" />
    <appender-ref ref="CONSOLE" />
</root>

您必须将 Logback 直接放在 class 路径 中。见 here:

Logback can be configured either programmatically or with a configuration script expressed in XML or Groovy format. By the way, existing log4j users can convert their log4j.properties files to logback.xml using our PropertiesTranslator web-application.

Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  • Logback tries to find a file called logback.groovy in the classpath.

  • If no such file is found, logback tries to find a file called logback-test.xml in the classpath.

  • If no such file is found, it checks for the file logback.xml in the classpath.

  • If no such file is found, and the executing JVM has the ServiceLoader (JDK 6 and above) the ServiceLoader will be used to resolve an implementation of com.qos.logback.classic.spi.Configurator. The first implementation found will be used. See ServiceLoader documentation for more details.

  • If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

我觉得你的配置文件 不在 class 路径 中。一般大部分框架默认config目录都不在class路径下,很多情况下是/config/<files>在class路径下,必须用[=13]指定=] 用 ClassLoader 加载它们时。然而,Logback 不会这样做,所以如果你想将文件存储在 config 目录中,你必须手动加载它们。

本质上,你可以看出JoranConfigurator to load the file from a custom location in your classpath, handle the errors, and so forth to make sure you've found your file. See here for more details.

这是我加载 Logback 配置的方式,您可以根据您的系统调整它。在这种情况下,resource 是 class 路径中的路径,指向您决定放置 logback.xml 文件的任何位置。在这种情况下,它可能是 /spring-xd/xd/config/logback.xml.

public class LogbackConfigurator {
    private static final Logger LOG =
            LoggerFactory.getLogger(LogbackConfigurator.class);
    
    public static boolean configure(String resource) throws JoranException {
        final InputStream configInputStream = LogbackConfigurator.class.getResourceAsStream(resource);
        final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(loggerContext);
        //  the context was probably already configured by default configuration rules
        loggerContext.reset();
        
        if(configInputStream != null) {
            try {
                configurator.doConfigure(configInputStream);
            } catch (JoranException e) {
                e.printStackTrace();
            }
            StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
            return true;
        } else {
            LOG.error("Unable to find logback file: {}", resource);
            StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
            return false;
        }
    }
}

通常,此方法将作为 main 中的第一行之一调用,类似于:

LogbackConfigurator.configure(path);

一旦这个 class 变成 运行,你的 logback 应该被配置为系统已经能够正常找到配置文件。请注意,如果您不想将位置硬编码到系统中,也可以使用 -Dproperty=valueSystem.getProperties().get(keyname) 动态指定 logback 文件路径的备用位置。

您还可以在 Spring 配置中配置要注入的位置,但我个人不建议这样做,因为您通常希望在 [=46= 之前配置日志记录] 发生注入,因此如果在注入期间发生任何日志记录事件,它们将被适当地记录下来。