0

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 ?

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114

1 Answers1

0

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
jmehrens
  • 10,580
  • 1
  • 38
  • 47
  • Reinitialize logging properties means I have to add all other parameters in existing logging.properties file in this new file along with my ne parameter to reset LogManagerright ? – Yuvraj Kolambe Apr 05 '17 at 05:36
  • when we use this config.file system property does this means that all other config params in existing logging.properties file get ignored ? – Yuvraj Kolambe Apr 05 '17 at 05:40
  • @yuvrajK Right. The system property controls the file input into LogManager.readConfiguration. They may be ignored or not. Order of operations will change the logging config you end up with. The system property runs at JVM boot. – jmehrens Apr 05 '17 at 14:44