Skip to content

Commit

Permalink
Fix: Use logger set on SentryOptions in GsonSerializer
Browse files Browse the repository at this point in the history
Fixes #1308
  • Loading branch information
maciejwalkowiak committed Mar 9, 2021
1 parent 13dd94a commit 95a25f4
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.sentry.test

import com.nhaarman.mockitokotlin2.check
import io.sentry.GsonSerializer
import io.sentry.NoOpLogger
import io.sentry.SentryEnvelope
import io.sentry.SentryEvent
import io.sentry.SentryOptions
Expand All @@ -12,7 +11,7 @@ import io.sentry.SentryOptions
*/
inline fun checkEvent(noinline predicate: (SentryEvent) -> Unit): SentryEnvelope {
val options = SentryOptions().apply {
setSerializer(GsonSerializer(NoOpLogger.getInstance(), envelopeReader))
setSerializer(GsonSerializer(SentryOptions(), envelopeReader))
}
return check {
val event = it.items.first().getEvent(options.serializer)!!
Expand Down
36 changes: 18 additions & 18 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public abstract interface class io/sentry/EventProcessor {
}

public final class io/sentry/GsonSerializer : io/sentry/ISerializer {
public fun <init> (Lio/sentry/ILogger;Lio/sentry/IEnvelopeReader;)V
public fun <init> (Lio/sentry/SentryOptions;Lio/sentry/IEnvelopeReader;)V
public fun deserialize (Ljava/io/Reader;Ljava/lang/Class;)Ljava/lang/Object;
public fun deserializeEnvelope (Ljava/io/InputStream;)Lio/sentry/SentryEnvelope;
public fun serialize (Lio/sentry/SentryEnvelope;Ljava/io/OutputStream;)V
Expand Down Expand Up @@ -906,7 +906,7 @@ public final class io/sentry/Session$State : java/lang/Enum {
}

public final class io/sentry/SessionAdapter : com/google/gson/TypeAdapter {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun read (Lcom/google/gson/stream/JsonReader;)Lio/sentry/Session;
public synthetic fun read (Lcom/google/gson/stream/JsonReader;)Ljava/lang/Object;
public fun write (Lcom/google/gson/stream/JsonWriter;Lio/sentry/Session;)V
Expand Down Expand Up @@ -1036,25 +1036,25 @@ public final class io/sentry/adapters/CollectionAdapter : com/google/gson/JsonSe
}

public final class io/sentry/adapters/ContextsDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lio/sentry/protocol/Contexts;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
}

public final class io/sentry/adapters/ContextsSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun serialize (Lio/sentry/protocol/Contexts;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

public final class io/sentry/adapters/DateDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/util/Date;
}

public final class io/sentry/adapters/DateSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Ljava/util/Date;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}
Expand All @@ -1066,73 +1066,73 @@ public final class io/sentry/adapters/MapAdapter : com/google/gson/JsonSerialize
}

public final class io/sentry/adapters/OrientationDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lio/sentry/protocol/Device$DeviceOrientation;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
}

public final class io/sentry/adapters/OrientationSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun serialize (Lio/sentry/protocol/Device$DeviceOrientation;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

public final class io/sentry/adapters/SentryIdDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lio/sentry/protocol/SentryId;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
}

public final class io/sentry/adapters/SentryIdSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun serialize (Lio/sentry/protocol/SentryId;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

public final class io/sentry/adapters/SentryLevelDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lio/sentry/SentryLevel;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
}

public final class io/sentry/adapters/SentryLevelSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun serialize (Lio/sentry/SentryLevel;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

public final class io/sentry/adapters/SpanIdDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lio/sentry/SpanId;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
}

public final class io/sentry/adapters/SpanIdSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun serialize (Lio/sentry/SpanId;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

public final class io/sentry/adapters/SpanStatusDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lio/sentry/SpanStatus;
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
}

public final class io/sentry/adapters/SpanStatusSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public fun serialize (Lio/sentry/SpanStatus;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}

public final class io/sentry/adapters/TimeZoneDeserializerAdapter : com/google/gson/JsonDeserializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public synthetic fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/lang/Object;
public fun deserialize (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Ljava/util/TimeZone;
}

public final class io/sentry/adapters/TimeZoneSerializerAdapter : com/google/gson/JsonSerializer {
public fun <init> (Lio/sentry/ILogger;)V
public fun <init> (Lio/sentry/SentryOptions;)V
public synthetic fun serialize (Ljava/lang/Object;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
public fun serialize (Ljava/util/TimeZone;Ljava/lang/reflect/Type;Lcom/google/gson/JsonSerializationContext;)Lcom/google/gson/JsonElement;
}
Expand Down
54 changes: 28 additions & 26 deletions sentry/src/main/java/io/sentry/GsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public final class GsonSerializer implements ISerializer {
@SuppressWarnings("CharsetObjectCanBeUsed")
private static final Charset UTF_8 = Charset.forName("UTF-8");

/** the ILogger interface */
private final @NotNull ILogger logger;
/** the SentryOptions */
private final @NotNull SentryOptions options;

/** the Gson instance */
private final @NotNull Gson gson;
Expand All @@ -60,12 +60,12 @@ public final class GsonSerializer implements ISerializer {
/**
* AndroidSerializer ctor
*
* @param logger the ILogger interface
* @param options the SentryOptions object
* @param envelopeReader the IEnvelopeReader interface
*/
public GsonSerializer(
final @NotNull ILogger logger, final @NotNull IEnvelopeReader envelopeReader) {
this.logger = Objects.requireNonNull(logger, "The ILogger object is required.");
final @NotNull SentryOptions options, final @NotNull IEnvelopeReader envelopeReader) {
this.options = Objects.requireNonNull(options, "The SentryOptions object is required.");
this.envelopeReader =
Objects.requireNonNull(envelopeReader, "The IEnvelopeReader object is required.");

Expand All @@ -80,28 +80,28 @@ public GsonSerializer(
private @NotNull Gson provideGson() {
return new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(SentryId.class, new SentryIdSerializerAdapter(logger))
.registerTypeAdapter(SentryId.class, new SentryIdDeserializerAdapter(logger))
.registerTypeAdapter(Date.class, new DateSerializerAdapter(logger))
.registerTypeAdapter(Date.class, new DateDeserializerAdapter(logger))
.registerTypeAdapter(TimeZone.class, new TimeZoneSerializerAdapter(logger))
.registerTypeAdapter(TimeZone.class, new TimeZoneDeserializerAdapter(logger))
.registerTypeAdapter(SentryId.class, new SentryIdSerializerAdapter(options))
.registerTypeAdapter(SentryId.class, new SentryIdDeserializerAdapter(options))
.registerTypeAdapter(Date.class, new DateSerializerAdapter(options))
.registerTypeAdapter(Date.class, new DateDeserializerAdapter(options))
.registerTypeAdapter(TimeZone.class, new TimeZoneSerializerAdapter(options))
.registerTypeAdapter(TimeZone.class, new TimeZoneDeserializerAdapter(options))
.registerTypeAdapter(
Device.DeviceOrientation.class, new OrientationSerializerAdapter(logger))
Device.DeviceOrientation.class, new OrientationSerializerAdapter(options))
.registerTypeAdapter(
Device.DeviceOrientation.class, new OrientationDeserializerAdapter(logger))
.registerTypeAdapter(SentryLevel.class, new SentryLevelSerializerAdapter(logger))
.registerTypeAdapter(SentryLevel.class, new SentryLevelDeserializerAdapter(logger))
.registerTypeAdapter(Contexts.class, new ContextsDeserializerAdapter(logger))
.registerTypeAdapter(Contexts.class, new ContextsSerializerAdapter(logger))
Device.DeviceOrientation.class, new OrientationDeserializerAdapter(options))
.registerTypeAdapter(SentryLevel.class, new SentryLevelSerializerAdapter(options))
.registerTypeAdapter(SentryLevel.class, new SentryLevelDeserializerAdapter(options))
.registerTypeAdapter(Contexts.class, new ContextsDeserializerAdapter(options))
.registerTypeAdapter(Contexts.class, new ContextsSerializerAdapter(options))
.registerTypeAdapterFactory(UnknownPropertiesTypeAdapterFactory.get())
.registerTypeAdapter(SentryEnvelopeHeader.class, new SentryEnvelopeHeaderAdapter())
.registerTypeAdapter(SentryEnvelopeItemHeader.class, new SentryEnvelopeItemHeaderAdapter())
.registerTypeAdapter(Session.class, new SessionAdapter(logger))
.registerTypeAdapter(SpanId.class, new SpanIdDeserializerAdapter(logger))
.registerTypeAdapter(SpanId.class, new SpanIdSerializerAdapter(logger))
.registerTypeAdapter(SpanStatus.class, new SpanStatusDeserializerAdapter(logger))
.registerTypeAdapter(SpanStatus.class, new SpanStatusSerializerAdapter(logger))
.registerTypeAdapter(Session.class, new SessionAdapter(options))
.registerTypeAdapter(SpanId.class, new SpanIdDeserializerAdapter(options))
.registerTypeAdapter(SpanId.class, new SpanIdSerializerAdapter(options))
.registerTypeAdapter(SpanStatus.class, new SpanStatusDeserializerAdapter(options))
.registerTypeAdapter(SpanStatus.class, new SpanStatusSerializerAdapter(options))
.registerTypeHierarchyAdapter(Collection.class, new CollectionAdapter())
.registerTypeHierarchyAdapter(Map.class, new MapAdapter())
.disableHtmlEscaping()
Expand Down Expand Up @@ -135,7 +135,7 @@ Device.DeviceOrientation.class, new OrientationDeserializerAdapter(logger))
try {
return envelopeReader.read(inputStream);
} catch (IOException e) {
logger.log(SentryLevel.ERROR, "Error deserializing envelope.", e);
options.getLogger().log(SentryLevel.ERROR, "Error deserializing envelope.", e);
return null;
}
}
Expand All @@ -146,8 +146,8 @@ public <T> void serialize(final @NotNull T entity, final @NotNull Writer writer)
Objects.requireNonNull(entity, "The entity is required.");
Objects.requireNonNull(writer, "The Writer object is required.");

if (logger.isEnabled(SentryLevel.DEBUG)) {
logger.log(SentryLevel.DEBUG, "Serializing object: %s", gson.toJson(entity));
if (options.getLogger().isEnabled(SentryLevel.DEBUG)) {
options.getLogger().log(SentryLevel.DEBUG, "Serializing object: %s", gson.toJson(entity));
}
gson.toJson(entity, entity.getClass(), writer);

Expand Down Expand Up @@ -187,7 +187,9 @@ public void serialize(

writer.write("\n");
} catch (Exception exception) {
logger.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to create envelope item. Dropping it.", exception);
}
}
writer.flush();
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class SentryOptions {
private @NotNull IEnvelopeReader envelopeReader = new EnvelopeReader();

/** Serializer interface to serialize/deserialize json events */
private @NotNull ISerializer serializer = new GsonSerializer(logger, envelopeReader);
private @NotNull ISerializer serializer = new GsonSerializer(this, envelopeReader);

/**
* Sentry client name used for the HTTP authHeader and userAgent eg
Expand Down
16 changes: 9 additions & 7 deletions sentry/src/main/java/io/sentry/SessionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
@ApiStatus.Internal
public final class SessionAdapter extends TypeAdapter<Session> {

private final @NotNull ILogger logger;
private final @NotNull SentryOptions options;

public SessionAdapter(final @NotNull ILogger logger) {
this.logger = Objects.requireNonNull(logger, "The Logger is required.");
public SessionAdapter(final @NotNull SentryOptions options) {
this.options = Objects.requireNonNull(options, "The SentryOptions is required.");
}

@Override
Expand Down Expand Up @@ -137,7 +137,7 @@ public Session read(JsonReader reader) throws IOException {
sidStr = reader.nextString();
sid = UUID.fromString(sidStr);
} catch (IllegalArgumentException e) {
logger.log(SentryLevel.ERROR, "%s sid is not valid.", sidStr);
options.getLogger().log(SentryLevel.ERROR, "%s sid is not valid.", sidStr);
}
break;
case "did":
Expand All @@ -155,7 +155,7 @@ public Session read(JsonReader reader) throws IOException {
statusStr = StringUtils.capitalize(reader.nextString());
status = Session.State.valueOf(statusStr);
} catch (IllegalArgumentException e) {
logger.log(SentryLevel.ERROR, "%s status is not valid.", statusStr);
options.getLogger().log(SentryLevel.ERROR, "%s status is not valid.", statusStr);
}
break;
case "errors":
Expand Down Expand Up @@ -204,7 +204,9 @@ public Session read(JsonReader reader) throws IOException {
reader.endObject();

if (status == null || started == null || release == null || release.isEmpty()) {
logger.log(SentryLevel.ERROR, "Session is gonna be dropped due to invalid fields.");
options
.getLogger()
.log(SentryLevel.ERROR, "Session is gonna be dropped due to invalid fields.");
return null;
}

Expand All @@ -229,7 +231,7 @@ public Session read(JsonReader reader) throws IOException {
try {
return DateUtils.getDateTime(timestamp);
} catch (IllegalArgumentException e) {
logger.log(SentryLevel.ERROR, e, "Error converting session (%s) field.", field);
options.getLogger().log(SentryLevel.ERROR, e, "Error converting session (%s) field.", field);
}
return null;
}
Expand Down
Loading

0 comments on commit 95a25f4

Please sign in to comment.