org.apache.juli.logging.net.logstash.logback.composite.loggingevent.KeyValuePairsJsonProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tomcat10-slf4j-logback Show documentation
Show all versions of tomcat10-slf4j-logback Show documentation
Tomcat10 Slf4j Logback Integration
/*
* Copyright 2013-2023 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
*
* 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 org.apache.juli.logging.net.logstash.logback.composite.loggingevent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.juli.logging.net.logstash.logback.composite.AbstractFieldJsonProvider;
import org.apache.juli.logging.net.logstash.logback.composite.FieldNamesAware;
import org.apache.juli.logging.net.logstash.logback.fieldnames.LogstashFieldNames;
import org.apache.juli.logging.ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.juli.logging.com.fasterxml.jackson.core.JsonGenerator;
import org.apache.juli.logging.org.slf4j.event.KeyValuePair;
/**
* Includes key value pairs added from slf4j's fluent api in the output according to
* {@link #includeKeyNames} and {@link #excludeKeyNames}.
*
* There are three valid combinations of {@link #includeKeyNames}
* and {@link #excludeKeyNames}:
*
*
* - When {@link #includeKeyNames} and {@link #excludeKeyNames}
* are both empty, then all entries will be included.
* - When {@link #includeKeyNames} is not empty and
* {@link #excludeKeyNames} is empty, then only those entries
* with key names in {@link #includeKeyNames} will be included.
* - When {@link #includeKeyNames} is empty and
* {@link #excludeKeyNames} is not empty, then all entries except those
* with key names in {@link #excludeKeyNames} will be included.
*
*
* It is a configuration error for both {@link #includeKeyNames}
* and {@link #excludeKeyNames} to be not empty.
*
* By default, for each key value pair, the key is output as the field name.
* This can be changed by specifying an explicit field name to use for a ke
* via {@link #addKeyFieldName(String)}
*
* If the fieldName is set, then the pairs will be written
* to that field as a subobject.
* Otherwise, the pairs are written inline.
*/
public class KeyValuePairsJsonProvider extends AbstractFieldJsonProvider implements FieldNamesAware {
/**
* See {@link KeyValuePairsJsonProvider}.
*/
private List includeKeyNames = new ArrayList<>();
/**
* See {@link KeyValuePairsJsonProvider}.
*/
private List excludeKeyNames = new ArrayList<>();
private final Map keyFieldNames = new HashMap<>();
@Override
public void start() {
if (!this.includeKeyNames.isEmpty() && !this.excludeKeyNames.isEmpty()) {
addError("Both includeKeyNames and excludeKeyNames are not empty. Only one is allowed to be not empty.");
}
super.start();
}
@Override
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
List keyValuePairs = event.getKeyValuePairs();
if (keyValuePairs == null || keyValuePairs.isEmpty()) {
return;
}
String fieldName = getFieldName();
if (fieldName != null) {
generator.writeObjectFieldStart(getFieldName());
}
for (KeyValuePair keyValuePair : keyValuePairs) {
if (keyValuePair.key != null && keyValuePair.value != null
&& (includeKeyNames.isEmpty() || includeKeyNames.contains(keyValuePair.key))
&& (excludeKeyNames.isEmpty() || !excludeKeyNames.contains(keyValuePair.key))) {
String key = keyFieldNames.get(keyValuePair.key);
if (key == null) {
key = keyValuePair.key;
}
generator.writeFieldName(key);
generator.writeObject(keyValuePair.value);
}
}
if (fieldName != null) {
generator.writeEndObject();
}
}
@Override
public void setFieldNames(LogstashFieldNames fieldNames) {
setFieldName(fieldNames.getKeyValuePair());
}
public List getIncludeKeyNames() {
return Collections.unmodifiableList(includeKeyNames);
}
public void addIncludeKeyName(String includedKeyName) {
this.includeKeyNames.add(includedKeyName);
}
public void setIncludeKeyNames(List includeKeyNames) {
this.includeKeyNames = new ArrayList<>(includeKeyNames);
}
public List getExcludeKeyNames() {
return Collections.unmodifiableList(excludeKeyNames);
}
public void addExcludeKeyName(String excludedKeyName) {
this.excludeKeyNames.add(excludedKeyName);
}
public void setExcludeKeyNames(List excludeKeyNames) {
this.excludeKeyNames = new ArrayList<>(excludeKeyNames);
}
public Map getKeyFieldNames() {
return keyFieldNames;
}
/**
* Adds the given keyFieldName entry in the form keyName=fieldName
* to use an alternative field name for an KeyValuePair key.
*
* @param keyFieldName a string in the form kvpKeyName=fieldName that identifies what field name to use for a specific KeyValuePair key.
*/
public void addKeyFieldName(String keyFieldName) {
String[] split = keyFieldName.split("=");
if (split.length != 2) {
throw new IllegalArgumentException("keyFieldName (" + keyFieldName + ") must be in the form keyName=fieldName");
}
keyFieldNames.put(split[0], split[1]);
}
}