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 org.apache.logging.log4j.core.impl.Log4jLogEvent;
23  import org.apache.logging.log4j.core.jackson.JsonConstants;
24  import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
25  import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;
26  import org.apache.logging.log4j.core.jackson.XmlConstants;
27  
28  import com.fasterxml.jackson.core.PrettyPrinter;
29  import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
30  import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
31  import com.fasterxml.jackson.databind.ObjectMapper;
32  import com.fasterxml.jackson.databind.ObjectWriter;
33  import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
34  import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
35  import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
36  
37  abstract class JacksonFactory {
38  
39      static class JSON extends JacksonFactory {
40  
41          @Override
42          protected String getPropertNameForContextMap() {
43              return JsonConstants.ELT_CONTEXT_MAP;
44          }
45  
46          @Override
47          protected String getPropertNameForSource() {
48              return JsonConstants.ELT_SOURCE;
49          }
50  
51          @Override
52          protected String getPropertNameForNanoTime() {
53              return JsonConstants.ELT_NANO_TIME;
54          }
55  
56          @Override
57          protected PrettyPrinter newCompactPrinter() {
58              return new MinimalPrettyPrinter();
59          }
60  
61          @Override
62          protected ObjectMapper newObjectMapper() {
63              return new Log4jJsonObjectMapper();
64          }
65  
66          @Override
67          protected PrettyPrinter newPrettyPrinter() {
68              return new DefaultPrettyPrinter();
69          }
70      }
71  
72      static class XML extends JacksonFactory {
73  
74          @Override
75          protected String getPropertNameForContextMap() {
76              return XmlConstants.ELT_CONTEXT_MAP;
77          }
78  
79          @Override
80          protected String getPropertNameForSource() {
81              return XmlConstants.ELT_SOURCE;
82          }
83  
84          @Override
85          protected String getPropertNameForNanoTime() {
86              return JsonConstants.ELT_NANO_TIME;
87          }
88  
89          @Override
90          protected PrettyPrinter newCompactPrinter() {
91              // Yes, null is the proper answer.
92              return null;
93          }
94  
95          @Override
96          protected ObjectMapper newObjectMapper() {
97              return new Log4jXmlObjectMapper();
98          }
99  
100         @Override
101         protected PrettyPrinter newPrettyPrinter() {
102             return new DefaultXmlPrettyPrinter();
103         }
104     }
105 
106     abstract protected String getPropertNameForContextMap();
107 
108     abstract protected String getPropertNameForSource();
109     
110     abstract protected String getPropertNameForNanoTime();
111 
112     abstract protected PrettyPrinter newCompactPrinter();
113 
114     abstract protected ObjectMapper newObjectMapper();
115 
116     abstract protected PrettyPrinter newPrettyPrinter();
117 
118     ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact) {
119         final SimpleFilterProvider filters = new SimpleFilterProvider();
120         final Set<String> except = new HashSet<>(2);
121         if (!locationInfo) {
122             except.add(this.getPropertNameForSource());
123         }
124         if (!properties) {
125             except.add(this.getPropertNameForContextMap());
126         }
127         except.add(this.getPropertNameForNanoTime());
128         filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
129         final ObjectWriter writer = this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
130         return writer.with(filters);
131     }
132 
133 }