简体   繁体   中英

Can't control logging levels when using jetty-runner

I have a Java 8 Maven webapp project that I am running using jetty-runner.jar . Everything works fine, except I can't control the logging levels (INFO, WARNING, FINE, FINER etc.). Am using java.util.logging and OS is Win7.

I've tried the following:

  • Created a logging.properties file in src/main/resources folder (ends up in WEB-INF/classes directory of the war ).
  • Renamed logging.properties to jetty-logging.properties
  • Used -Djava.util.logging.config.file=WEB-INF/classes/logging.properties (when the file was so named) with the command to run jetty-runner
    • java -Djava.util.logging.config.file=WEB-INF/classes/logging.prop erties -jar target/dependency/jetty-runner.jar target/xyz.war
  • Also tried -Djava.util.logging.config.file=/WEB-INF/classes/logging.properties (with a / in front of WEB-INF )

My logging file is simply:

.level  = WARNING

I haven't had to mess around with logging much before (except GAE projects), so not sure what I am doing wrong.

Since you are trying to load the logging.properties from inside of the WAR file you have to use the java.util.logging.config.class . Per the documentation:

If the "java.util.logging.config.class" property is set, then the property value is treated as a class name. The given class will be loaded, an object will be instantiated, and that object's constructor is responsible for reading in the initial configuration. (That object may use other system properties to control its configuration.) The alternate configuration class can use readConfiguration(InputStream) to define properties in the LogManager.

Using that property you can use Class.getResourceAsStream to locate the file inside of the WAR file. Here is an example:

package foo.bar.baz;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;

public class JulConfig {

    /**
     * Install this as the -Djava.util.logging.config.class=foo.bar.baz.JulConfig
     * -Djava.util.logging.config.file=WEB-INF/classes/logging.properties
     * @throws Exception if there is a problem.
     */
    public JulConfig() throws Exception {
        String key = "java.util.logging.config.file";
        String file = System.getProperty(key, "logging.properties");
        final InputStream in = JulConfig.class.getResourceAsStream(file);
        if (in != null) {
            try {
                LogManager.getLogManager().readConfiguration(in);
                //System.clearProperty(key); //Optional.
            } finally {
                try {
                    in.close();
                } catch (IOException ignore) {
                }
            }
        } else {
            throw new FileNotFoundException(file);
        }
    }
}

The reason you have to do this is because the LogManager uses java.io.File to locate the logging.properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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