Java creating logfile java0.log
and lock file in /root
folder
This behavior can be disabled using LogManager.reset()
function, but I want to call this function based on some configuration parameter.
Can anyone suggest which config file I can use to do this and how to achieve this ?
Use the java.util.logging.config.file system property and point it to the NUL device .
On Windows:
-Djava.util.logging.config.file=NUL
On Linux:
-Djava.util.logging.config.file=/dev/null
On any platform you can simply create an empty file and point to it.
What this does is on startup the LogManager.readConfiguration() is called with the given file path (stream). Per the contract this method will:
Reinitialize the logging properties and reread the logging configuration
When it reads the logging configuration there is no new data to read. Therefore, this behaves as if it was just calling LogManager.reset() on startup.
If you want more selective behavior on the root logger then you write a custom class and use the java.util.logging.config.class
property:
package foo.bar;
import java.io.FileInputStream;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public final class LogConfig {
public LogConfig() throws Throwable {
//Load the default config file.
String file = System.getProperty("java.util.logging.config.file");
if (file != null) {
LogManager.getLogManager().readConfiguration(new FileInputStream(file));
}
//Modify the logger tree if property is set.
String key = "some.config.property";
String v = System.getProperty(key);
if (v == null) {
throw new NullPointerException("System property '"
+ key + "' was not defined");
}
if (Boolean.parseBoolean(v)) {
Logger root = Logger.getLogger("");
for (Handler h : root.getHandlers()) {
if (h instanceof FileHandler) {
root.removeHandler(h);
h.close();
}
}
}
}
}
The to use this class you set the following command line properties:
-Djava.util.logging.config.file=/path/to/file -Djava.util.logging.config.class=foo.bar.LogConfig -Dsome.config.property=true
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.