1 /**
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  *      http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */

14 package net.logstash.logback.composite.loggingevent;
15
16 import java.io.IOException;
17
18 import com.fasterxml.jackson.core.JsonGenerator;
19
20 import ch.qos.logback.classic.pattern.ExtendedThrowableProxyConverter;
21 import ch.qos.logback.classic.pattern.ThrowableHandlingConverter;
22 import ch.qos.logback.classic.spi.ILoggingEvent;
23 import ch.qos.logback.classic.spi.IThrowableProxy;
24 import net.logstash.logback.composite.AbstractFieldJsonProvider;
25 import net.logstash.logback.composite.FieldNamesAware;
26 import net.logstash.logback.composite.JsonWritingUtils;
27 import net.logstash.logback.fieldnames.LogstashFieldNames;
28
29 public class StackTraceJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> implements FieldNamesAware<LogstashFieldNames> {
30
31     public static final String FIELD_STACK_TRACE = "stack_trace";
32
33     /**
34      * Used to format throwables as Strings.
35      * 
36      * Uses an {@link ExtendedThrowableProxyConverter} from logstash by default.
37      * 
38      * Consider using a
39      * {@link net.logstash.logback.stacktrace.ShortenedThrowableConverter ShortenedThrowableConverter}
40      * for more customization options. 
41      */

42     private ThrowableHandlingConverter throwableConverter = new ExtendedThrowableProxyConverter();
43     
44     public StackTraceJsonProvider() {
45         setFieldName(FIELD_STACK_TRACE);
46     }
47     
48     @Override
49     public void start() {
50         this.throwableConverter.start();
51         super.start();
52     }
53     
54     @Override
55     public void stop() {
56         this.throwableConverter.stop();
57         super.stop();
58     }
59     
60     @Override
61     public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
62         IThrowableProxy throwableProxy = event.getThrowableProxy();
63         if (throwableProxy != null) {
64             JsonWritingUtils.writeStringField(generator, getFieldName(), throwableConverter.convert(event));
65         }
66     }
67     
68     @Override
69     public void setFieldNames(LogstashFieldNames fieldNames) {
70         setFieldName(fieldNames.getStackTrace());
71     }
72     
73     public ThrowableHandlingConverter getThrowableConverter() {
74         return throwableConverter;
75     }
76     public void setThrowableConverter(ThrowableHandlingConverter throwableConverter) {
77         this.throwableConverter = throwableConverter;
78     }
79 }
80