-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathLoggerApi.java
More file actions
310 lines (274 loc) · 10.7 KB
/
LoggerApi.java
File metadata and controls
310 lines (274 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package io.sentry.logger;
import io.sentry.HostnameCache;
import io.sentry.IScope;
import io.sentry.ISpan;
import io.sentry.PropagationContext;
import io.sentry.Scopes;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributeType;
import io.sentry.SentryAttributes;
import io.sentry.SentryDate;
import io.sentry.SentryLevel;
import io.sentry.SentryLogEvent;
import io.sentry.SentryLogEventAttributeValue;
import io.sentry.SentryLogLevel;
import io.sentry.SentryOptions;
import io.sentry.SpanId;
import io.sentry.protocol.SdkVersion;
import io.sentry.protocol.SentryId;
import io.sentry.protocol.User;
import io.sentry.util.Platform;
import io.sentry.util.TracingUtils;
import java.util.HashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class LoggerApi implements ILoggerApi {
private final @NotNull Scopes scopes;
public LoggerApi(final @NotNull Scopes scopes) {
this.scopes = scopes;
}
@Override
public void trace(final @Nullable String message, final @Nullable Object... args) {
log(SentryLogLevel.TRACE, message, args);
}
@Override
public void debug(final @Nullable String message, final @Nullable Object... args) {
log(SentryLogLevel.DEBUG, message, args);
}
@Override
public void info(final @Nullable String message, final @Nullable Object... args) {
log(SentryLogLevel.INFO, message, args);
}
@Override
public void warn(final @Nullable String message, final @Nullable Object... args) {
log(SentryLogLevel.WARN, message, args);
}
@Override
public void error(final @Nullable String message, final @Nullable Object... args) {
log(SentryLogLevel.ERROR, message, args);
}
@Override
public void fatal(final @Nullable String message, final @Nullable Object... args) {
log(SentryLogLevel.FATAL, message, args);
}
@Override
public void log(
final @NotNull SentryLogLevel level,
final @Nullable String message,
final @Nullable Object... args) {
captureLog(level, SentryLogParameters.create(null, null), message, args);
}
@Override
public void log(
final @NotNull SentryLogLevel level,
final @Nullable SentryDate timestamp,
final @Nullable String message,
final @Nullable Object... args) {
captureLog(level, SentryLogParameters.create(timestamp, null), message, args);
}
@Override
public void log(
final @NotNull SentryLogLevel level,
final @NotNull SentryLogParameters params,
final @Nullable String message,
final @Nullable Object... args) {
captureLog(level, params, message, args);
}
@SuppressWarnings("AnnotateFormatMethod")
private void captureLog(
final @NotNull SentryLogLevel level,
final @NotNull SentryLogParameters params,
final @Nullable String message,
final @Nullable Object... args) {
final @NotNull SentryOptions options = scopes.getOptions();
try {
if (!scopes.isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Instance is disabled and this 'logger' call is a no-op.");
return;
}
if (!options.getLogs().isEnabled()) {
options
.getLogger()
.log(SentryLevel.WARNING, "Sentry Log is disabled and this 'logger' call is a no-op.");
return;
}
if (message == null) {
return;
}
final @Nullable SentryDate timestamp = params.getTimestamp();
final @NotNull SentryDate timestampToUse =
timestamp == null ? options.getDateProvider().now() : timestamp;
final @NotNull String messageToUse = maybeFormatMessage(message, args);
final @NotNull IScope combinedScope = scopes.getCombinedScopeView();
final @NotNull PropagationContext propagationContext = combinedScope.getPropagationContext();
final @Nullable ISpan span = combinedScope.getSpan();
if (span == null) {
TracingUtils.maybeUpdateBaggage(combinedScope, options);
}
final @NotNull SentryId traceId =
span == null ? propagationContext.getTraceId() : span.getSpanContext().getTraceId();
final @NotNull SpanId spanId =
span == null ? propagationContext.getSpanId() : span.getSpanContext().getSpanId();
final SentryLogEvent logEvent =
new SentryLogEvent(traceId, timestampToUse, messageToUse, level);
logEvent.setAttributes(createAttributes(params, message, spanId, args));
logEvent.setSeverityNumber(level.getSeverityNumber());
scopes.getClient().captureLog(logEvent, combinedScope);
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, "Error while capturing log event", e);
}
}
private @NotNull String maybeFormatMessage(
final @NotNull String message, final @Nullable Object[] args) {
if (args == null || args.length == 0) {
return message;
}
try {
return String.format(message, args);
} catch (Throwable t) {
scopes
.getOptions()
.getLogger()
.log(SentryLevel.ERROR, "Error while running log through String.format", t);
return message;
}
}
private @NotNull HashMap<String, SentryLogEventAttributeValue> createAttributes(
final @NotNull SentryLogParameters params,
final @NotNull String message,
final @NotNull SpanId spanId,
final @Nullable Object... args) {
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes = new HashMap<>();
attributes.put(
"sentry.origin",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, params.getOrigin()));
final @Nullable SentryAttributes incomingAttributes = params.getAttributes();
if (incomingAttributes != null) {
for (SentryAttribute attribute : incomingAttributes.getAttributes().values()) {
final @Nullable Object value = attribute.getValue();
final @NotNull SentryAttributeType type =
attribute.getType() == null ? getType(value) : attribute.getType();
attributes.put(attribute.getName(), new SentryLogEventAttributeValue(type, value));
}
}
if (args != null) {
int i = 0;
for (Object arg : args) {
final @NotNull SentryAttributeType type = getType(arg);
attributes.put(
"sentry.message.parameter." + i, new SentryLogEventAttributeValue(type, arg));
i++;
}
if (i > 0) {
if (attributes.get("sentry.message.template") == null) {
attributes.put(
"sentry.message.template",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, message));
}
}
}
final @Nullable SdkVersion sdkVersion = scopes.getOptions().getSdkVersion();
if (sdkVersion != null) {
attributes.put(
"sentry.sdk.name",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getName()));
attributes.put(
"sentry.sdk.version",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, sdkVersion.getVersion()));
}
final @Nullable String environment = scopes.getOptions().getEnvironment();
if (environment != null) {
attributes.put(
"sentry.environment",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, environment));
}
final @NotNull SentryId scopeReplayId = scopes.getCombinedScopeView().getReplayId();
if (!SentryId.EMPTY_ID.equals(scopeReplayId)) {
attributes.put(
"sentry.replay_id",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, scopeReplayId.toString()));
} else {
final @NotNull SentryId controllerReplayId =
scopes.getOptions().getReplayController().getReplayId();
if (!SentryId.EMPTY_ID.equals(controllerReplayId)) {
attributes.put(
"sentry.replay_id",
new SentryLogEventAttributeValue(
SentryAttributeType.STRING, controllerReplayId.toString()));
attributes.put(
"sentry._internal.replay_is_buffering",
new SentryLogEventAttributeValue(SentryAttributeType.BOOLEAN, true));
}
}
final @Nullable String release = scopes.getOptions().getRelease();
if (release != null) {
attributes.put(
"sentry.release", new SentryLogEventAttributeValue(SentryAttributeType.STRING, release));
}
attributes.put(
"sentry.trace.parent_span_id",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, spanId));
if (Platform.isJvm()) {
setServerName(attributes);
}
setUser(attributes);
return attributes;
}
private void setServerName(
final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
final @NotNull SentryOptions options = scopes.getOptions();
final @Nullable String optionsServerName = options.getServerName();
if (optionsServerName != null) {
attributes.put(
"server.address",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, optionsServerName));
} else if (options.isAttachServerName()) {
final @Nullable String hostname = HostnameCache.getInstance().getHostname();
if (hostname != null) {
attributes.put(
"server.address",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, hostname));
}
}
}
private void setUser(final @NotNull HashMap<String, SentryLogEventAttributeValue> attributes) {
final @Nullable User user = scopes.getCombinedScopeView().getUser();
if (user == null) {
// In case no user is set, we should fallback to the distinct id, known as installation id,
// which is used on Android as default user id
final @Nullable String id = scopes.getOptions().getDistinctId();
if (id != null) {
attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
}
} else {
final @Nullable String id = user.getId();
if (id != null) {
attributes.put("user.id", new SentryLogEventAttributeValue(SentryAttributeType.STRING, id));
}
final @Nullable String username = user.getUsername();
if (username != null) {
attributes.put(
"user.name", new SentryLogEventAttributeValue(SentryAttributeType.STRING, username));
}
final @Nullable String email = user.getEmail();
if (email != null) {
attributes.put(
"user.email", new SentryLogEventAttributeValue(SentryAttributeType.STRING, email));
}
}
}
private @NotNull SentryAttributeType getType(final @Nullable Object arg) {
if (arg instanceof Boolean) {
return SentryAttributeType.BOOLEAN;
}
if (arg instanceof Integer) {
return SentryAttributeType.INTEGER;
}
if (arg instanceof Number) {
return SentryAttributeType.DOUBLE;
}
return SentryAttributeType.STRING;
}
}