4

I am using java.util.logging for logging (I don't want to use log4j or anything else).

This is my complete private logging.properties:

handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.pattern = my.log
java.util.logging.FileHandler.limit = 500000
java.util.logging.FileHandler.count = 40
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

This is the code in my program:

public static Logger log = Logger.getLogger(MyClass.class.getName());
// Is there anything else to be init'ed here? 
// I don't. I just start using log directly in the code.

log.severe("something");
log.info("something else");

Since this gives each log message on 2 lines, I tried this

How do I get java logging output to appear on a single line?

Copied the LogFormatter class in the first reply exactly.

Changed one line in my logging.properties

java.util.logging.FileHandler.formatter = com.mycomp.myproj.LogFormatter;

Now my log has started appearing in XML. I have a strong feeling that the FileHandler doesn't like my com.mycomp.myproj.LogFormatter and hence defaulting to the default XMLFormatter. How do I figure out why FileHandler isn't using my LogFormatter class?

Community
  • 1
  • 1
user93353
  • 13,733
  • 8
  • 60
  • 122

6 Answers6

5

How do I figure out why FileHandler isn't using my LogFormatter class?

Start the program in a debugger and step through the code. Set a breakpoint in LogManager.getLogger() and/or LogManager.readConfiguration()

Or forget about java.util.logging and use a framework like logback that is easy to configure and gives you useful error messages when something is wrong.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
5

You can set the formatter in the code itself on the FileHandler instance.

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

// please change name of your own choice
Logger log = Logger.getLogger("CustomLogger"); 
log.setUseParentHandlers(false);
log.setLevel(Level.ALL);

FileHandler handler = new FileHandler("[log_file_location]");
handler.setFormatter(new CustomFormatter()); // set formatter
log.addHandler(handler);

log.info("test message");

handler.close(); // close the handler at some later point in your application.

The CustomFormatter class is defined as follows.

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class CustomFormatter extends Formatter {

    @Override
    public String format(LogRecord record) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(record.getMessage());
        return buffer.toString();
    }

}

You can code in CustomFormatter to output the messages in any format you want. Hope this helps.

  • I'll try this - but my question is more about why the existing setup isn't working. What am I doing wrong? – user93353 Apr 19 '13 at 02:03
  • `log.setLevel(Level.ALL);` - is this necessary - I want to use whatever Level is specified in my logging.properties file? – user93353 Apr 19 '13 at 05:48
  • Also the log_file_location - I want to use the location specified in logging.properties. – user93353 Apr 19 '13 at 05:59
  • Tried this out - everything works without the setLevel and log_file_location. Upvoted. However, still trying to figure out why setting the handler in logging.properties doesn't work. – user93353 Apr 19 '13 at 12:30
  • When specifying an external properties files you have to specify it when running the program E.g java -Djava.util.logging.config.file=C:\logger.properties myProgram.class –  Apr 19 '13 at 23:34
  • The parameter -Djava.util.logging.config.file points to the properties file path on your machine. The above is a comment only, to use the appropriate log level remove the call in code so the one in the properties file can be applied. –  Apr 19 '13 at 23:35
  • if it turns XML instead of the format you expected - that means the setting in your properties file did not found your formatter class, you can do 2 things: 1. make sure your class is in a package and set the formatter property to that class and package 2. make sure your class has a unique name like "MyFormatter", last: make sure all handlers have the formatter you wanted (java.util.logging.ConsoleHandler.formatter=my.package.MyFormatter and also: java.util.logging.FileHandler.formatter=my.package.MyFormatter) – Shaybc Mar 29 '14 at 10:57
2

Ok this one really annoyed me too, and was driving me nuts, I was checking the spelling endlessly, doing google searching etc - which is how I got here - I know this is a year old, but your going to kick yourself. Here is what I am pretty sure is your answer.

For :

java.util.logging.FileHandler.formatter = com.mycomp.myproj.LogFormatter;

That should be :

java.util.logging.FileHandler.formatter = com.mycomp.myproj.LogFormatter

Drop the training ";". It seems to need to match exactly to the class name I saw someone else complain they had trailing white space characters - which was what my problem was. A better error message would have been helpful.

Cheers - Mark

Mark O'Donohue
  • 681
  • 5
  • 4
1

com.mycomp.myproj.LogFormatter is not in your system class path.

LogManager will do the following call for loading your class:

Formatter getFormatterProperty(String name, Formatter defaultValue) {
        String val = getProperty(name);
        try {
            if (val != null) {
                Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(val);
                return (Formatter) clz.newInstance();
            }
        } catch (Exception ex) {
            // We got one of a variety of exceptions in creating the
            // class or creating an instance.
            // Drop through.
        }
        // We got an exception.  Return the defaultValue.
        return defaultValue;
}

You need to make sure your class LogFormatter is in the system's class path.

Dirk R
  • 357
  • 2
  • 13
0

I found that unless you set the format via the config, it does not apply the formatter. I tested this on JDK 11.

handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.pattern = my.log
java.util.logging.FileHandler.limit = 500000
java.util.logging.FileHandler.count = 40
java.util.logging.FileHandler.formatter = com.mycomp.myproj.LogFormatter
com.mycomp.myproj.LogFormatter.format = %n

i.e. add the last line com.mycomp.myproj.LogFormatter.format = %n

mrswadge
  • 1,659
  • 1
  • 20
  • 43
0

May be this is not relevant at this point but recently I have found one issue where $ in the format caused me a problem and it could not parse and fallen back to default format. Originally I had

[%1$tF %1$tT] [%4$-7s] %5$s %n

in logging.properties file which did not work as expected but

[%1\$tF %1\$tT] [%4\$-7s] %5\$s %n

this worked perfectly with \$. May be it will be helpful for others in future.

RIPAN
  • 3,326
  • 4
  • 17
  • 28