How to use a logging.properties file from disk as a java.util.logging.properties file

StackOverflow https://stackoverflow.com/questions/17601690

  •  02-06-2022
  •  | 
  •  

Domanda

I have a basic cmd program that calls a jar which has an Install class with the main method. The Install class uses a java.util.logging.config.file from its resources folder.

private static void setupLogging() {
    if (System.getProperty("java.util.logging.config.file") == null) {
        final InputStream inputStream = Install.class.getResourceAsStream("/logging.properties");
        try {
            LogManager.getLogManager().readConfiguration(inputStream);
        } catch (final IOException e) {
            Logger.getAnonymousLogger().severe("Could not load default logging.properties file");
            Logger.getAnonymousLogger().severe(e.getMessage());
        }
    }
}

This install program belongs to a different project and I shouldn't change it. But I want to use a seperate logging.properties file from my disk (preferably from the same folder where I have the cmd program). How do I force this install program to use my logging.properties file instead of it's default one from the resources folder? Is there a way I can force this?

È stato utile?

Soluzione

You could try this:

String yourFilePath = "./yourLogProperties.properties";
LogManager.getLogManager().readConfiguration(new FileInputStream(yourFilePath);

Properties will be loaded from a file stored in the same folder which your app is executed.

Update:

I was searching a little bit how to pass properties thru command line and there's a pretty good explanation here. So, in your case you'll need something like this:

java -jar -Djava.util.logging.config.file="./yourLogProperties.properties" YourApp.jar

Also you can test if it works making a simple test:

public class Tests {    
    public static void main(String[] args) {
        System.out.println(System.getProperty("java.util.logging.config.file"));
    }    
}

Running this from command line you should get the answer ./yourLogProperties.properties

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top