java.util.logging: how to suppress date line
Asked Answered
B

3

22

I'm trying to suppress output of the date line durinng logging when using the default logger in java.util.logging. For example, here is a typical output:

Jun 1, 2010 10:18:12 AM gamma.utility.application info

INFO: ping: db-time=2010-06-01 10:18:12.0, local-time=20100601t101812, duration=180000
Jun 1, 2010 10:21:12 AM gamma.utility.application info
INFO: ping: db-time=2010-06-01 10:21:12.0, local-time=20100601t102112, duration=180000

I would like to get rid of the Jun 1, 2010... lines, they just clutter my log output. How can I do this?

But answered 1/6, 2010 at 14:42 Comment(4)
The output format depends on the Formatter being used. java.sun.com/j2se/1.4.2/docs/api/java/util/logging/…. Can you specify what is yours and publish its configuration?Gebler
Here is an example on how to implement a custom formatterPaean
Anton, I use java.util.logging.SimpleFormatter in jdk 1.6. I tried creating a simple formatter and overriding its format(LogRecord) method but that didn't help.But
Bozho, where do I find a .properties file? What should it contain that is of relevance to logging?But
B
15

The problem is caused by a handler in the parent log. The solution is to remove all handlers from the parent log, and then add own custom handler. This code removes handlers from the parent log:

      for(Handler iHandler:log.getParent().getHandlers())
        {
        log.getParent().removeHandler(iHandler);
        }
But answered 3/6, 2010 at 6:32 Comment(1)
instead log.setUseParentHandlers(false); can also be usedNettles
S
33

From Java SE 7 there is a new system property: java.util.logging.SimpleFormatter.format.

The same property is also configurable on the java.util.logging properties file (logging.properties). If you are an Eclipse user, and you are annoyed by the double line message in the console output, you could change the jre logging.properties file (JDK_HOME/jre/lib/logging.properties) in this way:

java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n

Some example format is available here: http://docs.oracle.com/javase/7/docs/api/index.html?java/util/logging/SimpleFormatter.html.

Selfeffacing answered 23/5, 2012 at 14:39 Comment(2)
You just saved our day, thanks mate. For the record, if you're using Tomcat, you can also change this under /etc/tomcat/logging.properties, you don't need to change the global Java settings.Follower
If you want to do this programmatically, see answer here: #195265Heliozoan
B
15

The problem is caused by a handler in the parent log. The solution is to remove all handlers from the parent log, and then add own custom handler. This code removes handlers from the parent log:

      for(Handler iHandler:log.getParent().getHandlers())
        {
        log.getParent().removeHandler(iHandler);
        }
But answered 3/6, 2010 at 6:32 Comment(1)
instead log.setUseParentHandlers(false); can also be usedNettles
S
6

Write a custom formatter extending java.util.logging.Formatter class and implement the String format(LogRecord) method according to your needs. For example, the following formatter shows only the log message (and the throwable stacktrace if an exception is being logged):

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

class CustomRecordFormatter extends Formatter {
    @Override
    public String format(final LogRecord r) {
        StringBuilder sb = new StringBuilder();
        sb.append(formatMessage(r)).append(System.getProperty("line.separator"));
        if (null != r.getThrown()) {
            sb.append("Throwable occurred: "); //$NON-NLS-1$
            Throwable t = r.getThrown();
            PrintWriter pw = null;
            try {
                StringWriter sw = new StringWriter();
                pw = new PrintWriter(sw);
                t.printStackTrace(pw);
                sb.append(sw.toString());
            } finally {
                if (pw != null) {
                    try {
                        pw.close();
                    } catch (Exception e) {
                        // ignore
                    }
                }
            }
        }
        return sb.toString();
    }
}

This is how you use it:

import java.util.logging.ConsoleHandler;
import java.util.logging.Logger;

class A {
    private static final Logger LOGGER = Logger.getLogger(A.class.getName());

    static {
        CustomRecordFormatter formatter = new CustomRecordFormatter();
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setFormatter(formatter);
        LOGGER.addHandler(consoleHandler);
    }

    public void doSomething() {
        LOGGER.info("something happened");
    }
}
Sporocyte answered 1/6, 2010 at 16:47 Comment(3)
I'll give this a try, but when I tried something similar last time it didn't work.But
Need to follow andrew's answer below in addition to this :)Malt
Formatter is final.Barleycorn

© 2022 - 2024 — McMap. All rights reserved.