Discussion Locked

Java Logger config file system property

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I have a Java Logger config file where I specify the location of output files as in java.util.logging.FileHandler.pattern = "${APP_LOG}/service%u.log.%g"
This does not work as it does not appear to resolve ${APP_LOG} as a system property. Is there a way specify a variable into the Logger config file?

5 replies

by javed.beg

1. Pass the config file name with the exact path while executing your code (A.java):

java A 'C:\boom.cfg'

In side the code, read this arg

fileName = args[0].trim();

2. Read the properties from the config file
File f = new File(fileName);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
Properties prop = new Properties();
prop.load(bis);

3. Read the required property you would have defined in this file

String appLog = aceProperties.getProperty("APP_LOG").trim();

by sbockelman

Use your build tool/script (e.g., ant) to substitute the real path when the app is deployed.

by mjremijan

Remember, in Java there is no API specification to see system environment variables. To get an environment variable you either need to pass them to the JVM on the command line (java -DAPP_LOG=/var/log/app" or you need to write a piece of wrapper code to call a system-dependent binary or function. So the short answer to your question is no, I don't think so.

by georgehsnyder

Download the API and look at :System.
System.getProperty("APP_LOG") should return the system value stored in APP_LOG.

by xitx

I did it in this way.

I implemented a log configuration which tells us what handlers are associated with this logger.

In the class which is using the logger, just do it:

Logger myLogger = Logger.getLogger("myName");

In the configuration:

String logFile = "${APP_LOG}/service...";
..getLogger("myName").addHandler( new FileHandler( logFile,... ) );

You can call the configuration somewhere in your system

Wish it helps.
Tianxiao

This discussion is closed to new replies.