Eg: fpdefense
Each log line will be a JSON object with fields like @timestamp, @version, message, logger_name, thread_name, level, and stack traces as structured arrays. If you need to customize which fields appear or add MDC context, the LogstashEncoder supports extensive configuration via nested elements in the appender.
pom.xml
<!-- Logback JSON encoder (structured logging) -->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>9.0</version>
</dependency>
src/main/resources/logback-spring.xml
Easy to read for humans (profile local) and structured log for tools (profile !local)
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- Appender for development -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%gray(%d{yyyy-MM-dd HH:mm:ss:SSS}) %highlight(%-5level) %cyan(%C{1}.%M:%L) - %m%n</pattern>
</encoder>
</appender>
<!-- Appender for CloudWatch -->
<appender name="CLOUDWATCH_JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeLevelValue>false</includeLevelValue>
<fieldNames>
<timestamp>@timestamp</timestamp>
<message>@message</message>
<logger>logger</logger>
<level>level</level>
<levelValue>[ignore]</levelValue>
<thread>thread</thread>
<version>[ignore]</version>
</fieldNames>
</encoder>
</appender>
<!-- Enable DEBUG (or TRACE) only for tests ·
<logger name="edu.cou" level="DEBUG"/> -->
<!-- LOG_HUMAN_READABLE=true: Easy to read by humans (for local development) -->
<!-- LOG_HUMAN_READABLE unset or false: Structured JSON for CloudWatch -->
<condition class="ch.qos.logback.core.boolex.PropertyEqualityCondition">
<key>LOG_HUMAN_READABLE</key>
<value>true</value>
</condition>
<if>
<then>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</then>
<else>
<root level="INFO">
<appender-ref ref="CLOUDWATCH_JSON" />
</root>
</else>
</if>
</configuration>
src/test/resources/logback-spring.xml
Easy to read for humans
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- Appender for development -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%gray(%d{yyyy-MM-dd HH:mm:ss:SSS}) %highlight(%-5level) %cyan(%C{1}.%M:%L) - %m%n</pattern>
</encoder>
</appender>
<!-- Enable DEBUG (or TRACE) only for tests · -->
<logger name="edu.cou" level="DEBUG"/>
<!-- Easy to read by humans -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Human readable log selection for development purposes:
Appender selection is based in environment variable LOG_HUMAN_READABLE:
LOG_HUMAN_READABLE=true --> Easy to read by humans
LOG_HUMAN_READABLE unset or false --> Structured JSON
Eg:
export LOG_HUMAN_READABLE=true