daneshk
daneshk

Reputation: 186

How to use System Propery values as a part of logger handler pattern

I'm using java.util.logging.logger as the logging engine for my application. I want to set a system property as a part of the value of handler pattern which is mentioned in the logging.properties file like below,

java.util.logging.FileHandler.pattern = path/${custom.home}/logs/server.log

I tried setting the above value, but it doesn't resolve custom.home property instead use that as a string and give below error while initializing the handler

Can't load log handler "java.util.logging.FileHandler"
java.nio.file.NoSuchFileException: path/${custom.home}/logs/server.log.lck
java.nio.file.NoSuchFileException: path/${custom.home}/logs/server.log.lck
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.newFileChannel(UnixFileSystemProvider.java:177)
    at java.nio.channels.FileChannel.open(FileChannel.java:287)
    at java.nio.channels.FileChannel.open(FileChannel.java:335)
    at java.util.logging.FileHandler.openFiles(FileHandler.java:459)
    at java.util.logging.FileHandler.<init>(FileHandler.java:263)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.Class.newInstance(Class.java:442)
    at java.util.logging.LogManager$5.run(LogManager.java:966)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.util.logging.LogManager.loadLoggerHandlers(LogManager.java:958)
    at java.util.logging.LogManager.initializeGlobalHandlers(LogManager.java:1578)
    at java.util.logging.LogManager.access$1500(LogManager.java:145)
    at java.util.logging.LogManager$RootLogger.accessCheckedHandlers(LogManager.java:1667)
    at java.util.logging.Logger.getHandlers(Logger.java:1777)
    at java.util.logging.Logger.log(Logger.java:735)
    at java.util.logging.Logger.doLog(Logger.java:765)
    at java.util.logging.Logger.log(Logger.java:788)

Is there any way of achieving this requirement?

Thanks Danesh

Upvotes: 1

Views: 1420

Answers (2)

jmehrens
jmehrens

Reputation: 11045

From your question I'm assuming you have manually set a system property called custom_home as a launch argument. You can then subclass the java.util.logging.FileHandler to recognize the new pattern syntax:

public class EnvFileHandler extends FileHandler {

    private static String pattern() throws IOException {
        String prefix = EnvFileHandler.class.getName();
        String v = LogManager.getLogManager().getProperty(prefix +".pattern");
        return v.replace("${custom_home}", System.getProperty("custom_home", "%hjava.log"));
    }

    public EnvFileHandler() throws IOException {
        super(pattern());
    }
}

Then instead of installing the FileHandler, you install the FileHandler subclass using your logging.properties.

.handlers=package.of.EnvFileHandler
package.of.EnvFileHandler.pattern=path/${custom_home}/logs/server.log

If you have not set this custom property this code example will default to the home directory.

Upvotes: 1

Niton
Niton

Reputation: 306

The path path/${custom.home}/logs/server.log doesnt exists! Use:

`java.util.logging.FileHandler.pattern = "path/"+`System.getProperty("user.home")+"/logs/server.log";

instead. But eclipse says : "FileHander.pattern is not visible!"

Upvotes: 1

Related Questions