Explain Codes LogoExplain Codes Logo

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

java
logging
formatter
configuration
Alex KataevbyAlex Kataev·Feb 12, 2025
TLDR

Launcher-ready option: Customize a java.util.logging.Formatter and structure the format method to combine log details, bypassing line breaks. Assign this formatter to your desired handlers for neat, one-line logs.

Fork this formatter code:

import java.util.logging.*; public class OneLinerFormatter extends Formatter { @Override public String format(LogRecord rec) { // They said to keep logs on one line. So single line it is! return rec.getLevel() + " " + rec.getMessage() + "\n"; } }

And use it in your logging setup:

Logger logger = Logger.getLogger("compactLogger"); ConsoleHandler ch = new ConsoleHandler(); ch.setFormatter(new OneLinerFormatter()); // Here you go, single-line logs! logger.addHandler(ch); logger.info("Single-line log entry!"); // logging masterpiece!

Built-in SimpleFormatter Shortcut

Want to configure SimpleFormatter for a single line output? The key is manipulating java.util.logging.SimpleFormatter.format property, making SimpleFormatter use your custom pattern and thereby condensing entire log entry in one line.

Raring to go on command-line? Use this:

-Djava.util.logging.SimpleFormatter.format="%1$tF %1$tT %4$s %2$s %5$s%6$s%n"

Here's a deconstructed view of placeholders:

  • %1$tF %1$tT - The moment of truth (Time of logging)
  • %4$s - You can't handle the truth (Logging level)
  • %2$s - Deep throat (Source of the logging)
  • %5$s%6$s - Inside Man and his accomplice (Log message and thrown exception)

Ensure this is whisked before any logger wakes up in your application, property must be set at sunrise (startup).

Step Up Your Configuration Game

Project-wide Unity via logger.properties

For a global village (project-wide) setting, declare your asset (property) in the logger.properties file:

java.util.logging.SimpleFormatter.format=%1$tF %1$tT %4$s %2$s %5$s%6$s%n

This holds the reigns of consistent logging format across your entire Java land.

Tweaking Runtime Actions

Need to steer the logging format at runtime? Switch your strategy using System.setProperty:

System.setProperty("java.util.logging.SimpleFormatter.format", "%1$tF %1$tT %4$s %2$s %5$s%6$s%n");

Don't forget, it's a before and after scenario. Do it before the loggers get to work to make sure the format clicks.

Riding the Custom Formatters Wave

Classy with MessageFormat and Formatter

Having complex dilemmas? Get in good graces of MessageFormat and Formatter to yield tailored logging outputs. Perfect your format strings to echo specific needs or sync with common standards like JSON.

Meet the SingleLineFormatter class

Having special occasion? We've got you SingleLineFormatter class:

import java.util.logging.*; import java.util.*; public class SingleLineFormatter extends SimpleFormatter { private static final String pattern = "%1$tF %1$tT %4$s %2$s %5$s%6$s%n"; @Override public synchronized String format(LogRecord lr) { return String.format(pattern, new Date(lr.getMillis()), // Rock around the clock! lr.getSourceClassName() + "." + lr.getSourceMethodName(), // Here's where it's happening! lr.getLoggerName(), // Who was it, again?! lr.getLevel().getLocalizedName(), // Let's see how deep the rabbit hole goes... lr.getMessage(), // Are you not entertained?! Optional.ofNullable(lr.getThrown()).map(Throwable::toString).orElse("")); // Oops! Here's the blooper! } }

Slide this in your logging setup to relish the tailored experience.