Skip to content

Commit

Permalink
fix: logback error show in error reporting console (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
athakor committed Apr 2, 2020
1 parent 494a642 commit 1baab89
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Expand Up @@ -39,8 +39,10 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -72,6 +74,8 @@ public class LoggingAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
private static final String LEVEL_NAME_KEY = "levelName";
private static final String LEVEL_VALUE_KEY = "levelValue";
private static final String LOGGER_NAME_KEY = "loggerName";
private static final String TYPE =
"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent";
private static final List<LoggingEventEnhancer> DEFAULT_LOGGING_EVENT_ENHANCERS =
ImmutableList.<LoggingEventEnhancer>of(new MDCEventEnhancer());

Expand Down Expand Up @@ -284,11 +288,17 @@ private LogEntry logEntryFor(ILoggingEvent e) {
writeStack(e.getThrowableProxy(), "", payload);

Level level = e.getLevel();
Severity severity = severityFor(level);

Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("message", payload.toString().trim());
if (severity == Severity.ERROR) {
jsonContent.put("@type", TYPE);
}
LogEntry.Builder builder =
LogEntry.newBuilder(Payload.StringPayload.of(payload.toString().trim()))
LogEntry.newBuilder(Payload.JsonPayload.of(jsonContent))
.setTimestamp(e.getTimeStamp())
.setSeverity(severityFor(level));

.setSeverity(severity);
builder
.addLabel(LEVEL_NAME_KEY, level.toString())
.addLabel(LEVEL_VALUE_KEY, String.valueOf(level.toInt()))
Expand Down
Expand Up @@ -34,9 +34,10 @@
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.WriteOption;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Payload.StringPayload;
import com.google.cloud.logging.Payload.JsonPayload;
import com.google.cloud.logging.Severity;
import com.google.common.collect.ImmutableMap;
import java.util.HashMap;
import java.util.Map;
import org.easymock.Capture;
import org.easymock.EasyMock;
Expand Down Expand Up @@ -82,8 +83,11 @@ public void setUp() {

@Test
public void testFlushLevelConfigUpdatesLoggingFlushSeverity() {
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("message", "this is a test");
JsonPayload payload = JsonPayload.of(jsonContent);
LogEntry logEntry =
LogEntry.newBuilder(StringPayload.of("this is a test"))
LogEntry.newBuilder(payload)
.setTimestamp(100000L)
.setSeverity(Severity.WARNING)
.setLabels(
Expand All @@ -110,8 +114,14 @@ public void testFlushLevelConfigUpdatesLoggingFlushSeverity() {

@Test
public void testFilterLogsOnlyLogsAtOrAboveLogLevel() {
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("message", "this is a test");
jsonContent.put(
"@type",
"type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent");
JsonPayload payload = JsonPayload.of(jsonContent);
LogEntry logEntry =
LogEntry.newBuilder(StringPayload.of("this is a test"))
LogEntry.newBuilder(payload)
.setTimestamp(100000L)
.setSeverity(Severity.ERROR)
.setLabels(
Expand Down Expand Up @@ -145,8 +155,11 @@ public void testFilterLogsOnlyLogsAtOrAboveLogLevel() {

@Test
public void testEnhancersAddCorrectLabelsToLogEntries() {
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("message", "this is a test");
JsonPayload payload = JsonPayload.of(jsonContent);
LogEntry logEntry =
LogEntry.newBuilder(StringPayload.of("this is a test"))
LogEntry.newBuilder(payload)
.setTimestamp(100000L)
.setSeverity(Severity.WARNING)
.setLabels(
Expand Down Expand Up @@ -194,8 +207,11 @@ public void testDefaultWriteOptionsHasExpectedDefaults() {

@Test
public void testMdcValuesAreConvertedToLabels() {
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("message", "this is a test");
JsonPayload payload = JsonPayload.of(jsonContent);
LogEntry logEntry =
LogEntry.newBuilder(StringPayload.of("this is a test"))
LogEntry.newBuilder(payload)
.setTimestamp(100000L)
.setSeverity(Severity.INFO)
.setLabels(
Expand Down

0 comments on commit 1baab89

Please sign in to comment.