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.Property;
30 import org.apache.logging.log4j.core.config.plugins.Plugin;
31 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
32 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
33 import org.apache.logging.log4j.core.config.plugins.PluginElement;
34 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
35 import org.apache.logging.log4j.core.jmx.RingBufferAdmin;
36 import org.apache.logging.log4j.core.util.Booleans;
37 import org.apache.logging.log4j.util.Strings;
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
67 @Plugin(name = "asyncLogger", category = "Core", printObject = true)
68 public class AsyncLoggerConfig extends LoggerConfig {
69
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(final LogEvent event) {
106
107 event.getSource();
108 event.getThreadName();
109
110
111 if (!helper.callAppendersFromAnotherThread(event)) {
112 super.callAppenders(event);
113 }
114 }
115
116
117 void asyncCallAppenders(final LogEvent event) {
118 super.callAppenders(event);
119 }
120
121 @Override
122 public void start() {
123 LOGGER.trace("AsyncLoggerConfig[{}] starting...", getName());
124 this.setStarting();
125 if (helper == null) {
126 helper = new AsyncLoggerConfigHelper(this);
127 } else {
128 AsyncLoggerConfigHelper.claim();
129 }
130 super.start();
131 }
132
133 @Override
134 public void stop() {
135 LOGGER.trace("AsyncLoggerConfig[{}] stopping...", getName());
136 this.setStopping();
137 AsyncLoggerConfigHelper.release();
138 super.stop();
139 }
140
141
142
143
144
145
146
147 public RingBufferAdmin createRingBufferAdmin(final String contextName) {
148 return helper.createRingBufferAdmin(contextName, getName());
149 }
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 @PluginFactory
165 public static LoggerConfig createLogger(
166 @PluginAttribute("additivity") final String additivity,
167 @PluginAttribute("level") final String levelName,
168 @PluginAttribute("name") final String loggerName,
169 @PluginAttribute("includeLocation") final String includeLocation,
170 @PluginElement("AppenderRef") final AppenderRef[] refs,
171 @PluginElement("Properties") final Property[] properties,
172 @PluginConfiguration final Configuration config,
173 @PluginElement("Filter") final Filter filter) {
174 if (loggerName == null) {
175 LOGGER.error("Loggers cannot be configured without a name");
176 return null;
177 }
178
179 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
180 Level level;
181 try {
182 level = Level.toLevel(levelName, Level.ERROR);
183 } catch (final Exception ex) {
184 LOGGER.error(
185 "Invalid Log level specified: {}. Defaulting to Error",
186 levelName);
187 level = Level.ERROR;
188 }
189 final String name = loggerName.equals("root") ? Strings.EMPTY : loggerName;
190 final boolean additive = Booleans.parseBoolean(additivity, true);
191
192 return new AsyncLoggerConfig(name, appenderRefs, filter, level,
193 additive, properties, config, includeLocation(includeLocation));
194 }
195
196
197 protected static boolean includeLocation(final String includeLocationConfigValue) {
198 return Boolean.parseBoolean(includeLocationConfigValue);
199 }
200
201
202
203
204 @Plugin(name = "asyncRoot", category = "Core", printObject = true)
205 public static class RootLogger extends LoggerConfig {
206
207 @PluginFactory
208 public static LoggerConfig createLogger(
209 @PluginAttribute("additivity") final String additivity,
210 @PluginAttribute("level") final String levelName,
211 @PluginAttribute("includeLocation") final String includeLocation,
212 @PluginElement("AppenderRef") final AppenderRef[] refs,
213 @PluginElement("Properties") final Property[] properties,
214 @PluginConfiguration final Configuration config,
215 @PluginElement("Filter") final Filter filter) {
216 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
217 Level level;
218 try {
219 level = Level.toLevel(levelName, Level.ERROR);
220 } catch (final Exception ex) {
221 LOGGER.error(
222 "Invalid Log level specified: {}. Defaulting to Error",
223 levelName);
224 level = Level.ERROR;
225 }
226 final boolean additive = Booleans.parseBoolean(additivity, true);
227
228 return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME,
229 appenderRefs, filter, level, additive, properties, config,
230 includeLocation(includeLocation));
231 }
232 }
233 }