Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
/**
* Parse SOFATracer Configuration in early stage.
*
* @author qilong.zql
* @author huzijie
* @since 2.2.2
*/
public class SofaTracerConfigurationListener
Expand Down Expand Up @@ -98,7 +96,23 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
String.valueOf(sofaTracerProperties.getSamplerPercentage()));

SofaTracerConfiguration.setProperty(SofaTracerConfiguration.JSON_FORMAT_OUTPUT,
String.valueOf(sofaTracerProperties.isJsonOutput()));
String.valueOf(resolveJsonOutput(environment, sofaTracerProperties)));
}

private boolean resolveJsonOutput(ConfigurableEnvironment environment,
SofaTracerProperties sofaTracerProperties) {
if (Binder.get(environment).bind("sofa.boot.tracer.json-output", Boolean.class).isBound()) {
return sofaTracerProperties.isJsonOutput();
}
if (sofaTracerProperties.isOutputStructured()) {
return isStructuredLoggingEnabled(environment);
}
return sofaTracerProperties.isJsonOutput();
}

private boolean isStructuredLoggingEnabled(ConfigurableEnvironment environment) {
return StringUtils.hasText(environment.getProperty("logging.structured.format.console"))
|| StringUtils.hasText(environment.getProperty("logging.structured.format.file"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
/**
* Configuration properties to configure tracer.
*
* @author yangguanchao
* @author huzijie
* @since 2018/04/30
*/
@ConfigurationProperties("sofa.boot.tracer")
Expand Down Expand Up @@ -93,6 +91,11 @@ public class SofaTracerProperties {
*/
private boolean jsonOutput = true;

/**
* Follow SOFABoot structured logging when jsonOutput is not explicitly configured.
*/
private boolean outputStructured;

public String getDisableDigestLog() {
return disableDigestLog;
}
Expand Down Expand Up @@ -180,4 +183,12 @@ public boolean isJsonOutput() {
public void setJsonOutput(boolean jsonOutput) {
this.jsonOutput = jsonOutput;
}

public boolean isOutputStructured() {
return outputStructured;
}

public void setOutputStructured(boolean outputStructured) {
this.outputStructured = outputStructured;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@

/**
* Tests for {@link SofaTracerConfigurationListener}.
*
* @author huzijie
* @version SofaTracerConfigurationListenerTests.java, v 0.1 2023年01月11日 3:28 PM huzijie Exp $
*/
public class SofaTracerConfigurationListenerTests {

Expand Down Expand Up @@ -86,6 +83,8 @@ public void injectSofaTracerConfiguration() {
props.put("sofa.boot.tracer.samplerName", "test");
props.put("sofa.boot.tracer.samplerPercentage", "200.0");
props.put("sofa.boot.tracer.samplerCustomRuleClassName", "TestRuleClass");
props.put("sofa.boot.tracer.output-structured", "true");
props.put("logging.structured.format.file", "ecs");
props.put("sofa.boot.tracer.jsonOutput", "false");
application.setDefaultProperties(props);
application.addListeners(new SpringCloudConfigListener());
Expand Down Expand Up @@ -129,6 +128,39 @@ public void injectSofaTracerConfiguration() {
.isEqualTo("false");
}

@Test
public void tracerJsonOutputFollowsStructuredLogging() {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
Map<String, Object> props = new HashMap<>();
props.put("spring.application.name", "tracer-structured");
props.put("sofa.boot.tracer.output-structured", "true");
props.put("logging.structured.format.console", "logstash");
application.setDefaultProperties(props);
application.addListeners(new SpringCloudConfigListener());
application.addListeners(new SofaTracerConfigurationListener());
this.context = application.run();

assertThat(SofaTracerConfiguration.getProperty(SofaTracerConfiguration.JSON_FORMAT_OUTPUT))
.isEqualTo("true");
}

@Test
public void tracerJsonOutputCanBeDisabledWhenStructuredLoggingIsDisabled() {
SpringApplication application = new SpringApplication(Config.class);
application.setWebApplicationType(WebApplicationType.NONE);
Map<String, Object> props = new HashMap<>();
props.put("spring.application.name", "tracer-pattern");
props.put("sofa.boot.tracer.output-structured", "true");
application.setDefaultProperties(props);
application.addListeners(new SpringCloudConfigListener());
application.addListeners(new SofaTracerConfigurationListener());
this.context = application.run();

assertThat(SofaTracerConfiguration.getProperty(SofaTracerConfiguration.JSON_FORMAT_OUTPUT))
.isEqualTo("false");
}

@Configuration(proxyBeanMethods = false)
static class Config {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand All @@ -42,8 +43,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand All @@ -65,8 +67,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t %c{2} - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand All @@ -87,8 +90,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t %c{2} - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand All @@ -109,8 +113,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t %c{2} - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand Down Expand Up @@ -139,4 +144,4 @@
<appender-ref ref="ERROR-APPENDER"/>
</root>

</configuration>
</configuration>
10 changes: 5 additions & 5 deletions sofa-boot-project/sofa-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.boot.logging.logback;

import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.Encoder;
import ch.qos.logback.core.encoder.EncoderBase;
import org.springframework.boot.logging.logback.StructuredLogEncoder;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
* Logback encoder that keeps SOFABoot's pattern output by default and switches to
* Spring Boot's structured logging encoder when a structured format system property is set.
*/
public class SofaBootStructuredLogEncoder extends EncoderBase<ILoggingEvent> {

private String pattern;

private Charset charset = StandardCharsets.UTF_8;

private String formatProperty;

private Encoder<ILoggingEvent> delegate;

public void setPattern(String pattern) {
this.pattern = pattern;
}

public void setCharset(Charset charset) {
this.charset = charset;
}

public void setFormatProperty(String formatProperty) {
this.formatProperty = formatProperty;
}

@Override
public void start() {
Assert.state(StringUtils.hasText(this.pattern), "Pattern has not been set");
Assert.state(StringUtils.hasText(this.formatProperty), "Format property has not been set");
this.delegate = createDelegate();
super.start();
}

private Encoder<ILoggingEvent> createDelegate() {
String format = System.getProperty(this.formatProperty);
if (StringUtils.hasText(format)) {
return createStructuredDelegate(format);
}
return createPatternDelegate();
}

private Encoder<ILoggingEvent> createStructuredDelegate(String format) {
StructuredLogEncoder encoder = new StructuredLogEncoder();
encoder.setContext(getContext());
encoder.setCharset((this.charset != null) ? this.charset : StandardCharsets.UTF_8);
encoder.setFormat(format);
try {
encoder.start();
return encoder;
} catch (RuntimeException exception) {
addWarn(
"Falling back to pattern logging because structured logging could not be initialized for "
+ this.formatProperty, exception);
encoder.stop();
return createPatternDelegate();
}
}

private Encoder<ILoggingEvent> createPatternDelegate() {
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(getContext());
encoder.setPattern(this.pattern);
encoder.setCharset((this.charset != null) ? this.charset : StandardCharsets.UTF_8);
encoder.start();
return encoder;
}

@Override
public void stop() {
if (this.delegate != null) {
this.delegate.stop();
}
super.stop();
}

@Override
public byte[] headerBytes() {
return (this.delegate != null) ? this.delegate.headerBytes() : null;
}

@Override
public byte[] encode(ILoggingEvent event) {
Assert.state(this.delegate != null, "Encoder has not been started");
return this.delegate.encode(event);
}

@Override
public byte[] footerBytes() {
return (this.delegate != null) ? this.delegate.footerBytes() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
<FileNamePattern>${logging.path}/sofa-runtime/common-error.log.%d{yyyy-MM-dd}</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<charset>${file.encoding}</charset>
</encoder>
</appender>
Expand All @@ -43,8 +44,9 @@
<FileNamePattern>${logging.path}/sofa-runtime/sofa-default.log.%d{yyyy-MM-dd}</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<charset>${file.encoding}</charset>
</encoder>
</appender>
Expand All @@ -66,8 +68,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand All @@ -88,8 +91,9 @@
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<encoder class="com.alipay.sofa.boot.logging.logback.SofaBootStructuredLogEncoder">
<pattern>%d %-5p %-32t - %m%n</pattern>
<formatProperty>FILE_LOG_STRUCTURED_FORMAT</formatProperty>
<!-- 编码 -->
<charset>${file.encoding}</charset>
</encoder>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@

/**
* Tests for {@link LogEnvironmentPostProcessor}.
*
* @author huzijie
* @version LogEnvironmentPreparingListenerTests.java, v 0.1 2023年02月01日 3:35 PM huzijie Exp $
*/
public class LogEnvironmentPreparingListenerTests {

Expand Down
Loading
Loading