All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.spring.logging.extensions.LogstashLoggingEventEnhancer Maven / Gradle / Ivy

There is a newer version: 5.8.0
Show newest version
/*
 * Copyright 2017-2019 the original author or authors.
 *
 * 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
 *
 *      https://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.spring.logging.extensions;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.logback.LoggingEventEnhancer;
import com.google.cloud.spring.logging.JsonLoggingEventEnhancer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import net.logstash.logback.marker.ObjectAppendingMarker;
import org.slf4j.Marker;

/**
 * Logging enhancer which adds Logstash markers to Logging API calls and JSON log entries.
 *
 * 

Supported Markers: - ObjectAppendingMarker: key-value pairs are added to the log entries. * *

This can be used by specifying this class in a {@code } element for your * {@link com.google.cloud.logging.logback.LoggingAppender} or {@link * com.google.cloud.spring.logging.StackdriverJsonLayout} definitions in logback.xml. */ public class LogstashLoggingEventEnhancer implements LoggingEventEnhancer, JsonLoggingEventEnhancer { @Override public void enhanceLogEntry(LogEntry.Builder builder, ILoggingEvent event) { addLogstashMarkerIfNecessary( getFirstMarker(event.getMarkerList()), marker -> builder.addLabel(marker.getFieldName(), marker.getFieldValue().toString())); } @Override public void enhanceJsonLogEntry(Map jsonMap, ILoggingEvent event) { addLogstashMarkerIfNecessary( getFirstMarker(event.getMarkerList()), marker -> jsonMap.put(marker.getFieldName(), marker.getFieldValue())); } private Marker getFirstMarker(List markers) { return markers == null || markers.isEmpty() ? null : markers.get(0); } private void addLogstashMarkerIfNecessary( Marker marker, Consumer markerAdderFunction) { if (marker == null) { return; } if (marker instanceof ObjectAppendingMarker objectAppendingMarker) { markerAdderFunction.accept(objectAppendingMarker); } if (marker.hasReferences()) { for (Iterator i = marker.iterator(); i.hasNext(); ) { Marker next = (Marker) i.next(); addLogstashMarkerIfNecessary(next, markerAdderFunction); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy