2012-10-25 48 views
8

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

Logback documentation seems to provide a solution但我需要的是如何做出了榜样它的工作:

指定默认的配置文件的位置作为一个系统 财产

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

的java -Dlogback.configurationFile = /路径/到/ config.xml中 chapters.configuration.MyApp1

任何人都可以指向我其中logback.configurationFile被定义为在类路径作为资源的示例反对文件系统?

+0

如果它只是您想更改的日志文件,为什么不在一个logback.xml配置文件中将日志文件定义为变量。如果大多数日志记录配置相同,这一点特别好。然后日志文件名可以从类路径中的资源中读取,或者成为安装程序调用的一部分。 – andygavin

回答

10

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

+0

我认为这样会起作用,但出于好奇,你会如何为类路径下根目录下的某个地方做这件事? –

+1

不,它不起作用 –

+1

我其实现在试过了,它确实有效。你能再次检查你的设置吗? –

1

回答这个问题是在所提供的链接: 与下面的例子:

包括可以作为一个文件引用,作为一种资源,或作为一个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"/> 
相关问题