1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.builders;
18
19 import org.apache.log4j.bridge.FilterAdapter;
20 import org.apache.log4j.bridge.FilterWrapper;
21 import org.apache.log4j.helpers.OptionConverter;
22 import org.apache.log4j.spi.Filter;
23 import org.apache.logging.log4j.Level;
24 import org.apache.logging.log4j.Logger;
25 import org.apache.logging.log4j.core.filter.CompositeFilter;
26 import org.apache.logging.log4j.core.filter.ThresholdFilter;
27 import org.apache.logging.log4j.core.lookup.ConfigurationStrSubstitutor;
28 import org.apache.logging.log4j.core.lookup.StrSubstitutor;
29 import org.apache.logging.log4j.status.StatusLogger;
30
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Properties;
36
37
38
39
40 public abstract class AbstractBuilder {
41
42 private static Logger LOGGER = StatusLogger.getLogger();
43 protected static final String FILE_PARAM = "File";
44 protected static final String APPEND_PARAM = "Append";
45 protected static final String BUFFERED_IO_PARAM = "BufferedIO";
46 protected static final String BUFFER_SIZE_PARAM = "BufferSize";
47 protected static final String MAX_SIZE_PARAM = "MaxFileSize";
48 protected static final String MAX_BACKUP_INDEX = "MaxBackupIndex";
49 protected static final String RELATIVE = "RELATIVE";
50
51 private final String prefix;
52 private final Properties props;
53 private final StrSubstitutor strSubstitutor;
54
55 public AbstractBuilder() {
56 this.prefix = null;
57 this.props = new Properties();
58 strSubstitutor = new ConfigurationStrSubstitutor(System.getProperties());
59 }
60
61 public AbstractBuilder(String prefix, Properties props) {
62 this.prefix = prefix + ".";
63 this.props = props;
64 Map<String, String> map = new HashMap<>();
65 System.getProperties().forEach((k, v) -> map.put(k.toString(), v.toString()));
66 props.forEach((k, v) -> map.put(k.toString(), v.toString()));
67 strSubstitutor = new ConfigurationStrSubstitutor(map);
68 }
69
70 public String getProperty(String key) {
71 return strSubstitutor.replace(props.getProperty(prefix + key));
72 }
73
74 public String getProperty(String key, String defaultValue) {
75 return strSubstitutor.replace(props.getProperty(prefix + key, defaultValue));
76 }
77
78 public boolean getBooleanProperty(String key) {
79 return Boolean.parseBoolean(strSubstitutor.replace(props.getProperty(prefix + key, Boolean.FALSE.toString())));
80 }
81
82 public int getIntegerProperty(String key, int defaultValue) {
83 String value = getProperty(key);
84 try {
85 if (value != null) {
86 return Integer.parseInt(value);
87 }
88 } catch (Exception ex) {
89 LOGGER.warn("Error converting value {} of {} to an integer: {}", value, key, ex.getMessage());
90 }
91 return defaultValue;
92 }
93
94 public Properties getProperties() {
95 return props;
96 }
97
98
99 protected org.apache.logging.log4j.core.Filter buildFilters(String level, Filter filter) {
100 if (level != null && filter != null) {
101 List<org.apache.logging.log4j.core.Filter> filterList = new ArrayList<>();
102 org.apache.logging.log4j.core.Filter thresholdFilter =
103 ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE),
104 org.apache.logging.log4j.core.Filter.Result.NEUTRAL,
105 org.apache.logging.log4j.core.Filter.Result.DENY);
106 filterList.add(thresholdFilter);
107 Filter f = filter;
108 while (f != null) {
109 if (filter instanceof FilterWrapper) {
110 filterList.add(((FilterWrapper) f).getFilter());
111 } else {
112 filterList.add(new FilterAdapter(f));
113 }
114 f = f.next;
115 }
116 return CompositeFilter.createFilters(filterList.toArray(new org.apache.logging.log4j.core.Filter[0]));
117 } else if (level != null) {
118 return ThresholdFilter.createFilter(OptionConverter.convertLevel(level, Level.TRACE),
119 org.apache.logging.log4j.core.Filter.Result.NEUTRAL,
120 org.apache.logging.log4j.core.Filter.Result.DENY);
121 } else if (filter != null) {
122 if (filter instanceof FilterWrapper) {
123 return ((FilterWrapper) filter).getFilter();
124 }
125 return new FilterAdapter(filter);
126 }
127 return null;
128 }
129 }