How do I specify a logback configuration for use i

2019-09-10 08:41发布

问题:

I want to add logging to a number of internal Java webstart applications, preferably using the logback logging framework and logback.groovy configuration files. I expect to specify this in the webstart application .jnlp file, but it's not clear how to do this.

I have tried adding the logback.groovy file in the resources of my main jar (i.e. that which contains my main method), but that didn't get picked up by logback.

I have also tried various attempts to add the logback.groovy file to the <resources> section of the .jnlp file:

<jnlp ...>
  <resources>
    <j2se version="$j2seVersion" />
    $dependencies // from maven-webstart-plugin

    // some reference to my logback configuration - e.g. 
    <dir href="log/" />
  </resources>
</jnlp>

It's worth noting that logback expects the configuration files to be referenced on the classpath as the folder in which it resides, not to the file itself - see FAQs.

However, I'm really not sure how this would apply to Java Webstart.

Is this possible? If so, how do I do this?

回答1:

So... the approach that I ended up using for this was (in line with Askel's comments) to put the logback.groovy configuration file into /src/main/resources.

However, we use our Java libraries in a number of different ways, and I didn't want to include the logback configuration file in the jar by default. In part, this is because we have several libraries, and logback doesn't play nicely with multiple configuration files. Also depending on the release, I wanted to use different logback configurations.

For our webstart releases, we use the webstart-maven-plugin to build the library, assemble the dependencies etc. But before doing this build, we copy the logback.groovy configuration file to /src/main/resources - this includes the file in the build for that library. Then, after the build/release, you can remove the configuration file from /src/main/resources.

However, this plugin will also run mvn install, which will put the library with the logback configuration file in your local maven repository. Therefore, after the build/release/temp configuration removal, we do another mvn clean install, which will overwrite that library.