这个答案包含了我需要的大多数信息:https :
//stackoverflow.com/a/39779646
我调整了答案以适合我的确切问题,并修复了一些似乎已过时的怪异输入和代码。
logback.xml:
<configuration> <appender name="STDOUT" > <enprer > <layout > <pattern>%-4relative [%thread] %-5level %logger{35} - %msg</pattern> </layout> </enprer> </appender> <root level="INFO"> <appender-ref ref="STDOUT"/> </root></configuration>GCPCloudLoggingJSONLayout:
import ch.qos.logback.classic.Level;import ch.qos.logback.classic.PatternLayout;import ch.qos.logback.classic.spi.ILoggingEvent;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import java.util.Map;import static ch.qos.logback.classic.Level.DEBUG_INT;import static ch.qos.logback.classic.Level.ERROR_INT;import static ch.qos.logback.classic.Level.INFO_INT;import static ch.qos.logback.classic.Level.TRACE_INT;import static ch.qos.logback.classic.Level.WARN_INT;public class GCPCloudLoggingJSonLayout extends PatternLayout { private static final ObjectMapper objectMapper = new ObjectMapper(); @Override public String doLayout(ILoggingEvent event) { String formattedMessage = super.doLayout(event); return doLayoutInternal(formattedMessage, event); } private String doLayoutInternal(String formattedMessage, ILoggingEvent event) { GCPCloudLoggingEvent gcpLogEvent = new GCPCloudLoggingEvent(formattedMessage, convertTimestampToGCPLogTimestamp(event.getTimeStamp()), mapLevelToGCPLevel(event.getLevel()), event.getThreadName()); try { // Add a newline so that each JSON log entry is on its own line. // Note that it is also important that the JSON log entry does not span multiple lines. return objectMapper.writevalueAsString(gcpLogEvent) + "n"; } catch (JsonProcessingException e) { return ""; } } private static GCPCloudLoggingEvent.GCPCloudLoggingTimestamp convertTimestampToGCPLogTimestamp( long millisSinceEpoch) { int nanos = ((int) (millisSinceEpoch % 1000)) * 1_000_000; // strip out just the milliseconds and convert to nanoseconds long seconds = millisSinceEpoch / 1000L; // remove the milliseconds return new GCPCloudLoggingEvent.GCPCloudLoggingTimestamp(seconds, nanos); } private static String mapLevelToGCPLevel(Level level) { switch (level.toInt()) { case TRACE_INT: return "TRACE"; case DEBUG_INT: return "DEBUG"; case INFO_INT: return "INFO"; case WARN_INT: return "WARN"; case ERROR_INT: return "ERROR"; default: return null; } } public static class GCPCloudLoggingEvent { private String message; private GCPCloudLoggingTimestamp timestamp; private String thread; private String severity; public GCPCloudLoggingEvent(String message, GCPCloudLoggingTimestamp timestamp, String severity, String thread) { super(); this.message = message; this.timestamp = timestamp; this.thread = thread; this.severity = severity; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public GCPCloudLoggingTimestamp getTimestamp() { return timestamp; } public void setTimestamp(GCPCloudLoggingTimestamp timestamp) { this.timestamp = timestamp; } public String getThread() { return thread; } public void setThread(String thread) { this.thread = thread; } public String getSeverity() { return severity; } public void setSeverity(String severity) { this.severity = severity; } public static class GCPCloudLoggingTimestamp { private long seconds; private int nanos; public GCPCloudLoggingTimestamp(long seconds, int nanos) { super(); this.seconds = seconds; this.nanos = nanos; } public long getSeconds() { return seconds; } public void setSeconds(long seconds) { this.seconds = seconds; } public int getNanos() { return nanos; } public void setNanos(int nanos) { this.nanos = nanos; } } } @Override public Map<String, String> getDefaultConverterMap() { return PatternLayout.defaultConverterMap; }}就像我之前说的那样,该代码最初是从另一个答案获得的,我只是稍微清理了一下代码以使其更适合我的用例。



