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;
15
16 import java.io.IOException;
17
18 import net.logstash.logback.pattern.AbstractJsonPatternParser;
19 import net.logstash.logback.pattern.NodeWriter;
20 import ch.qos.logback.access.spi.IAccessEvent;
21 import ch.qos.logback.classic.spi.ILoggingEvent;
22 import ch.qos.logback.core.spi.DeferredProcessingAware;
23
24 import com.fasterxml.jackson.core.JsonFactory;
25 import com.fasterxml.jackson.core.JsonGenerator;
26
27 /**
28 * Transforms an string containing patterns understood by PatternLayouts into JSON output.
29 * Delegates most of the work to the {@link AbstractJsonPatternParser} that is to
30 * parse the pattern specified.
31 * Subclasses must implement {@link #createParser()} method so it returns parser valid for a specified event class.
32 *
33 * @param <Event> type of event ({@link ILoggingEvent} or {@link IAccessEvent}).
34 *
35 * @author <a href="mailto:dimas@dataart.com">Dmitry Andrianov</a>
36 */
37 public abstract class AbstractPatternJsonProvider<Event extends DeferredProcessingAware>
38 extends AbstractJsonProvider<Event> implements JsonFactoryAware {
39
40 private NodeWriter<Event> nodeWriter;
41
42 private String pattern;
43
44 protected JsonFactory jsonFactory;
45
46 /**
47 * When true, fields whose values are considered empty ({@link AbstractJsonPatternParser#isEmptyValue(Object)}})
48 * will be omitted from json output.
49 */
50 private boolean omitEmptyFields;
51
52 @Override
53 public void writeTo(JsonGenerator generator, Event event) throws IOException {
54 if (nodeWriter != null) {
55 nodeWriter.write(generator, event);
56 }
57 }
58
59 protected abstract AbstractJsonPatternParser<Event> createParser();
60
61 public String getPattern() {
62 return pattern;
63 }
64
65 public void setPattern(final String pattern) {
66 this.pattern = pattern;
67 parse();
68 }
69
70 @Override
71 public void setJsonFactory(JsonFactory jsonFactory) {
72 this.jsonFactory = jsonFactory;
73 parse();
74 }
75
76 /**
77 * Parses the pattern into a {@link NodeWriter}.
78 * We do this when the properties are set instead of on {@link #start()},
79 * because {@link #start()} is called by logstash's xml parser
80 * before the Formatter has had an opportunity to set the jsonFactory.
81 */
82 private void parse() {
83 if (pattern != null && jsonFactory != null) {
84 AbstractJsonPatternParser<Event> parser = createParser();
85 parser.setOmitEmptyFields(omitEmptyFields);
86 nodeWriter = parser.parse(pattern);
87 }
88 }
89
90 /**
91 * When true, fields whose values are considered empty ({@link AbstractJsonPatternParser#isEmptyValue(Object)}})
92 * will be omitted from json output.
93 */
94 public boolean isOmitEmptyFields() {
95 return omitEmptyFields;
96 }
97
98 /**
99 * When true, fields whose values are considered empty ({@link AbstractJsonPatternParser#isEmptyValue(Object)}})
100 * will be omitted from json output.
101 */
102 public void setOmitEmptyFields(boolean omitEmptyFields) {
103 this.omitEmptyFields = omitEmptyFields;
104 }
105
106
107 }
108