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 final AsyncLoggerConfigDelegate delegate;
72
73 protected AsyncLoggerConfig(final String name,
74 final List<AppenderRef> appenders, final Filter filter,
75 final Level level, final boolean additive,
76 final Property[] properties, final Configuration config,
77 final boolean includeLocation) {
78 super(name, appenders, filter, level, additive, properties, config,
79 includeLocation);
80 delegate = config.getAsyncLoggerConfigDelegate();
81 delegate.setLogEventFactory(getLogEventFactory());
82 }
83
84
85
86
87
88 @Override
89 protected void callAppenders(final LogEvent event) {
90 populateLazilyInitializedFields(event);
91
92 if (!delegate.tryEnqueue(event, this)) {
93 final EventRoute eventRoute = delegate.getEventRoute(event.getLevel());
94 eventRoute.logMessage(this, event);
95 }
96 }
97
98 private void populateLazilyInitializedFields(final LogEvent event) {
99 event.getSource();
100 event.getThreadName();
101 }
102
103 void callAppendersInCurrentThread(final LogEvent event) {
104 super.callAppenders(event);
105 }
106
107 void callAppendersInBackgroundThread(final LogEvent event) {
108 delegate.enqueueEvent(event, this);
109 }
110
111
112 void asyncCallAppenders(final LogEvent event) {
113 super.callAppenders(event);
114 }
115
116 private String displayName() {
117 return LogManager.ROOT_LOGGER_NAME.equals(getName()) ? LoggerConfig.ROOT : getName();
118 }
119
120 @Override
121 public void start() {
122 LOGGER.trace("AsyncLoggerConfig[{}] starting...", displayName());
123 super.start();
124 }
125
126 @Override
127 public void stop() {
128 LOGGER.trace("AsyncLoggerConfig[{}] stopping...", displayName());
129 super.stop();
130 }
131
132
133
134
135
136
137
138
139 public RingBufferAdmin createRingBufferAdmin(final String contextName) {
140 return delegate.createRingBufferAdmin(contextName, getName());
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156 @PluginFactory
157 public static LoggerConfig createLogger(
158 @PluginAttribute("additivity") final String additivity,
159 @PluginAttribute("level") final String levelName,
160 @PluginAttribute("name") final String loggerName,
161 @PluginAttribute("includeLocation") final String includeLocation,
162 @PluginElement("AppenderRef") final AppenderRef[] refs,
163 @PluginElement("Properties") final Property[] properties,
164 @PluginConfiguration final Configuration config,
165 @PluginElement("Filter") final Filter filter) {
166 if (loggerName == null) {
167 LOGGER.error("Loggers cannot be configured without a name");
168 return null;
169 }
170
171 final List<AppenderRef> appenderRefs = Arrays.asList(refs);
172 Level level;
173 try {
174 level = Level.toLevel(levelName, Level.ERROR);
175 } catch (final Exception ex) {
176 LOGGER.error(
177 "Invalid Log level specified: {}. Defaulting to Error",
178 levelName);
179 level = Level.ERROR;
180 }
181 final String name = loggerName.equals(LoggerConfig.ROOT) ? Strings.EMPTY : loggerName;
182 final boolean additive = Booleans.parseBoolean(additivity, true);
183
184 return new AsyncLoggerConfig(name, appenderRefs, filter, level,
185 additive, properties, config, includeLocation(includeLocation));
186 }
187
188
189 protected static boolean includeLocation(final String includeLocationConfigValue) {
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 @PluginAttribute("additivity") final String additivity,
202 @PluginAttribute("level") final String levelName,
203 @PluginAttribute("includeLocation") final String includeLocation,
204 @PluginElement("AppenderRef") final AppenderRef[] refs,
205 @PluginElement("Properties") final Property[] properties,
206 @PluginConfiguration final Configuration config,
207 @PluginElement("Filter") 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 = Booleans.parseBoolean(additivity, true);
219
220 return new AsyncLoggerConfig(LogManager.ROOT_LOGGER_NAME,
221 appenderRefs, filter, level, additive, properties, config,
222 AsyncLoggerConfig.includeLocation(includeLocation));
223 }
224 }
225 }