Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: logback error show in error reporting console #43

Merged
merged 1 commit into from Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -274,11 +278,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