net.logstash.logback.composite.loggingevent.MdcJsonProvider Maven / Gradle / Ivy
Show all versions of logstash-logback-encoder Show documentation
/**
* 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 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.slf4j.MDC;
import net.logstash.logback.composite.AbstractFieldJsonProvider;
import net.logstash.logback.composite.FieldNamesAware;
import net.logstash.logback.composite.JsonWritingUtils;
import net.logstash.logback.fieldnames.LogstashFieldNames;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.fasterxml.jackson.core.JsonGenerator;
/**
* Includes {@link MDC} properties in the JSON output according to
* {@link #includeMdcKeyNames} and {@link #excludeMdcKeyNames}.
*
* There are three valid combinations of {@link #includeMdcKeyNames}
* and {@link #excludeMdcKeyNames}:
*
*
* - When {@link #includeMdcKeyNames} and {@link #excludeMdcKeyNames}
* are both empty, then all entries will be included.
* - When {@link #includeMdcKeyNames} is not empty and
* {@link #excludeMdcKeyNames} is empty, then only those entries
* with key names in {@link #includeMdcKeyNames} will be included.
* - When {@link #includeMdcKeyNames} is empty and
* {@link #excludeMdcKeyNames} is not empty, then all entries except those
* with key names in {@link #excludeMdcKeyNames} will be included.
*
*
* It is a configuration error for both {@link #includeMdcKeyNames}
* and {@link #excludeMdcKeyNames} to be not empty.
*
*
* If the fieldName is set, then the properties will be written
* to that field as a subobject.
* Otherwise, the properties are written inline.
*/
public class MdcJsonProvider extends AbstractFieldJsonProvider implements FieldNamesAware {
/**
* See {@link MdcJsonProvider}.
*/
private List includeMdcKeyNames = new ArrayList();
/**
* See {@link MdcJsonProvider}.
*/
private List excludeMdcKeyNames = new ArrayList();
@Override
public void start() {
if (!this.includeMdcKeyNames.isEmpty() && !this.excludeMdcKeyNames.isEmpty()) {
addError("Both includeMdcKeyNames and excludeMdcKeyNames are not empty. Only one is allowed to be not empty.");
}
super.start();
}
@Override
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
Map mdcProperties = event.getMDCPropertyMap();
if (mdcProperties != null && !mdcProperties.isEmpty()) {
if (getFieldName() != null) {
generator.writeObjectFieldStart(getFieldName());
}
if (!includeMdcKeyNames.isEmpty()) {
mdcProperties = new HashMap(mdcProperties);
mdcProperties.keySet().retainAll(includeMdcKeyNames);
}
if (!excludeMdcKeyNames.isEmpty()) {
mdcProperties = new HashMap(mdcProperties);
mdcProperties.keySet().removeAll(excludeMdcKeyNames);
}
JsonWritingUtils.writeMapEntries(generator, mdcProperties);
if (getFieldName() != null) {
generator.writeEndObject();
}
}
}
@Override
public void setFieldNames(LogstashFieldNames fieldNames) {
setFieldName(fieldNames.getMdc());
}
public List getIncludeMdcKeyNames() {
return Collections.unmodifiableList(includeMdcKeyNames);
}
public void addIncludeMdcKeyName(String includedMdcKeyName) {
this.includeMdcKeyNames.add(includedMdcKeyName);
}
public void setIncludeMdcKeyNames(List includeMdcKeyNames) {
this.includeMdcKeyNames = new ArrayList(includeMdcKeyNames);
}
public List getExcludeMdcKeyNames() {
return Collections.unmodifiableList(excludeMdcKeyNames);
}
public void addExcludeMdcKeyName(String excludedMdcKeyName) {
this.excludeMdcKeyNames.add(excludedMdcKeyName);
}
public void setExcludeMdcKeyNames(List excludeMdcKeyNames) {
this.excludeMdcKeyNames = new ArrayList(excludeMdcKeyNames);
}
}