Skip to content

Commit b9fd85d

Browse files
authored
Making Logging a separate module outside of autoconfiguration (spring-attic#1455)
* Keep moved classes and xml config files as deprecated. Fixes: spring-attic#1448.
1 parent e225207 commit b9fd85d

File tree

84 files changed

+871
-520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+871
-520
lines changed

docs/src/main/asciidoc/logging.adoc

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ However, if Sleuth is used the trace ID will be picked up from the MDC.
4646
Currently, only Logback is supported and there are 2 possibilities to log to Stackdriver via this library with Logback: via direct API calls and through JSON-formatted console logs.
4747

4848
==== Log via API
49-
A Stackdriver appender is available using `org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml`.
49+
A Stackdriver appender is available using `org/springframework/cloud/gcp/logging/logback-appender.xml`.
5050
This appender builds a Stackdriver Logging log entry from a JUL or Logback log entry, adds a trace ID to it and sends it to Stackdriver Logging.
5151

5252
`STACKDRIVER_LOG_NAME` and `STACKDRIVER_LOG_FLUSH_LEVEL` environment variables can be used to customize the `STACKDRIVER` appender.
@@ -55,7 +55,7 @@ Your configuration may then look like this:
5555
[source, xml]
5656
----
5757
<configuration>
58-
<include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />
58+
<include resource="org/springframework/cloud/gcp/logging/logback-appender.xml" />
5959
6060
<root level="INFO">
6161
<appender-ref ref="STACKDRIVER" />
@@ -78,13 +78,13 @@ This can also be set via the `STACKDRIVER_LOG_FLUSH_LEVEL` environmental variabl
7878

7979
==== Log via Console
8080

81-
For Logback, a `org/springframework/cloud/gcp/autoconfigure/logging/logback-json-appender.xml` file is made available for import to make it easier to configure the JSON Logback appender.
81+
For Logback, a `org/springframework/cloud/gcp/logging/logback-json-appender.xml` file is made available for import to make it easier to configure the JSON Logback appender.
8282

8383
Your configuration may then look something like this:
8484
[source, xml]
8585
----
8686
<configuration>
87-
<include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-json-appender.xml" />
87+
<include resource="org/springframework/cloud/gcp/logging/logback-json-appender.xml" />
8888
8989
<root level="INFO">
9090
<appender-ref ref="CONSOLE_JSON" />
@@ -93,7 +93,7 @@ Your configuration may then look something like this:
9393
----
9494

9595
If your application is running on Google Kubernetes Engine, Google Compute Engine or Google App Engine Flexible, your console logging is automatically saved to Google Stackdriver Logging.
96-
Therefore, you can just include `org/springframework/cloud/gcp/autoconfigure/logging/logback-json-appender.xml` in your logging configuration, which logs JSON entries to the console.
96+
Therefore, you can just include `org/springframework/cloud/gcp/logging/logback-json-appender.xml` in your logging configuration, which logs JSON entries to the console.
9797
The trace id will be set correctly.
9898

9999
If you want to have more control over the log output, you can further configure the appender.

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<module>spring-cloud-gcp-security-iap</module>
3939
<module>spring-cloud-gcp-vision</module>
4040
<module>spring-cloud-gcp-kotlin-samples</module>
41+
<module>spring-cloud-gcp-logging</module>
4142
</modules>
4243

4344
<properties>

spring-cloud-gcp-autoconfigure/pom.xml

+5-10
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,21 @@
3636
</dependency>
3737

3838
<!-- Logging -->
39+
<dependency>
40+
<groupId>org.springframework.cloud</groupId>
41+
<artifactId>spring-cloud-gcp-logging</artifactId>
42+
<optional>true</optional>
43+
</dependency>
3944
<dependency>
4045
<groupId>javax.servlet</groupId>
4146
<artifactId>javax.servlet-api</artifactId>
4247
<scope>provided</scope>
4348
</dependency>
44-
<dependency>
45-
<groupId>com.google.cloud</groupId>
46-
<artifactId>google-cloud-logging-logback</artifactId>
47-
<optional>true</optional>
48-
</dependency>
4949
<dependency>
5050
<groupId>org.springframework</groupId>
5151
<artifactId>spring-webmvc</artifactId>
5252
<optional>true</optional>
5353
</dependency>
54-
<dependency>
55-
<groupId>ch.qos.logback.contrib</groupId>
56-
<artifactId>logback-json-classic</artifactId>
57-
<optional>true</optional>
58-
</dependency>
5954

6055
<!-- Pub/Sub -->
6156
<dependency>

spring-cloud-gcp-autoconfigure/src/main/java/org/springframework/cloud/gcp/autoconfigure/config/GoogleConfigPropertySourceLocator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-cloud-gcp-autoconfigure/src/main/java/org/springframework/cloud/gcp/autoconfigure/datastore/GcpDatastoreAutoConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-cloud-gcp-autoconfigure/src/main/java/org/springframework/cloud/gcp/autoconfigure/datastore/GcpDatastoreProperties.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,46 +16,22 @@
1616

1717
package org.springframework.cloud.gcp.autoconfigure.logging;
1818

19-
import org.springframework.beans.factory.annotation.Autowired;
20-
import org.springframework.cloud.gcp.autoconfigure.logging.extractors.XCloudTraceIdExtractor;
2119
import org.springframework.cloud.gcp.core.GcpProjectIdProvider;
2220
import org.springframework.context.annotation.Configuration;
23-
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
24-
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
2521

2622
/**
2723
* MVC Adapter that adds the {@link TraceIdLoggingWebMvcInterceptor}.
2824
*
2925
* @author Mike Eltsufin
3026
* @author Chengyuan Zhao
27+
*
28+
* @deprecated use {@link org.springframework.cloud.gcp.logging.LoggingWebMvcConfigurer}
3129
*/
3230
@Configuration
33-
public class LoggingWebMvcConfigurer implements WebMvcConfigurer {
34-
35-
private final TraceIdLoggingWebMvcInterceptor interceptor;
36-
37-
/**
38-
* Constructor that accepts an {@link TraceIdLoggingWebMvcInterceptor}. If the given
39-
* interceptor is null, then a default {@link XCloudTraceIdExtractor} is used.
40-
* @param interceptor the interceptor to use with this configurer. If not provided a
41-
* {@link TraceIdLoggingWebMvcInterceptor} is used with the trace ID extractor
42-
* described above.
43-
* @param projectIdProvider the project ID provider to use
44-
*/
45-
public LoggingWebMvcConfigurer(
46-
@Autowired(required = false) TraceIdLoggingWebMvcInterceptor interceptor,
31+
@Deprecated
32+
public class LoggingWebMvcConfigurer extends org.springframework.cloud.gcp.logging.LoggingWebMvcConfigurer {
33+
public LoggingWebMvcConfigurer(TraceIdLoggingWebMvcInterceptor interceptor,
4734
GcpProjectIdProvider projectIdProvider) {
48-
if (interceptor != null) {
49-
this.interceptor = interceptor;
50-
}
51-
else {
52-
this.interceptor = new TraceIdLoggingWebMvcInterceptor(
53-
new XCloudTraceIdExtractor());
54-
}
55-
}
56-
57-
@Override
58-
public void addInterceptors(InterceptorRegistry registry) {
59-
registry.addInterceptor(this.interceptor);
35+
super(interceptor, projectIdProvider);
6036
}
6137
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2018 the original author or authors.
2+
* Copyright 2017-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,22 +16,6 @@
1616

1717
package org.springframework.cloud.gcp.autoconfigure.logging;
1818

19-
import java.util.Arrays;
20-
import java.util.HashSet;
21-
import java.util.LinkedHashMap;
22-
import java.util.Map;
23-
import java.util.Set;
24-
import java.util.concurrent.TimeUnit;
25-
26-
import ch.qos.logback.classic.spi.ILoggingEvent;
27-
import ch.qos.logback.classic.spi.IThrowableProxy;
28-
import ch.qos.logback.contrib.json.classic.JsonLayout;
29-
import com.google.gson.Gson;
30-
31-
import org.springframework.cloud.gcp.core.DefaultGcpProjectIdProvider;
32-
import org.springframework.cloud.gcp.core.GcpProjectIdProvider;
33-
import org.springframework.util.StringUtils;
34-
3519
/**
3620
* This class provides a JSON layout for a Logback appender compatible to the Stackdriver
3721
* log format.
@@ -40,188 +24,10 @@
4024
*
4125
* @author Andreas Berger
4226
* @author Chengyuan Zhao
27+
*
28+
* @deprecated use {@link org.springframework.cloud.gcp.logging.StackdriverJsonLayout}
4329
*/
44-
public class StackdriverJsonLayout extends JsonLayout {
45-
46-
private static final Set<String> FILTERED_MDC_FIELDS = new HashSet<>(Arrays.asList(
47-
StackdriverTraceConstants.MDC_FIELD_TRACE_ID,
48-
StackdriverTraceConstants.MDC_FIELD_SPAN_ID,
49-
StackdriverTraceConstants.MDC_FIELD_SPAN_EXPORT));
50-
51-
private String projectId;
52-
53-
private boolean includeTraceId;
54-
55-
private boolean includeSpanId;
56-
57-
private boolean includeExceptionInMessage;
58-
59-
/**
60-
* creates a layout for a Logback appender compatible to the Stackdriver log format.
61-
*/
62-
public StackdriverJsonLayout() {
63-
this.appendLineSeparator = true;
64-
this.includeExceptionInMessage = true;
65-
this.includeException = false;
66-
this.includeTraceId = true;
67-
this.includeSpanId = true;
68-
Gson formatter = new Gson();
69-
setJsonFormatter(formatter::toJson);
70-
}
71-
72-
/**
73-
* Get the project id.
74-
* @return the Google Cloud project id relevant for logging the traceId
75-
*/
76-
public String getProjectId() {
77-
return this.projectId;
78-
}
79-
80-
/**
81-
* set the project id.
82-
* @param projectId the Google Cloud project id relevant for logging the traceId
83-
*/
84-
public void setProjectId(String projectId) {
85-
this.projectId = projectId;
86-
}
87-
88-
/**
89-
* check if the trace id is included.
90-
* @return true if the traceId should be included into the JSON
91-
*/
92-
public boolean isIncludeTraceId() {
93-
return this.includeTraceId;
94-
}
95-
96-
/**
97-
* set whether the trace id is included.
98-
* @param includeTraceId true if the traceId should be included into the JSON
99-
*/
100-
public void setIncludeTraceId(boolean includeTraceId) {
101-
this.includeTraceId = includeTraceId;
102-
}
103-
104-
/**
105-
* check if the span id is included.
106-
* @return true if the spanId should be included into the JSON
107-
*/
108-
public boolean isIncludeSpanId() {
109-
return this.includeSpanId;
110-
}
111-
112-
/**
113-
* set whether the span id is included.
114-
* @param includeSpanId true if the spanId should be included into the JSON
115-
*/
116-
public void setIncludeSpanId(boolean includeSpanId) {
117-
this.includeSpanId = includeSpanId;
118-
}
119-
120-
/**
121-
* check if there is an included exception in the message.
122-
* @return true if the exception should be added to the message
123-
*/
124-
public boolean isIncludeExceptionInMessage() {
125-
return this.includeExceptionInMessage;
126-
}
127-
128-
/**
129-
* set whether the exception is included in the message.
130-
* @param includeExceptionInMessage true if the exception should be added to the message
131-
*/
132-
public void setIncludeExceptionInMessage(boolean includeExceptionInMessage) {
133-
this.includeExceptionInMessage = includeExceptionInMessage;
134-
}
135-
136-
@Override
137-
public void start() {
138-
super.start();
139-
140-
// If no Project ID set, then attempt to resolve it with the default project ID provider
141-
if (StringUtils.isEmpty(this.projectId) || this.projectId.endsWith("_IS_UNDEFINED")) {
142-
GcpProjectIdProvider projectIdProvider = new DefaultGcpProjectIdProvider();
143-
this.projectId = projectIdProvider.getProjectId();
144-
}
145-
}
146-
147-
/**
148-
* Convert a logging event into a Map.
149-
* @param event the logging event
150-
* @return the map which should get rendered as JSON
151-
*/
152-
@Override
153-
protected Map<String, Object> toJsonMap(ILoggingEvent event) {
154-
155-
Map<String, Object> map = new LinkedHashMap<>();
156-
157-
if (this.includeMDC) {
158-
event.getMDCPropertyMap().forEach((key, value) -> {
159-
if (!FILTERED_MDC_FIELDS.contains(key)) {
160-
map.put(key, value);
161-
}
162-
});
163-
}
164-
if (this.includeTimestamp) {
165-
map.put(StackdriverTraceConstants.TIMESTAMP_SECONDS_ATTRIBUTE,
166-
TimeUnit.MILLISECONDS.toSeconds(event.getTimeStamp()));
167-
map.put(StackdriverTraceConstants.TIMESTAMP_NANOS_ATTRIBUTE,
168-
TimeUnit.MILLISECONDS.toNanos(event.getTimeStamp() % 1_000));
169-
}
170-
171-
add(StackdriverTraceConstants.SEVERITY_ATTRIBUTE, this.includeLevel,
172-
String.valueOf(event.getLevel()), map);
173-
add(JsonLayout.THREAD_ATTR_NAME, this.includeThreadName, event.getThreadName(), map);
174-
add(JsonLayout.LOGGER_ATTR_NAME, this.includeLoggerName, event.getLoggerName(), map);
175-
176-
if (this.includeFormattedMessage) {
177-
String message = event.getFormattedMessage();
178-
if (this.includeExceptionInMessage) {
179-
IThrowableProxy throwableProxy = event.getThrowableProxy();
180-
if (throwableProxy != null) {
181-
String stackTrace = getThrowableProxyConverter().convert(event);
182-
if (stackTrace != null && !stackTrace.equals("")) {
183-
message += "\n" + stackTrace;
184-
}
185-
}
186-
}
187-
map.put(JsonLayout.FORMATTED_MESSAGE_ATTR_NAME, message);
188-
}
189-
add(JsonLayout.MESSAGE_ATTR_NAME, this.includeMessage, event.getMessage(), map);
190-
add(JsonLayout.CONTEXT_ATTR_NAME, this.includeContextName, event.getLoggerContextVO().getName(), map);
191-
addThrowableInfo(JsonLayout.EXCEPTION_ATTR_NAME, this.includeException, event, map);
192-
addTraceId(event, map);
193-
add(StackdriverTraceConstants.SPAN_ID_ATTRIBUTE, this.includeSpanId,
194-
event.getMDCPropertyMap().get(StackdriverTraceConstants.MDC_FIELD_SPAN_ID), map);
195-
addCustomDataToJsonMap(map, event);
196-
return map;
197-
}
198-
199-
protected String formatTraceId(final String traceId) {
200-
// Trace IDs are either 64-bit or 128-bit, which is 16-digit hex, or 32-digit hex.
201-
// If traceId is 64-bit (16-digit hex), then we need to prepend 0's to make a 32-digit hex.
202-
if (traceId != null && traceId.length() == 16) {
203-
return "0000000000000000" + traceId;
204-
}
205-
return traceId;
206-
}
207-
208-
private void addTraceId(ILoggingEvent event, Map<String, Object> map) {
209-
if (!this.includeTraceId) {
210-
return;
211-
}
212-
213-
String traceId =
214-
event.getMDCPropertyMap().get(StackdriverTraceConstants.MDC_FIELD_TRACE_ID);
215-
if (traceId == null) {
216-
traceId = TraceIdLoggingEnhancer.getCurrentTraceId();
217-
}
218-
if (!StringUtils.isEmpty(traceId)
219-
&& !StringUtils.isEmpty(this.projectId)
220-
&& !this.projectId.endsWith("_IS_UNDEFINED")) {
221-
traceId = StackdriverTraceConstants.composeFullTraceName(
222-
this.projectId, formatTraceId(traceId));
223-
}
30+
@Deprecated
31+
public class StackdriverJsonLayout extends org.springframework.cloud.gcp.logging.StackdriverJsonLayout {
22432

225-
add(StackdriverTraceConstants.TRACE_ID_ATTRIBUTE, this.includeTraceId, traceId, map);
226-
}
22733
}

0 commit comments

Comments
 (0)