152

At the moment a default entry looks something like this:

Oct 12, 2008 9:45:18 AM myClassInfoHere
INFO: MyLogMessageHere

How do I get it to do this?

Oct 12, 2008 9:45:18 AM myClassInfoHere - INFO: MyLogMessageHere

Clarification I'm using java.util.logging

1
  • I landed here because of adding Tomcat server to Eclipse. It seems Tomcat ignores the logging.properties so all answers below, except setting VM argument, failed. Actually this problem is answered here: stackoverflow.com/questions/44198899/… . Hope it helps someone :-) Apr 4, 2022 at 19:24

10 Answers 10

94

As of Java 7, java.util.logging.SimpleFormatter supports getting its format from a system property, so adding something like this to the JVM command line will cause it to print on one line:

-Djava.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'

Alternatively, you can also add this to your logger.properties:

java.util.logging.SimpleFormatter.format='%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n'
8
  • 4
    True as of Java7. There is no mention of this in the Java6 docs.
    – jbruni
    Jun 11, 2012 at 22:51
  • 1
    Also in IDEA, works with double quotes. @jbruni This does not work in Java6. Jul 26, 2013 at 15:15
  • 1
    Works with single quotes in Eclipse for me.
    – rich
    Feb 14, 2014 at 15:22
  • 1
    Instead of %1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS you can write %1$tF %1$tT . Makes it a little bit shorter. I don't know if it's even faster. Dec 28, 2014 at 8:47
  • 1
    ... or in logging.properties, with adaptation based on the suggestion by @BrunoEberhard and a shortened logger name: java.util.logging.SimpleFormatter.format=%1$tF %1$tT %4$.1s %2$s %5$s%6$s%n
    – Kariem
    Apr 20, 2015 at 9:27
79

Similar to Tervor, But I like to change the property on runtime.

Note that this need to be set before the first SimpleFormatter is created - as was written in the comments.

    System.setProperty("java.util.logging.SimpleFormatter.format", 
            "%1$tF %1$tT %4$s %2$s %5$s%6$s%n");
3
  • 7
    Note that this need to be set before the first SimpleFormatter is created - for example before getting global logger's handlers.
    – Sheepy
    Jul 6, 2016 at 16:43
  • 5
    This is the quickest way to force your logs to write out to a single line without adding any other startup parameters to the JVM. But make sure you set this property before you make your call to 'new SimpleFormatter()' in your code. Aug 2, 2016 at 19:29
  • 1
    FYI this pattern generates messages like this: 2022-03-16 13:40:04 INFORMATION org.apache.coyote.AbstractProtocol start Starting ProtocolHandler ["http-bio-8080"]
    – Grim
    Mar 16, 2022 at 12:40
77

1) -Djava.util.logging.SimpleFormatter.format

Java 7 supports a property with the java.util.Formatter format string syntax.

-Djava.util.logging.SimpleFormatter.format=... 

See here.

My favorite is:

-Djava.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n

which makes output like:

2014-09-02 16:44:57 SEVERE org.jboss.windup.util.ZipUtil unzip: Failed to load: foo.zip

2) Putting it to IDEs

IDEs typically let you set system properties for a project. E.g. in NetBeans, instead of adding -D...=... somewhere, add the property in the action dialog, in a form of java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-... - without any quotes. The IDE should figure out.

3) Putting that to Maven - Surefire

For your convenience, Here is how to put it to Surefire:

<!-- Surefire -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <systemPropertyVariables>
            <!-- Set JUL Formatting -->
            <java.util.logging.SimpleFormatter.format>%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$-6s %2$s %5$s%6$s%n</java.util.logging.SimpleFormatter.format>
        </systemPropertyVariables>
    </configuration>
</plugin>

4) Hand-made

I have a library with few java.util.logging related classes. Amongst them, it's SingleLineFormatter. Downloadable jar here.

public class SingleLineFormatter extends Formatter {

  Date dat = new Date();
  private final static String format = "{0,date} {0,time}";
  private MessageFormat formatter;
  private Object args[] = new Object[1];

  // Line separator string.  This is the value of the line.separator
  // property at the moment that the SimpleFormatter was created.
  //private String lineSeparator = (String) java.security.AccessController.doPrivileged(
  //        new sun.security.action.GetPropertyAction("line.separator"));
  private String lineSeparator = "\n";

  /**
   * Format the given LogRecord.
   * @param record the log record to be formatted.
   * @return a formatted log record
   */
  public synchronized String format(LogRecord record) {

    StringBuilder sb = new StringBuilder();

    // Minimize memory allocations here.
    dat.setTime(record.getMillis());    
    args[0] = dat;


    // Date and time 
    StringBuffer text = new StringBuffer();
    if (formatter == null) {
      formatter = new MessageFormat(format);
    }
    formatter.format(args, text, null);
    sb.append(text);
    sb.append(" ");


    // Class name 
    if (record.getSourceClassName() != null) {
      sb.append(record.getSourceClassName());
    } else {
      sb.append(record.getLoggerName());
    }

    // Method name 
    if (record.getSourceMethodName() != null) {
      sb.append(" ");
      sb.append(record.getSourceMethodName());
    }
    sb.append(" - "); // lineSeparator



    String message = formatMessage(record);

    // Level
    sb.append(record.getLevel().getLocalizedName());
    sb.append(": ");

    // Indent - the more serious, the more indented.
    //sb.append( String.format("% ""s") );
    int iOffset = (1000 - record.getLevel().intValue()) / 100;
    for( int i = 0; i < iOffset;  i++ ){
      sb.append(" ");
    }


    sb.append(message);
    sb.append(lineSeparator);
    if (record.getThrown() != null) {
      try {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        record.getThrown().printStackTrace(pw);
        pw.close();
        sb.append(sw.toString());
      } catch (Exception ex) {
      }
    }
    return sb.toString();
  }
}
9
  • nicer memory consumption than the other solution. Sep 23, 2011 at 0:36
  • I tried this - but it gives me the log in XML - what am I doing wrong - stackoverflow.com/questions/16030578/…
    – user93353
    Apr 16, 2013 at 6:58
  • 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, 2014 at 10:56
  • Thanks for sharing. I shared a Formatter I wrote in response to another question. Its performed alright for me. I like seeing the tricks other developers come up with for making code smaller and faster.
    – Ryan
    Jul 11, 2014 at 17:02
  • 1
    @ondra-Žižka For what its worth I just sent you a patch on your google code repo.
    – Ryan
    Jul 11, 2014 at 17:24
38

Like Obediah Stane said, it's necessary to create your own format method. But I would change a few things:

  • Create a subclass directly derived from Formatter, not from SimpleFormatter. The SimpleFormatter has nothing to add anymore.

  • Be careful with creating a new Date object! You should make sure to represent the date of the LogRecord. When creating a new Date with the default constructor, it will represent the date and time the Formatter processes the LogRecord, not the date that the LogRecord was created.

The following class can be used as formatter in a Handler, which in turn can be added to the Logger. Note that it ignores all class and method information available in the LogRecord.

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

public final class LogFormatter extends Formatter {

    private static final String LINE_SEPARATOR = System.getProperty("line.separator");

    @Override
    public String format(LogRecord record) {
        StringBuilder sb = new StringBuilder();

        sb.append(new Date(record.getMillis()))
            .append(" ")
            .append(record.getLevel().getLocalizedName())
            .append(": ")
            .append(formatMessage(record))
            .append(LINE_SEPARATOR);

        if (record.getThrown() != null) {
            try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                record.getThrown().printStackTrace(pw);
                pw.close();
                sb.append(sw.toString());
            } catch (Exception ex) {
                // ignore
            }
        }

        return sb.toString();
    }
}
2
  • I tried this - but it gives me the log in XML - what am I doing wrong - stackoverflow.com/questions/16030578/…
    – user93353
    Apr 16, 2013 at 6:59
  • 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, 2014 at 10:55
12

This is what I'm using.

public class VerySimpleFormatter extends Formatter {

    private static final String PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    @Override
    public String format(final LogRecord record) {
        return String.format(
                "%1$s %2$-7s %3$s\n",
                new SimpleDateFormat(PATTERN).format(
                        new Date(record.getMillis())),
                record.getLevel().getName(), formatMessage(record));
    }
}

You'll get something like...

2016-08-19T17:43:14.295+09:00 INFO    Hey~
2016-08-19T17:43:16.068+09:00 SEVERE  Seriously?
2016-08-19T17:43:16.068+09:00 WARNING I'm warning you!!!
2
  • How could we have this formatter output exception information, such as its stacktrace?
    – fegemo
    Apr 24, 2020 at 15:42
  • 1
    @fegemo I think you can print the starcktrace like this. ofNullable(record.getThrown()).ifPresent(v -> v.printStackTrace()); just before return the formatted message.
    – Jin Kwon
    Apr 30, 2020 at 3:57
10

Eclipse config

Per screenshot, in Eclipse select "run as" then "Run Configurations..." and add the answer from Trevor Robinson with double quotes instead of quotes. If you miss the double quotes you'll get "could not find or load main class" errors.

2
  • 2
    As per the previous answer, this works in Java 7. Java 6 doesn't have this feature. Jul 26, 2013 at 15:16
  • 1
    Thank you, it had to be this way, the logging.properties does not work for Eclipse console, for some reason! Apr 4, 2022 at 19:04
5

I've figured out a way that works. You can subclass SimpleFormatter and override the format method

    public String format(LogRecord record) {
        return new java.util.Date() + " " + record.getLevel() + " " + record.getMessage() + "\r\n";
    }

A bit surprised at this API I would have thought that more functionality/flexibility would have been provided out of the box

1
  • Anyone should use formatMessage(record) rather than record.getMessage(). Place holders won't changed.
    – Jin Kwon
    Aug 19, 2016 at 8:44
-2

If you log in a web application using tomcat add:

-Djava.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter

On VM arguments

-3

This logging is specific to your application and not a general Java feature. What application(s) are you running?

It might be that this is coming from a specific logging library that you are using within your own code. If so, please post the details of which one you are using.

1
  • 3
    It's not some random logger application. It's java's own java.util.logging.Logger Jan 3, 2014 at 12:52
-3

if you're using java.util.logging, then there is a configuration file that is doing this to log contents (unless you're using programmatic configuration). So, your options are
1) run post -processor that removes the line breaks
2) change the log configuration AND remove the line breaks from it. Restart your application (server) and you should be good.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.