Use gson to make JSON string in proxy log formatter

This is simpler than using fasterxml.jackson.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=224583713
This commit is contained in:
jianglai 2018-12-07 15:01:23 -08:00
parent 57a53db84e
commit a85544b3f6
4 changed files with 81 additions and 97 deletions

View file

@ -79,9 +79,6 @@ dependencies {
testImplementation project(':third_party') testImplementation project(':third_party')
compile 'com.beust:jcommander:1.48' compile 'com.beust:jcommander:1.48'
maybeRuntime 'com.fasterxml.jackson.core:jackson-core:2.9.6'
maybeRuntime 'com.fasterxml.jackson.core:jackson-annotations:2.8.0'
maybeRuntime 'com.fasterxml.jackson.core:jackson-databind:2.8.5'
compile 'com.google.api-client:google-api-client:1.22.0' compile 'com.google.api-client:google-api-client:1.22.0'
maybeRuntime 'com.google.api-client:google-api-client-appengine:1.22.0' maybeRuntime 'com.google.api-client:google-api-client-appengine:1.22.0'
maybeRuntime 'com.google.api-client:google-api-client-jackson2:1.20.0' maybeRuntime 'com.google.api-client:google-api-client-jackson2:1.20.0'

View file

@ -45,9 +45,6 @@ task deployJar(type: Jar) {
dependencies { dependencies {
compile 'com.beust:jcommander:1.48' compile 'com.beust:jcommander:1.48'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.9'
compile 'com.fasterxml.jackson.core:jackson-core:2.9.6'
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.9'
compile 'com.google.api-client:google-api-client:1.27.0' compile 'com.google.api-client:google-api-client:1.27.0'
compile 'com.google.api-client:google-api-client:1.27.0' compile 'com.google.api-client:google-api-client:1.27.0'
compile 'com.google.apis:google-api-services-cloudkms:v1-rev12-1.22.0' compile 'com.google.apis:google-api-services-cloudkms:v1-rev12-1.22.0'
@ -55,6 +52,7 @@ dependencies {
compile 'com.google.apis:google-api-services-storage:v1-rev86-1.22.0' compile 'com.google.apis:google-api-services-storage:v1-rev86-1.22.0'
compile 'com.google.auto.value:auto-value-annotations:1.6.2' compile 'com.google.auto.value:auto-value-annotations:1.6.2'
compile 'com.google.code.findbugs:jsr305:3.0.2' compile 'com.google.code.findbugs:jsr305:3.0.2'
compile 'com.google.code.gson:gson:2.8.5'
compile 'com.google.dagger:dagger:2.15' compile 'com.google.dagger:dagger:2.15'
compile 'com.google.flogger:flogger:0.1' compile 'com.google.flogger:flogger:0.1'
compile 'com.google.guava:guava:27.0-jre' compile 'com.google.guava:guava:27.0-jre'

View file

@ -19,7 +19,6 @@ java_library(
]), ]),
deps = [ deps = [
"//java/google/registry/util", "//java/google/registry/util",
"//third_party/java/jackson2:jackson2-databind",
"@com_beust_jcommander", "@com_beust_jcommander",
"@com_google_api_client", "@com_google_api_client",
"@com_google_apis_google_api_services_cloudkms", "@com_google_apis_google_api_services_cloudkms",
@ -27,6 +26,7 @@ java_library(
"@com_google_apis_google_api_services_storage", "@com_google_apis_google_api_services_storage",
"@com_google_auto_value", "@com_google_auto_value",
"@com_google_code_findbugs_jsr305", "@com_google_code_findbugs_jsr305",
"@com_google_code_gson",
"@com_google_dagger", "@com_google_dagger",
"@com_google_flogger", "@com_google_flogger",
"@com_google_flogger_system_backend", "@com_google_flogger_system_backend",

View file

@ -14,10 +14,8 @@
package google.registry.proxy; package google.registry.proxy;
import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.collect.ImmutableMap;
import com.fasterxml.jackson.core.JsonProcessingException; import com.google.gson.Gson;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auto.value.AutoValue;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.logging.Formatter; import java.util.logging.Formatter;
@ -41,98 +39,89 @@ import java.util.logging.LogRecord;
*/ */
class GcpJsonFormatter extends Formatter { class GcpJsonFormatter extends Formatter {
private static final ObjectMapper MAPPER = new ObjectMapper(); /** JSON field that determines the log level. */
private static final String SEVERITY = "severity";
/**
* JSON field that stores the calling class and function when the log occurs.
*
* <p>This field is not used by Stackdriver, but it is useful and can be found when the log
* entries are expanded
*/
private static final String SOURCE = "source";
/** JSON field that contains the content, this will show up as the main entry in a log. */
private static final String MESSAGE = "message";
private static final Gson gson = new Gson();
@Override @Override
public String format(LogRecord record) { public String format(LogRecord record) {
try { // Add an extra newline before the message. Stackdriver does not show newlines correctly, and
return MAPPER.writeValueAsString(LogEvent.create(record)) + "\n"; // treats them as whitespace. If you want to see correctly formatted log message, expand the
} catch (JsonProcessingException e) { // log and look for the jsonPayload.message field. This newline makes sure that the entire
throw new RuntimeException(e); // message starts on its own line, so that indentation within the message is correct.
String message = "\n" + record.getMessage();
String severity = severityFor(record.getLevel());
// The rest is mostly lifted from java.util.logging.SimpleFormatter.
String stacktrace = "";
if (record.getThrown() != null) {
StringWriter sw = new StringWriter();
try (PrintWriter pw = new PrintWriter(sw)) {
pw.println();
record.getThrown().printStackTrace(pw);
}
stacktrace = sw.toString();
} }
String source;
if (record.getSourceClassName() != null) {
source = record.getSourceClassName();
if (record.getSourceMethodName() != null) {
source += " " + record.getSourceMethodName();
}
} else {
source = record.getLoggerName();
}
return gson.toJson(
ImmutableMap.of(SEVERITY, severity, SOURCE, source, MESSAGE, message + stacktrace))
+ '\n';
} }
@AutoValue /**
abstract static class LogEvent { * Map {@link Level} to a severity string that Stackdriver understands.
*
/** Field that determines the log level. */ * @see <a
@JsonProperty("severity") * href="https://github.com/googleapis/google-cloud-java/blob/master/google-cloud-clients/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java#L325">{@code LoggingHandler}</a>
abstract String severity(); */
private static String severityFor(Level level) {
/** switch (level.intValue()) {
* Field that stores the calling class and function when the log occurs. // FINEST
* case 300:
* <p>This field is not used by Stackdriver, but it is useful and can be found when the log return "DEBUG";
* entries are expanded // FINER
*/ case 400:
@JsonProperty("source") return "DEBUG";
abstract String source(); // FINE
case 500:
/** Field that contains the content, this will show up as the main entry in a log. */ return "DEBUG";
@JsonProperty("message") // CONFIG
abstract String message(); case 700:
return "INFO";
static LogEvent create(LogRecord record) { // INFO
// Add an extra newline before the message. Stackdriver does not show newlines correctly, and case 800:
// treats them as whitespace. If you want to see correctly formatted log message, expand the return "INFO";
// log and look for the jsonPayload.message field. This newline makes sure that the entire // WARNING
// message starts on its own line, so that indentation within the message is correct. case 900:
return "WARNING";
String message = "\n" + record.getMessage(); // SEVERE
Level level = record.getLevel(); case 1000:
// See return "ERROR";
// https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-logging/src/main/java/com/google/cloud/logging/Severity.java default:
// on how {@code Level} is mapped to severity. return "DEFAULT";
String severity;
switch (level.intValue()) {
// FINEST
case 300:
// FINER
case 400:
// FINE
case 500:
severity = "DEBUG";
break;
// CONFIG
case 700:
// INFO
case 800:
severity = "INFO";
break;
// WARNING
case 900:
severity = "WARNING";
break;
// SEVERE
case 1000:
severity = "ERROR";
break;
default:
severity = "DEFAULT";
}
// The rest is mostly lifted from java.util.logging.SimpleFormatter.
String stacktrace = "";
if (record.getThrown() != null) {
StringWriter sw = new StringWriter();
try (PrintWriter pw = new PrintWriter(sw)) {
pw.println();
record.getThrown().printStackTrace(pw);
}
stacktrace = sw.toString();
}
String source;
if (record.getSourceClassName() != null) {
source = record.getSourceClassName();
if (record.getSourceMethodName() != null) {
source += " " + record.getSourceMethodName();
}
} else {
source = record.getLoggerName();
}
return new AutoValue_GcpJsonFormatter_LogEvent(severity, source, message + stacktrace);
} }
} }
} }