1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.async;
18
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.apache.logging.log4j.Level;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.apache.logging.log4j.core.Filter;
26 import org.apache.logging.log4j.core.LogEvent;
27 import org.apache.logging.log4j.core.config.AppenderRef;
28 import org.apache.logging.log4j.core.config.Configuration;
29 import org.apache.logging.log4j.core.config.LoggerConfig;
30 import org.apache.logging.log4j.core.config.Property;
31 import org.apache.logging.log4j.core.config.plugins.Plugin;
32 import org.apache.logging.log4j.core.config.plugins.PluginAttr;
33 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
34 import org.apache.logging.log4j.core.config.plugins.PluginElement;
35 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
36 import org.apache.logging.log4j.status.StatusLogger;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Plugin(name = "asyncLogger", category = "Core", printObject = true)
67 public class AsyncLoggerConfig extends LoggerConfig {
68
69 private static final Logger LOGGER = StatusLogger.getLogger();
70 private AsyncLoggerConfigHelper helper;
71
72
73
74
75 public AsyncLoggerConfig() {
76 super();
77 }
78
79
80
81
82
83
84
85
86 public AsyncLoggerConfig(final String name, final Level level,
87 final boolean additive) {
88 super(name, level, additive);
89 }
90
91 protected AsyncLoggerConfig(final String name,
92 final List<AppenderRef> appenders, final Filter filter,
93 final Level level, final boolean additive,
94 final Property[] properties, final Configuration config,
95 final boolean includeLocation) {
96 super(name, appenders, filter, level, additive, properties, config,
97 includeLocation);
98 }
99
100
101
102
103
104 @Override
105 protected void callAppenders(LogEvent event) {
106
107 event.getSource();
108 event.getThreadName();
109
110
111 helper.callAppendersFromAnotherThread(event);
112 }
113
114
115 void asyncCallAppenders(LogEvent event) {
116 super.callAppenders(event);
117 }
118
119 @Override
120 public void startFilter() {
121 if (helper == null) {
122 helper = new AsyncLoggerConfigHelper(this);
123 }
124 super.startFilter();
125 }
126
127 @Override
128 public void stopFilter() {
129
130
131
132
133
134
135 helper.shutdown();
136 super.stopFilter();
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 @PluginFactory
153 public static LoggerConfig createLogger(
154 @PluginAttr("additivity") final String additivity,
155 @PluginAttr("level") final String levelName,
156 @PluginAttr("name") final String loggerName,
157 @PluginAttr("includeLocation") final String includeLocation,
158 @PluginElement("appender-ref") final AppenderRef[] refs,
159 @PluginElement("properties") final Property[] properties,
160 @PluginConfiguration final Configuration config,
161 @PluginElement("filters") final Filter filter) {
162 if (loggerName == null) {
163 LOGGER.error("Loggers cannot be configured without a name");
164 return null;
165 }
166
167 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
168 Level level;
169 try {
170 level = Level.toLevel(levelName, Level.ERROR);
171 } catch (final Exception ex) {
172 LOGGER.error(
173 "Invalid Log level specified: {}. Defaulting to Error",
174 levelName);
175 level = Level.ERROR;
176 }
177 final String name = loggerName.equals("root") ? "" : loggerName;
178 final boolean additive = additivity == null ? true : Boolean
179 .parseBoolean(additivity);
180
181 return new AsyncLoggerConfig(name, appenderRefs, filter, level,
182 additive, properties, config, includeLocation(includeLocation));
183 }
184
185
186 private static boolean includeLocation(String includeLocationConfigValue) {
187 if (includeLocationConfigValue == null) {
188 return false;
189 }
190 return Boolean.parseBoolean(includeLocationConfigValue);
191 }
192
193
194
195
196 @Plugin(name = "asyncRoot", category = "Core", printObject = true)
197 public static class RootLogger extends LoggerConfig {
198
199 @PluginFactory
200 public static LoggerConfig createLogger(
201 @PluginAttr("additivity") final String additivity,
202 @PluginAttr("level") final String levelName,
203 @PluginAttr("includeLocation") final String includeLocation,
204 @PluginElement("appender-ref") final AppenderRef[] refs,
205 @PluginElement("properties") final Property[] properties,
206 @PluginConfiguration final Configuration config,
207 @PluginElement("filters") final Filter filter) {
208 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
209 Level level;
210 try {
211 level = Level.toLevel(levelName, Level.ERROR);
212 } catch (final Exception ex) {
213 LOGGER.error(
214 "Invalid Log level specified: {}. Defaulting to Error",
215 levelName);
216 level = Level.ERROR;
217 }
218 final boolean additive = additivity == null ? true : Boolean
219 .parseBoolean(additivity);
220
221 return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME,
222 appenderRefs, filter, level, additive, properties, config,
223 includeLocation(includeLocation));
224 }
225 }
226 }