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.core.Filter;
25 import org.apache.logging.log4j.core.LogEvent;
26 import org.apache.logging.log4j.core.config.AppenderRef;
27 import org.apache.logging.log4j.core.config.Configuration;
28 import org.apache.logging.log4j.core.config.LoggerConfig;
29 import org.apache.logging.log4j.core.config.Node;
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.PluginAttribute;
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.core.jmx.RingBufferAdmin;
37 import org.apache.logging.log4j.core.util.Booleans;
38 import org.apache.logging.log4j.util.Strings;
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
67
68 @Plugin(name = "asyncLogger", category = Node.CATEGORY, printObject = true)
69 public class AsyncLoggerConfig extends LoggerConfig {
70
71 private static final long serialVersionUID = 1L;
72
73 private AsyncLoggerConfigHelper helper;
74
75
76
77
78 public AsyncLoggerConfig() {
79 super();
80 }
81
82
83
84
85
86
87
88
89 public AsyncLoggerConfig(final String name, final Level level,
90 final boolean additive) {
91 super(name, level, additive);
92 }
93
94 protected AsyncLoggerConfig(final String name,
95 final List<AppenderRef> appenders, final Filter filter,
96 final Level level, final boolean additive,
97 final Property[] properties, final Configuration config,
98 final boolean includeLocation) {
99 super(name, appenders, filter, level, additive, properties, config,
100 includeLocation);
101 }
102
103
104
105
106
107 @Override
108 protected void callAppenders(final LogEvent event) {
109
110 event.getSource();
111 event.getThreadName();
112
113
114 if (!helper.callAppendersFromAnotherThread(event)) {
115 super.callAppenders(event);
116 }
117 }
118
119
120 void asyncCallAppenders(final LogEvent event) {
121 super.callAppenders(event);
122 }
123
124 private String displayName() {
125 return LogManager.ROOT_LOGGER_NAME.equals(getName()) ? LoggerConfig.ROOT : getName();
126 }
127
128 @Override
129 public void start() {
130 LOGGER.trace("AsyncLoggerConfig[{}] starting...", displayName());
131 this.setStarting();
132 if (helper == null) {
133 helper = new AsyncLoggerConfigHelper(this);
134 } else {
135 AsyncLoggerConfigHelper.claim();
136 }
137 super.start();
138 }
139
140 @Override
141 public void stop() {
142 LOGGER.trace("AsyncLoggerConfig[{}] stopping...", displayName());
143 this.setStopping();
144 AsyncLoggerConfigHelper.release();
145 super.stop();
146 }
147
148
149
150
151
152
153
154
155 public RingBufferAdmin createRingBufferAdmin(final String contextName) {
156 return helper.createRingBufferAdmin(contextName, getName());
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 @PluginFactory
173 public static LoggerConfig createLogger(
174 @PluginAttribute("additivity") final String additivity,
175 @PluginAttribute("level") final String levelName,
176 @PluginAttribute("name") final String loggerName,
177 @PluginAttribute("includeLocation") final String includeLocation,
178 @PluginElement("AppenderRef") final AppenderRef[] refs,
179 @PluginElement("Properties") final Property[] properties,
180 @PluginConfiguration final Configuration config,
181 @PluginElement("Filter") final Filter filter) {
182 if (loggerName == null) {
183 LOGGER.error("Loggers cannot be configured without a name");
184 return null;
185 }
186
187 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
188 Level level;
189 try {
190 level = Level.toLevel(levelName, Level.ERROR);
191 } catch (final Exception ex) {
192 LOGGER.error(
193 "Invalid Log level specified: {}. Defaulting to Error",
194 levelName);
195 level = Level.ERROR;
196 }
197 final String name = loggerName.equals(LoggerConfig.ROOT) ? Strings.EMPTY : loggerName;
198 final boolean additive = Booleans.parseBoolean(additivity, true);
199
200 return new AsyncLoggerConfig(name, appenderRefs, filter, level,
201 additive, properties, config, includeLocation(includeLocation));
202 }
203
204
205 protected static boolean includeLocation(final String includeLocationConfigValue) {
206 return Boolean.parseBoolean(includeLocationConfigValue);
207 }
208
209
210
211
212 @Plugin(name = "asyncRoot", category = "Core", printObject = true)
213 public static class RootLogger extends LoggerConfig {
214
215 private static final long serialVersionUID = 1L;
216
217 @PluginFactory
218 public static LoggerConfig createLogger(
219 @PluginAttribute("additivity") final String additivity,
220 @PluginAttribute("level") final String levelName,
221 @PluginAttribute("includeLocation") final String includeLocation,
222 @PluginElement("AppenderRef") final AppenderRef[] refs,
223 @PluginElement("Properties") final Property[] properties,
224 @PluginConfiguration final Configuration config,
225 @PluginElement("Filter") final Filter filter) {
226 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
227 Level level;
228 try {
229 level = Level.toLevel(levelName, Level.ERROR);
230 } catch (final Exception ex) {
231 LOGGER.error(
232 "Invalid Log level specified: {}. Defaulting to Error",
233 levelName);
234 level = Level.ERROR;
235 }
236 final boolean additive = Booleans.parseBoolean(additivity, true);
237
238 return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME,
239 appenderRefs, filter, level, additive, properties, config,
240 AsyncLoggerConfig.includeLocation(includeLocation));
241 }
242 }
243 }