Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Jun 20, 2024
1 parent 40c8c52 commit 67515c0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class StructuredLoggingEncoder extends EncoderBase<ILoggingEvent> {

private final ThrowableProxyConverter throwableProxyConverter = new ThrowableProxyConverter();

private String formatAsString;

private StructuredLoggingFormat format;

private Long pid;
Expand All @@ -73,19 +75,7 @@ public class StructuredLoggingEncoder extends EncoderBase<ILoggingEvent> {
* @param format the format
*/
public void setFormat(String format) {
StructuredLoggingFormats formats = StructuredLoggingFormats.loadFromSpringFactories();
StructuredLoggingFormat commonFormat = formats.get(format);
if (commonFormat != null) {
this.format = commonFormat;
}
else if (ClassUtils.isPresent(format, null)) {
this.format = BeanUtils.instantiateClass(ClassUtils.resolveClassName(format, null),
StructuredLoggingFormat.class);
}
else {
throw new IllegalArgumentException(
"Unknown format '%s'. Supported common formats are: %s".formatted(format, formats.getFormats()));
}
this.formatAsString = format;
}

public void setPid(Long pid) {
Expand Down Expand Up @@ -118,11 +108,26 @@ public void setCharset(Charset charset) {

@Override
public void start() {
Assert.state(this.format != null, "Format has not been set");
Assert.state(this.formatAsString != null, "Format has not been set");
this.format = createFormat();
super.start();
this.throwableProxyConverter.start();
}

private StructuredLoggingFormat createFormat() {
StructuredLoggingFormats formats = StructuredLoggingFormats.loadFromSpringFactories();
StructuredLoggingFormat commonFormat = formats.get(this.formatAsString);
if (commonFormat != null) {
return commonFormat;
}
if (ClassUtils.isPresent(this.formatAsString, null)) {
return BeanUtils.instantiateClass(ClassUtils.resolveClassName(this.formatAsString, null),
StructuredLoggingFormat.class);
}
throw new IllegalArgumentException("Unknown format '%s'. Supported common formats are: %s"
.formatted(this.formatAsString, formats.getFormats()));
}

@Override
public void stop() {
this.throwableProxyConverter.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ initialization performed by Boot
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${CONSOLE_LOG_THRESHOLD}</level>
</filter>
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
<!-- <encoder>-->
<!-- <pattern>${CONSOLE_LOG_PATTERN}</pattern>-->
<!-- <charset>${CONSOLE_LOG_CHARSET}</charset>-->
<!-- </encoder>-->
<encoder class="org.springframework.boot.logging.logback.StructuredLoggingEncoder">
<format>${CONSOLE_LOG_STRUCTURED}</format>
<charset>${CONSOLE_LOG_CHARSET}</charset>
<pid>${PID:--1}</pid>
<serviceName>${APPLICATION_NAME:-}</serviceName>
</encoder>
</appender>
</included>
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ sample.name=Andy
# TODO MH: Remove
#logging.structured.file=logfmt
#logging.file.name=/home/mhalbritter/tmp/log.txt
#logging.structured.console=custom
logging.structured.console=ecs
#logging.structured.console=smoketest.simple.CustomFormat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>

0 comments on commit 67515c0

Please sign in to comment.