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