View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.layout;
18  
19  import java.util.HashSet;
20  import java.util.Set;
21  
22  import javax.xml.stream.XMLStreamException;
23  
24  import org.apache.logging.log4j.core.impl.Log4jLogEvent;
25  import org.apache.logging.log4j.core.jackson.JsonConstants;
26  import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
27  import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
28  import org.apache.logging.log4j.core.jackson.Log4jYamlObjectMapper;
29  import org.apache.logging.log4j.core.jackson.XmlConstants;
30  import org.codehaus.stax2.XMLStreamWriter2;
31  
32  import com.fasterxml.jackson.core.PrettyPrinter;
33  import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
34  import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
35  import com.fasterxml.jackson.databind.ObjectMapper;
36  import com.fasterxml.jackson.databind.ObjectWriter;
37  import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
38  import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
39  import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
40  
41  abstract class JacksonFactory {
42  
43      static class JSON extends JacksonFactory {
44  
45          private final boolean encodeThreadContextAsList;
46  
47          public JSON(final boolean encodeThreadContextAsList) {
48              this.encodeThreadContextAsList = encodeThreadContextAsList;
49          }
50  
51          @Override
52          protected String getPropertNameForContextMap() {
53              return JsonConstants.ELT_CONTEXT_MAP;
54          }
55  
56          @Override
57          protected String getPropertNameForSource() {
58              return JsonConstants.ELT_SOURCE;
59          }
60  
61          @Override
62          protected String getPropertNameForNanoTime() {
63              return JsonConstants.ELT_NANO_TIME;
64          }
65  
66          @Override
67          protected PrettyPrinter newCompactPrinter() {
68              return new MinimalPrettyPrinter();
69          }
70  
71          @Override
72          protected ObjectMapper newObjectMapper() {
73              return new Log4jJsonObjectMapper(encodeThreadContextAsList);
74          }
75  
76          @Override
77          protected PrettyPrinter newPrettyPrinter() {
78              return new DefaultPrettyPrinter();
79          }
80      }
81  
82      static class XML extends JacksonFactory {
83  
84          static final int DEFAULT_INDENT = 1;
85  
86          @Override
87          protected String getPropertNameForContextMap() {
88              return XmlConstants.ELT_CONTEXT_MAP;
89          }
90  
91          @Override
92          protected String getPropertNameForSource() {
93              return XmlConstants.ELT_SOURCE;
94          }
95  
96          @Override
97          protected String getPropertNameForNanoTime() {
98              return JsonConstants.ELT_NANO_TIME;
99          }
100 
101         @Override
102         protected PrettyPrinter newCompactPrinter() {
103             // Yes, null is the proper answer.
104             return null;
105         }
106 
107         @Override
108         protected ObjectMapper newObjectMapper() {
109             return new Log4jXmlObjectMapper();
110         }
111 
112         @Override
113         protected PrettyPrinter newPrettyPrinter() {
114             return new Log4jXmlPrettyPrinter(DEFAULT_INDENT);
115         }
116     }
117 
118     static class YAML extends JacksonFactory {
119 
120         @Override
121         protected String getPropertNameForContextMap() {
122             return JsonConstants.ELT_CONTEXT_MAP;
123         }
124 
125         @Override
126         protected String getPropertNameForSource() {
127             return JsonConstants.ELT_SOURCE;
128         }
129 
130         @Override
131         protected String getPropertNameForNanoTime() {
132             return JsonConstants.ELT_NANO_TIME;
133         }
134 
135         @Override
136         protected PrettyPrinter newCompactPrinter() {
137             return new MinimalPrettyPrinter();
138         }
139 
140         @Override
141         protected ObjectMapper newObjectMapper() {
142             return new Log4jYamlObjectMapper(false);
143         }
144 
145         @Override
146         protected PrettyPrinter newPrettyPrinter() {
147             return new DefaultPrettyPrinter();
148         }
149     }
150 
151     /**
152      * When <Event>s are written into a XML file; the "Event" object is not the root element, but an element named
153      * <Events> created using {@link XmlLayout#getHeader()} and {@link XmlLayout#getFooter()} methods.
154      * <p>
155      * {@link com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter} is used to print the Event object into
156      * XML; hence it assumes &lt;Event&gt; tag as the root element, so it prints the &lt;Event&gt; tag without any
157      * indentation. To add an indentation to the &lt;Event&gt; tag; hence an additional indentation for any
158      * sub-elements, this class is written. As an additional task, to avoid the blank line printed after the ending
159      * &lt;/Event&gt; tag, {@link #writePrologLinefeed(XMLStreamWriter2)} method is also overridden.
160      * </p>
161      */
162     static class Log4jXmlPrettyPrinter extends DefaultXmlPrettyPrinter {
163 
164         private static final long serialVersionUID = 1L;
165 
166         Log4jXmlPrettyPrinter(final int nesting) {
167             _nesting = nesting;
168         }
169 
170         @Override
171         public void writePrologLinefeed(final XMLStreamWriter2 sw) throws XMLStreamException {
172             // nothing
173         }
174 
175         /**
176          * Sets the nesting level to 1 rather than 0, so the "Event" tag will get indentation of next level below root.
177          */
178         @Override
179         public DefaultXmlPrettyPrinter createInstance() {
180             return new Log4jXmlPrettyPrinter(XML.DEFAULT_INDENT);
181         }
182 
183     }
184 
185     abstract protected String getPropertNameForContextMap();
186 
187     abstract protected String getPropertNameForSource();
188 
189     abstract protected String getPropertNameForNanoTime();
190 
191     abstract protected PrettyPrinter newCompactPrinter();
192 
193     abstract protected ObjectMapper newObjectMapper();
194 
195     abstract protected PrettyPrinter newPrettyPrinter();
196 
197     ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact) {
198         final SimpleFilterProvider filters = new SimpleFilterProvider();
199         final Set<String> except = new HashSet<>(2);
200         if (!locationInfo) {
201             except.add(this.getPropertNameForSource());
202         }
203         if (!properties) {
204             except.add(this.getPropertNameForContextMap());
205         }
206         except.add(this.getPropertNameForNanoTime());
207         filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
208         final ObjectWriter writer = this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
209         return writer.with(filters);
210     }
211 
212 }