|
1 | 1 | /*
|
2 |
| - * Copyright 2017-2018 the original author or authors. |
| 2 | + * Copyright 2017-2019 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.cloud.gcp.autoconfigure.logging;
|
18 | 18 |
|
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 |
| - |
35 | 19 | /**
|
36 | 20 | * This class provides a JSON layout for a Logback appender compatible to the Stackdriver
|
37 | 21 | * log format.
|
|
40 | 24 | *
|
41 | 25 | * @author Andreas Berger
|
42 | 26 | * @author Chengyuan Zhao
|
| 27 | + * |
| 28 | + * @deprecated use {@link org.springframework.cloud.gcp.logging.StackdriverJsonLayout} |
43 | 29 | */
|
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 { |
224 | 32 |
|
225 |
| - add(StackdriverTraceConstants.TRACE_ID_ATTRIBUTE, this.includeTraceId, traceId, map); |
226 |
| - } |
227 | 33 | }
|
0 commit comments