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.log4j.builders.layout;
18  
19  import org.apache.log4j.Layout;
20  import org.apache.log4j.bridge.LayoutWrapper;
21  import org.apache.log4j.builders.AbstractBuilder;
22  import org.apache.log4j.builders.BooleanHolder;
23  import org.apache.log4j.builders.Holder;
24  import org.apache.log4j.config.Log4j1Configuration;
25  import org.apache.log4j.config.PropertiesConfiguration;
26  import org.apache.log4j.xml.XmlConfiguration;
27  import org.apache.logging.log4j.Logger;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.layout.PatternLayout;
30  import org.apache.logging.log4j.status.StatusLogger;
31  import org.w3c.dom.Element;
32  
33  import java.util.Properties;
34  
35  import static org.apache.log4j.builders.BuilderManager.CATEGORY;
36  import static org.apache.log4j.xml.XmlConfiguration.*;
37  
38  /**
39   * Build a Pattern Layout
40   */
41  @Plugin(name = "org.apache.log4j.TTCCLayout", category = CATEGORY)
42  public class TTCCLayoutBuilder extends AbstractBuilder implements LayoutBuilder {
43  
44      private static final Logger LOGGER = StatusLogger.getLogger();
45  
46      private static final String THREAD_PRINTING_PARAM = "ThreadPrinting";
47      private static final String CATEGORY_PREFIXING_PARAM = "CategoryPrefixing";
48      private static final String CONTEXT_PRINTING_PARAM = "ContextPrinting";
49      private static final String DATE_FORMAT_PARAM = "DateFormat";
50      private static final String TIMEZONE_FORMAT = "TimeZone";
51  
52      public TTCCLayoutBuilder() {
53      }
54  
55      public TTCCLayoutBuilder(String prefix, Properties props) {
56          super(prefix, props);
57      }
58  
59      @Override
60      public Layout parseLayout(Element layoutElement, XmlConfiguration config) {
61          final Holder<Boolean> threadPrinting = new BooleanHolder();
62          final Holder<Boolean> categoryPrefixing = new BooleanHolder();
63          final Holder<Boolean> contextPrinting = new BooleanHolder();
64          final Holder<String> dateFormat = new Holder<>();
65          final Holder<String> timezone = new Holder<>();
66          forEachElement(layoutElement.getElementsByTagName("param"), (currentElement) -> {
67              if (currentElement.getTagName().equals(PARAM_TAG)) {
68                  switch (currentElement.getAttribute(NAME_ATTR)) {
69                      case THREAD_PRINTING_PARAM:
70                          threadPrinting.set(Boolean.parseBoolean(currentElement.getAttribute(VALUE_ATTR)));
71                          break;
72                      case CATEGORY_PREFIXING_PARAM:
73                          categoryPrefixing.set(Boolean.parseBoolean(currentElement.getAttribute(VALUE_ATTR)));
74                          break;
75                      case CONTEXT_PRINTING_PARAM:
76                          contextPrinting.set(Boolean.parseBoolean(currentElement.getAttribute(VALUE_ATTR)));
77                          break;
78                      case DATE_FORMAT_PARAM:
79                          dateFormat.set(currentElement.getAttribute(VALUE_ATTR));
80                          break;
81                      case TIMEZONE_FORMAT:
82                          timezone.set(currentElement.getAttribute(VALUE_ATTR));
83                          break;
84                  }
85              }
86          });
87          return createLayout(threadPrinting.get(), categoryPrefixing.get(), contextPrinting.get(),
88                  dateFormat.get(), timezone.get(), config);
89      }
90  
91      @Override
92      public Layout parseLayout(PropertiesConfiguration config) {
93          boolean threadPrinting = getBooleanProperty(THREAD_PRINTING_PARAM);
94          boolean categoryPrefixing = getBooleanProperty(CATEGORY_PREFIXING_PARAM);
95          boolean contextPrinting = getBooleanProperty(CONTEXT_PRINTING_PARAM);
96          String dateFormat = getProperty(DATE_FORMAT_PARAM);
97          String timezone = getProperty(TIMEZONE_FORMAT);
98  
99          return createLayout(threadPrinting, categoryPrefixing, contextPrinting,
100                 dateFormat, timezone, config);
101     }
102 
103     private Layout createLayout(boolean threadPrinting, boolean categoryPrefixing, boolean contextPrinting,
104             String dateFormat, String timezone, Log4j1Configuration config) {
105         StringBuilder sb = new StringBuilder();
106         if (dateFormat != null) {
107             if (RELATIVE.equalsIgnoreCase(dateFormat)) {
108                 sb.append("%r ");
109             } else {
110                 sb.append("%d{").append(dateFormat).append("}");
111                 if (timezone != null) {
112                     sb.append("{").append(timezone).append("}");
113                 }
114                 sb.append(" ");
115             }
116         }
117         if (threadPrinting) {
118             sb.append("[%t] ");
119         }
120         sb.append("%p ");
121         if (categoryPrefixing) {
122             sb.append("%c ");
123         }
124         if (contextPrinting) {
125             sb.append("%notEmpty{%ndc }");
126         }
127         sb.append("- %m%n");
128         return new LayoutWrapper(PatternLayout.newBuilder()
129                 .withPattern(sb.toString())
130                 .withConfiguration(config)
131                 .build());
132     }
133 }