com.google.cloud.logging.GoogleCloudLoggingV2Layout Maven / Gradle / Ivy
/**
* Copyright (C) 2016 - Ankur Chauhan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.logging;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.contrib.json.JsonLayoutBase;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Google cloud logging v2 json layout
*/
public class GoogleCloudLoggingV2Layout extends JsonLayoutBase {
private static final String TRACE_ID_FIELD_KEY = "logging.googleapis.com/trace";
private static final String SPAN_ID_FIELD_KEY = "logging.googleapis.com/spanId";
private final ThrowableProxyConverter tpc;
private String serviceName;
private String serviceVersion;
private boolean addTraceFields;
private boolean addHttpRequestFields;
public String getServiceName() {
return serviceName;
}
public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}
public String getServiceVersion() {
return serviceVersion;
}
public void setServiceVersion(String serviceVersion) {
this.serviceVersion = serviceVersion;
}
public boolean getAddTraceFields() {
return addTraceFields;
}
public void setAddTraceFields(boolean addTraceFields) {
this.addTraceFields = addTraceFields;
}
public boolean getAddHttpRequestFields() {
return addHttpRequestFields;
}
public void setAddHttpRequestFields(boolean addHttpRequestFields) {
this.addHttpRequestFields = addHttpRequestFields;
}
public GoogleCloudLoggingV2Layout() {
this("default", "default", true, true);
}
public GoogleCloudLoggingV2Layout(String serviceName, String serviceVersion, boolean addTraceFields, boolean addHttpRequestFields) {
this.serviceName = serviceName;
this.serviceVersion = serviceVersion;
this.addTraceFields = addTraceFields;
this.addHttpRequestFields = addHttpRequestFields;
tpc = new ThrowableProxyConverter();
tpc.setOptionList(Collections.singletonList("full"));
}
@Override
public void start() {
tpc.start();
super.start();
}
@Override
public void stop() {
tpc.stop();
super.stop();
}
@Override
protected Map toJsonMap(ILoggingEvent event) {
Map