如何将logback配置指定为类路径资源而不是文件

时间:2012-10-25 13:59:13

标签: logback

我有以下情况。我需要能够运行由不同批处理文件启动的两个程序,其中每个批处理文件从同一个jar调用带有main()的java类。我希望每个程序都有自己的日志。但是,第二个程序是第一个程序的安装程序,因此,我不希望/不能轻易指定-Dlogback.configurationFile = / path / to / config文件,因为该位置可能尚不存在。

Logback documentation seems to provide a solution但我需要一个如何让它发挥作用的例子:

  

指定默认配置文件作为系统的位置   属性

     

如果您愿意,可以指定默认配置的位置   具有名为logback.configurationFile的系统属性的文件。价值   此属性的属性可以是URL,类路径上的资源或路径   到应用程序外部的文件。

     

java -Dlogback.configurationFile = / path / to / config.xml   chapters.configuration.MyApp1

有人能指出一个例子,其中logback.configurationFile被定义为类路径上的资源而不是文件系统吗?

4 个答案:

答案 0 :(得分:10)

您只需将my-logback.xml放在其中一个类路径条目的根目录中,然后指定-Dlogback.configurationFile=my-logback.xml即可。在内部,它可能会使用ClassLoader#getResource(String name)来获取文件 - 请查看此方法的JavaDoc以获取更多信息。

答案 1 :(得分:1)

这个问题的答案在提供的链接中: https://logback.qos.ch/manual/configuration.html#configFileProperty 以下示例:

要包含的内容可以作为文件,资源或URL引用。

As a file:
To include a file use the file attribute. You can use relative paths but note that the current directory is defined by the application and is not necessarily related to the path of the configuration file.

As a resource:
To include a resource, i.e a file found on the class path, use the resource attribute.

<include resource="includedConfig.xml"/>

As a URL:
To include the contents of a URL use the url attribute.

<include url="http://some.host.com/includedConfig.xml"/>

答案 2 :(得分:0)

我提供了一个略微不同的解决方案,而不是提供命令行参数。

我的项目设置如下:

| foo
  - src
    - conf
      - logback.xml
    - main
    - test

我想要jar之外的logback.xml,因为我设置了scanPeriod实时重新加载。我想,因为它应该在类路径中被拾取,我将添加到manifest.mf的路径并且它会被自动拾取。

foo&#39; s pom.xml的相关部分如下所示:

<plugins>
  . . . Some plugins before
  <plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
      <execution>
        <id>copy-resources</id>
        <phase>validate</phase>
        <goals>
          <goal>copy-resources</goal>
        </goals>
        <configuration>
          <outputDirectory>${basedir}/target/conf</outputDirectory>
          <resources>
            <resource>
              <directory>src/conf</directory>
            </resource>
          </resources>
        </configuration>
      </execution>
    </executions>
  </plugin>

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
      <archive>
        <manifest>
          <addClasspath>true</addClasspath>
          <classpathPrefix>lib/</classpathPrefix>
          <mainClass>com.company.foo.main.Main</mainClass>
        </manifest>
        <manifestEntries>
          <version>${project.version}</version>
          <Class-Path>.</Class-Path>
          <Class-Path>conf/</Class-Path>
        </manifestEntries>
      </archive>
    </configuration>
  </plugin>
  . . . Some plugins after
</plugins>

需要注意的重要事项:

  • Maven Resources插件
    • src/conf中的所有文件复制到标准编译文件夹target/conf
  • Maven Jar插件
    • conf文件夹添加到MANIFEST.MF的类路径中,这样您就不需要在命令行中指定位置。
    • conf文件夹中的所有文件将添加到类路径中,而不仅仅是logback.xml

要在此方案中运行jar,{jar}必须将conf文件夹放在旁边。它看起来像这样:

path/to/jar
  | foo.jar
  | lib/
  | conf/
    | logback.xml

MANIFEST.MF看起来像这样:

Manifest-Version: 1.0
Built-By: $user
Class-Path: lib/x.jar lib/y.jar lib/z.jar conf/
version: 0.0.1-SNAPSHOT
Created-By: Apache Maven 3.5.2
Build-Jdk: 1.8.0_161
Main-Class: com.company.foo.main.Main

答案 3 :(得分:0)

好的,我继续尝试。

如果logback配置文件在jar所在的本地(在您正在运行的同一个jar中),则此方法有效:

System.setProperty("logback.configurationFile", "org/package/path/your.logback.xml");

有效。如果在类路径“任何地方”。这真的很奇怪,因为通常在调用getResource时必须用前面的斜杠指定它,例如:

YourClass.class.getResource("/org/package/path/your.logback.xml"),所以不确定为什么。

其他更奇特的资源路径(如指定确切的jar)似乎也不起作用。很奇怪。