简体   繁体   English

Spring-XD不读取logback.xml

[英]Spring-XD does not read logback.xml

I am trying to run a job in Spring-XD, located under the following path: 我正在尝试在Spring-XD中运行一个位于以下路径下的工作:

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

My jar, located under MyJobName/lib , contains in its root path the file logback.xml. 我的jar位于MyJobName/lib ,在其根路径中包含文件logback.xml。 Unfortunately, Spring-XD seems to be completely ignoring that file. 不幸的是,Spring-XD似乎完全无视该文件。 When I run the job through my IDE (IntelliJ), the logging works fine, but when I run it using Spring-XD it completely ignores my SiftingAppender. 当我通过我的IDE(IntelliJ)运行作业时,日志记录工作正常,但是当我使用Spring-XD运行它时,它完全忽略了我的SiftingAppender。

Here is what my logback.xml file looks like: 这是我的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>

I want to put this logback.xml file under /spring-xd/xd/config, or under another configuration folder, but nothing I try works. 我想把这个logback.xml文件放在/ spring-xd / xd / config下,或者放在另一个配置文件夹下,但是我尝试的都没有。 I tried looking through the Spring-XD docs, but found nothing. 我尝试查看Spring-XD文档,但一无所获。

Any insight would be appreciated. 任何见解将不胜感激。

You have to put Logback directly in the classpath . 您必须将Logback 直接放在类路径中 See here : 看到这里

Logback can be configured either programmatically or with a configuration script expressed in XML or Groovy format. 可以通过编程方式或使用以XML或Groovy格式表示的配置脚本来配置Logback。 By the way, existing log4j users can convert their log4j.properties files to logback.xml using our PropertiesTranslator web-application. 顺便说一句,现有的log4j用户可以使用我们的PropertiesTranslator Web应用程序将他们的log4j.properties文件转换为logback.xml。

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. Logback尝试在类路径中查找名为logback.groovy的文件。

  • If no such file is found, logback tries to find a file called logback-test.xml in the classpath. 如果未找到此类文件,则logback会尝试在类路径中查找名为logback-test.xml的文件。

  • If no such file is found, it checks for the file logback.xml in the classpath. 如果未找到此类文件,则会检查类路径中的文件logback.xml。

  • 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. 如果未找到此类文件,并且正在执行的JVM具有ServiceLoader(JDK 6及更高版本),则将使用ServiceLoader来解析com.qos.logback.classic.spi.Configurator的实现。 The first implementation found will be used. 将使用找到的第一个实现。 See ServiceLoader documentation for more details. 有关更多详细信息,请参阅ServiceLoader文档。

  • If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console. 如果以上都不成功,则logback将使用BasicConfigurator自动配置自身,这将导致日志记录输出定向到控制台。

It sounds to me like your configuration file is not in the classpath . 听起来我的配置文件不在类路径中 Generally the config directory is not in the classpath by default in most frameworks, in many cases it's /config/<files> that are in the class path, and you have to specify them with /config/name when loading them with the ClassLoader . 通常,在大多数框架中,配置目录默认不在类路径中,在很多情况下,它是类路径中的/config/<files> ,并且在使用ClassLoader加载它们时必须使用/config/name指定它们。 However, Logback doesn't do that, so if you want to store the files in the config directory, you have to load them manually. 但是,Logback不会这样做,所以如果要将文件存储在config目录中,则必须手动加载它们。

Essentially, you can tell 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. 基本上,您可以告诉JoranConfigurator从类路径中的自定义位置加载文件,处理错误等等,以确保您找到了您的文件。 See here for more details. 有关详细信息,请参见此处

Here is how I load my Logback config, you can probably adapt this to your system. 以下是我加载Logback配置的方法,您可以将其调整到您的系统中。 In this case resource is the path within your classpath to wherever you've decided to put your logback.xml file. 在这种情况下, resource是您的类路径中的路径,无论您决定放置logback.xml文件。 In this case it would probably be /spring-xd/xd/config/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;
        }
    }
}

Typically, this method would be called as one of the first lines in main with something like: 通常,此方法将被称为main中的第一行,其中包括:

LogbackConfigurator.configure(path);

Once this class has been run, your logback should be configured as if the system had been able to find the configuration file normally. 运行此类后,应配置您的logback,就好像系统能够正常查找配置文件一样。 Note that you can also use -Dproperty=value and System.getProperties().get(keyname) to specify an alternate location for the path to the logback file dynamically if you don't want to hardcode the location into your system. 请注意,如果您不想将位置硬编码到系统中,还可以使用-Dproperty=valueSystem.getProperties().get(keyname)指定备用文件路径的备用位置。

You can also configure the location to be injected in your Spring configuration, but I personally don't recommend that as you would normally like logging to be configured before injection occurs, so that if any logging events occur during injection, they will be logged appropriately. 您还可以在Spring配置中配置要注入的位置,但我个人不建议您注入之前通常需要配置日志记录,这样如果在注入期间发生任何日志记录事件,它们将被适当地记录。

Seems like your logback configuration is having some minor issues. 似乎您的logback配置存在一些小问题。

Try putting the below configuration into your application classpath; 尝试将以下配置放入应用程序类路径中; should resolve the problem. 应该解决问题。

<?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>

Note below part is changed according to the logback configuration reference: 注意下面的部分根据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>

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

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