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

net.logstash.logback.composite.loggingevent.StackHashJsonProvider Maven / Gradle / Ivy

There is a newer version: 7.2.0
Show newest version
/**
 * 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 org.apache.juli.logging.ch.qos.logback.classic.spi.ILoggingEvent;
import org.apache.juli.logging.ch.qos.logback.classic.spi.IThrowableProxy;
import org.apache.juli.logging.ch.qos.logback.classic.spi.ThrowableProxy;
import com.fasterxml.jackson.core.JsonGenerator;
import net.logstash.logback.composite.AbstractFieldJsonProvider;
import net.logstash.logback.composite.JsonWritingUtils;
import net.logstash.logback.stacktrace.StackElementFilter;
import net.logstash.logback.stacktrace.StackHasher;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

/**
 * A JSON provider that adds a {@code stack_hash} Json field on a log with a stack trace
 * 

* This hash is computed using {@link StackHasher} * * @author Pierre Smeyers */ public class StackHashJsonProvider extends AbstractFieldJsonProvider { public static final String FIELD_NAME = "stack_hash"; /** * Patterns used to determine which stacktrace elements to exclude from hash computation. * * The strings being matched against are in the form "fullyQualifiedClassName.methodName" * (e.g. "java.lang.Object.toString"). */ private List excludes = new ArrayList(5); private StackHasher hasher = new StackHasher(); public StackHashJsonProvider() { setFieldName(FIELD_NAME); } @Override public void start() { if(!excludes.isEmpty()) { hasher = new StackHasher(StackElementFilter.byPattern(excludes)); } super.start(); } public void addExclude(String exclusionPattern) { excludes.add(Pattern.compile(exclusionPattern)); } /** * Set exclusion patterns as a list of coma separated patterns * @param comaSeparatedPatterns list of coma separated patterns */ public void setExclusions(String comaSeparatedPatterns) { if (comaSeparatedPatterns == null || comaSeparatedPatterns.isEmpty()) { this.excludes = new ArrayList(5); } else { setExcludes(Arrays.asList(comaSeparatedPatterns.split("\\s*\\,\\s*"))); } } public void setExcludes(List exclusionPatterns) { if (exclusionPatterns == null || exclusionPatterns.isEmpty()) { this.excludes = new ArrayList(5); } else { this.excludes = new ArrayList(exclusionPatterns.size()); for (String pattern : exclusionPatterns) { addExclude(pattern); } } } public List getExcludes() { List exclusionPatterns = new ArrayList(excludes.size()); for (Pattern pattern : excludes) { exclusionPatterns.add(pattern.pattern()); } return exclusionPatterns; } @Override public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException { IThrowableProxy throwableProxy = event.getThrowableProxy(); if (throwableProxy != null && throwableProxy instanceof ThrowableProxy) { String hash = hasher.hexHash(((ThrowableProxy)event.getThrowableProxy()).getThrowable()); JsonWritingUtils.writeStringField(generator, getFieldName(), hash); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy