Polish logging listeners.
Rename slf4j package to log. Shorten enabled properties. JsonLogSpanListener only depends on Commons Logging.
This commit is contained in:
@@ -14,33 +14,33 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.sleuth.slf4j;
|
||||
package org.springframework.cloud.sleuth.log;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import lombok.Data;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
import org.springframework.cloud.sleuth.event.SpanStoppedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Slf4j
|
||||
@CommonsLog
|
||||
@Order(Ordered.LOWEST_PRECEDENCE)
|
||||
@Data
|
||||
public class JsonSlf4jSpanListener {
|
||||
public class JsonLogSpanListener {
|
||||
|
||||
private final String prefix;
|
||||
private final String suffix;
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
public JsonSlf4jSpanListener() {
|
||||
public JsonLogSpanListener() {
|
||||
prefix = "[span]";
|
||||
suffix = "[endspan]";
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
@@ -49,8 +49,7 @@ public class JsonSlf4jSpanListener {
|
||||
@SneakyThrows
|
||||
@EventListener(SpanStoppedEvent.class)
|
||||
public void stop(SpanStoppedEvent event) {
|
||||
log.info("{}{}{}", prefix,
|
||||
objectMapper.writeValueAsString(event.getSpan()),
|
||||
log.info(prefix + objectMapper.writeValueAsString(event.getSpan()) +
|
||||
suffix);
|
||||
}
|
||||
|
||||
@@ -14,8 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.sleuth.slf4j;
|
||||
package org.springframework.cloud.sleuth.log;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -26,19 +27,26 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass(MDC.class)
|
||||
public class SleuthSlf4jAutoConfiguration {
|
||||
public class SleuthLogAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "spring.cloud.sleuth.listener.slf4j.enabled", matchIfMissing = true)
|
||||
public Slf4jSpanListener slf4jSpanStartedListener() {
|
||||
return new Slf4jSpanListener();
|
||||
@Configuration
|
||||
@ConditionalOnClass(MDC.class)
|
||||
protected static class Slf4jConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "spring.cloud.sleuth.log.slf4j.enabled", matchIfMissing = true)
|
||||
public Slf4jSpanListener slf4jSpanStartedListener() {
|
||||
return new Slf4jSpanListener();
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty("spring.cloud.sleuth.listener.json.slf4j.enabled")
|
||||
public JsonSlf4jSpanListener jsonSlf4jSpanListener() {
|
||||
return new JsonSlf4jSpanListener();
|
||||
@Configuration
|
||||
@ConditionalOnClass(Log.class)
|
||||
protected static class JsonConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnProperty("spring.cloud.sleuth.log.json.enabled")
|
||||
public JsonLogSpanListener jsonSlf4jSpanListener() {
|
||||
return new JsonLogSpanListener();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.sleuth.slf4j;
|
||||
package org.springframework.cloud.sleuth.log;
|
||||
|
||||
import static org.springframework.cloud.sleuth.Trace.SPAN_ID_NAME;
|
||||
import static org.springframework.cloud.sleuth.Trace.TRACE_ID_NAME;
|
||||
@@ -1,7 +1,7 @@
|
||||
# Auto Configuration
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.slf4j.SleuthSlf4jAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.instrument.scheduling.TraceSchedulingAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.instrument.web.TraceWebAutoConfiguration,\
|
||||
org.springframework.cloud.sleuth.instrument.web.client.TraceWebClientAutoConfiguration
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.sleuth.slf4j;
|
||||
package org.springframework.cloud.sleuth.log;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -30,13 +30,13 @@ import java.io.IOException;
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class JsonSlf4jSpanListenerTests {
|
||||
public class JsonLogSpanListenerTests {
|
||||
@Rule
|
||||
public final OutputCapture output = new OutputCapture();
|
||||
|
||||
@Test
|
||||
public void jsonSpanIsOnOneLine() throws IOException {
|
||||
JsonSlf4jSpanListener listener = new JsonSlf4jSpanListener();
|
||||
JsonLogSpanListener listener = new JsonLogSpanListener();
|
||||
Span span = MilliSpan.builder()
|
||||
.name("testSpan")
|
||||
.spanId("spanId1")
|
||||
Reference in New Issue
Block a user