views:

140

answers:

1

For my java project, i am using the java logging api. I want to log everything using a property file.

Before using this file (log.properties), I configured my onwn formatter in the java code. (see below)

Now I want to configure my own fomatter in the propertie file, instead of the java code. does someone know how to do that ?

Formatter formatter = new Formatter() {

                @Override
                public String format(LogRecord arg0) {
                    StringBuilder b = new StringBuilder();
                    b.append(new Date());
                    b.append(" ");
                    b.append(arg0.getSourceClassName());
                    b.append(" ");
                    b.append(arg0.getSourceMethodName());
                    b.append(" ");
                    b.append(arg0.getLevel());
                    b.append(" ");
                    b.append(arg0.getMessage());
                    b.append(System.getProperty("line.separator"));
                    return b.toString();
                }

            };

fomatter in the java code

..... .....

java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.FileHandler.level=WARNING

java.util.logging.??? = how can i configure my own formater in the property file with this information: data, clasename, methodename, level .etc.**

formatter in de log.proprties

A: 

Put the properties you want in the properties file.

In your Formatter call

LogManager theManager = LogManager.getLogManager();
String value = theManager.getProperty("propName");
// do whatever with value
Romain Hippeau