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 PrettyPrinter newCompactPrinter() {
53              return new MinimalPrettyPrinter();
54          }
55  
56          @Override
57          protected ObjectMapper newObjectMapper() {
58              return new Log4jJsonObjectMapper();
59          }
60  
61          @Override
62          protected PrettyPrinter newPrettyPrinter() {
63              return new DefaultPrettyPrinter();
64          }
65      }
66  
67      static class XML extends JacksonFactory {
68  
69          @Override
70          protected String getPropertNameForContextMap() {
71              return XmlConstants.ELT_CONTEXT_MAP;
72          }
73  
74          @Override
75          protected String getPropertNameForSource() {
76              return XmlConstants.ELT_SOURCE;
77          }
78  
79          @Override
80          protected PrettyPrinter newCompactPrinter() {
81              // Yes, null is the proper answer.
82              return null;
83          }
84  
85          @Override
86          protected ObjectMapper newObjectMapper() {
87              return new Log4jXmlObjectMapper();
88          }
89  
90          @Override
91          protected PrettyPrinter newPrettyPrinter() {
92              return new DefaultXmlPrettyPrinter();
93          }
94      }
95  
96      abstract protected String getPropertNameForContextMap();
97  
98      abstract protected String getPropertNameForSource();
99  
100     abstract protected PrettyPrinter newCompactPrinter();
101 
102     abstract protected ObjectMapper newObjectMapper();
103 
104     abstract protected PrettyPrinter newPrettyPrinter();
105 
106     ObjectWriter newWriter(final boolean locationInfo, final boolean properties, final boolean compact) {
107         final SimpleFilterProvider filters = new SimpleFilterProvider();
108         final Set<String> except = new HashSet<String>(2);
109         if (!locationInfo) {
110             except.add(this.getPropertNameForSource());
111         }
112         if (!properties) {
113             except.add(this.getPropertNameForContextMap());
114         }
115         filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
116         final ObjectWriter writer = this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
117         return writer.with(filters);
118     }
119 
120 }