Logback
JsonLayout
logging
Java
troubleshooting

Logback JsonLayout printing all logs on the same line

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When Logback JSON output appears on one long line, the issue is usually layout configuration, not logger behavior. JSON log events are often emitted as compact single-line objects by design for machine parsing. The fix depends on whether you need one-line-per-event output, pretty-printed multiline JSON, or proper line separators in file appenders.

Why This Happens

Most JSON layouts optimize for ingestion by log processors such as Fluent Bit, Logstash, or Cloud logging agents. Compact output is valid JSON and usually desirable in production. Problems appear when:

  • no line separator is written between events
  • pretty printing is enabled unexpectedly
  • output viewer wraps lines in a misleading way

Distinguish these cases before changing configuration.

Baseline Logback JSON Appender

A common setup using logback-contrib layout:

xml
1<configuration>
2  <appender name="JSON_FILE" class="ch.qos.logback.core.FileAppender">
3    <file>logs/app.json</file>
4    <append>true</append>
5
6    <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
7      <appendLineSeparator>true</appendLineSeparator>
8      <timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSZ</timestampFormat>
9      <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter"/>
10    </layout>
11  </appender>
12
13  <root level="INFO">
14    <appender-ref ref="JSON_FILE"/>
15  </root>
16</configuration>

appendLineSeparator is the key option when events are concatenated into a single physical line.

Alternative With Logstash Encoder

Many teams use net.logstash.logback instead of raw layout classes.

xml
1<configuration>
2  <appender name="JSON_STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
4      <timeZone>UTC</timeZone>
5    </encoder>
6  </appender>
7
8  <root level="INFO">
9    <appender-ref ref="JSON_STDOUT"/>
10  </root>
11</configuration>

This encoder writes one JSON object per line, which is ideal for line-oriented collectors.

Verify Output Correctly

Some terminals and editors wrap display in ways that look like one line. Validate with command-line checks:

bash
wc -l logs/app.json
head -n 3 logs/app.json

If line count increases with requests, separators are present even if your viewer wraps visually.

Pretty Printing Tradeoff

If you want human-readable multiline JSON in local development, configure pretty printing in formatter settings. Keep production logs compact to reduce size and preserve one-event-per-line ingestion. Mixed formatting across environments causes parsing inconsistencies.

A practical pattern:

  • production: compact JSON with line separator
  • local debug: optional pretty JSON in console only

File and Collector Compatibility

Different collectors have different assumptions. Some treat each line as one event, while others can parse multiline JSON only with extra configuration. If your platform expects newline-delimited JSON, do not emit multiline pretty logs to that destination. Route pretty logs to local console and keep shipped logs line-delimited for operational safety.

Spring Boot Integration Note

In Spring Boot projects, ensure only one logging system manages appenders. Conflicting logback.xml and application.properties logging settings can create duplicate appenders or confusing output. Keep one source of truth for log format and destination.

Quick Validation Script

Add a small smoke test in CI that writes two log events and asserts two newline-delimited JSON objects are produced. This catches accidental formatter changes early and protects ingestion contracts for downstream log processing systems.

Environment Consistency

Keep local, staging, and production logging format aligned unless there is a strong reason to diverge, because differences hide formatting regressions until deployment.

Common Pitfalls

  • Forgetting appendLineSeparator for JsonLayout file appenders
  • Expecting pretty JSON while using compact format intentionally
  • Confusing visual line wrapping with actual file line breaks
  • Mixing Logback contrib layout and logstash encoder without clear ownership
  • Sending multiline JSON to collectors that assume one JSON event per line

Most “same line” complaints are configuration clarity issues rather than logging framework bugs.

Summary

  • Compact JSON output is normal and often preferred for production logs.
  • Add appendLineSeparator when using JsonLayout to separate events.
  • Validate actual line structure with shell tools, not only editor display.
  • Keep production logs one-event-per-line for ingestion stability.
  • Use pretty formatting only where human readability is the primary goal.

Course illustration
Course illustration

All Rights Reserved.