1

I am trying to enable Amazon Web Services SDK logging by using the java.util.logging framework instead of log4j. I have managed to get it working and the logs go into a file specified by java.util.logging.FileHandler.pattern in the properties file.

My log.properties file

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
handlers=java.util.logging.FileHandler
com.amazonaws.request.level=FINE
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern=./javalog.log

What I am looking for is a way to set the file name at runtime.

I tried the following options

Option 1: In the properties file: Set the following java.util.logging.FileHandler.pattern=${mylogfile}

and then in the main() function of my java program, call System.setProperty("mylogfile", logName);

Option 2: Delete the line "java.util.logging.FileHandler.pattern" from the properties file.

Instead, call System.setProperty("java.util.logging.FileHandler.pattern", logName);

Both these options do not work.

Ps: In case of log4j, option 1 works fine.

Any idea how I can customize the log file for SDK logging dynamically ?

Thanks Vijay

Vijay
  • 13
  • 3

1 Answers1

0

The LogManager supports arbitrary configuration code. Per the documentation:

A property "config". This property is intended to allow arbitrary configuration code to be run. The property defines a whitespace or comma separated list of class names. A new instance will be created for each named class. The default constructor of each class may execute arbitrary code to update the logging configuration, such as setting logger levels, adding handlers, adding filters, etc.

For example you can compile the following:

    package somepackage;
    public class Config {
        public Config() throws Exception {
            String pattern = System.getProperty("mylogfile", "javalog.log");
            FileHandler f = new FileHandler(pattern);
            Logger.getLogger("").addHandler(f);
        }
    }

Then install it using the following config file:

config=somepackage.Config
com.amazonaws.request.level=FINE

You can also create a subclass or proxy handler to control how and when the log file name is generated at runtime.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47