Skip to content

Commit de42610

Browse files
committed
Refactor property names
Property names are often inconsistent. This commit: * changes `log4j.configuration.file` to `log4j.configuration.location`, since the value is not necessarily a file, * the names of property classes end in `Properties`, whereas their prefix starts with a small letter, * the `AsyncLogger` and `AsyncLoggerConfig` properties are merged into one class, since they should not be used together, * the `AuthenticationProvider` is removed from `ConfigurationProperties` and placed in a standalone class, since multiple components use it.
1 parent d61ad26 commit de42610

File tree

75 files changed

+546
-614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+546
-614
lines changed

log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationFactoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.log4j.config;
1818

19+
import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
1920
import static org.junit.Assert.assertTrue;
2021

2122
import java.io.File;
@@ -31,7 +32,7 @@ public class XmlConfigurationFactoryTest {
3132

3233
@BeforeClass
3334
public static void beforeClass() {
34-
System.setProperty("log4j2.Version1.configuration", "target/test-classes/log4j1-file.xml");
35+
System.setProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY, "target/test-classes/log4j1-file.xml");
3536
}
3637

3738
@Test

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/AsyncLoggerConfigDelegate.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/AsyncLoggerConfigDisruptor.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.lmax.disruptor.EventFactory;
2222
import com.lmax.disruptor.EventHandler;
2323
import com.lmax.disruptor.EventTranslatorTwoArg;
24-
import com.lmax.disruptor.ExceptionHandler;
2524
import com.lmax.disruptor.RingBuffer;
2625
import com.lmax.disruptor.Sequence;
2726
import com.lmax.disruptor.TimeoutException;
@@ -33,12 +32,11 @@
3332
import java.util.concurrent.locks.Lock;
3433
import java.util.concurrent.locks.ReentrantLock;
3534
import org.apache.logging.log4j.Level;
36-
import org.apache.logging.log4j.async.logger.internal.DisruptorUtil;
35+
import org.apache.logging.log4j.async.logger.internal.AsyncLoggerConfigDefaultExceptionHandler;
3736
import org.apache.logging.log4j.core.AbstractLifeCycle;
3837
import org.apache.logging.log4j.core.LogEvent;
3938
import org.apache.logging.log4j.core.ReusableLogEvent;
4039
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicy;
41-
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicyFactory;
4240
import org.apache.logging.log4j.core.async.DiscardingAsyncQueueFullPolicy;
4341
import org.apache.logging.log4j.core.async.EventRoute;
4442
import org.apache.logging.log4j.core.async.InternalAsyncUtil;
@@ -50,6 +48,7 @@
5048
import org.apache.logging.log4j.core.util.Throwables;
5149
import org.apache.logging.log4j.message.ReusableMessage;
5250
import org.apache.logging.log4j.plugins.Inject;
51+
import org.apache.logging.log4j.util.LoaderUtil;
5352

5453
/**
5554
* Helper class decoupling the {@code AsyncLoggerConfig} class from the LMAX Disruptor library.
@@ -72,15 +71,16 @@ public class AsyncLoggerConfigDisruptor extends AbstractLifeCycle {
7271
* RingBuffer events contain all information necessary to perform the work in a separate thread.
7372
*/
7473
public static class Log4jEventWrapper {
74+
7575
public Log4jEventWrapper() {}
7676

7777
public Log4jEventWrapper(final LogEvent mutableLogEvent) {
7878
event = mutableLogEvent;
7979
}
8080

8181
private AsyncLoggerConfig loggerConfig;
82-
private LogEvent event;
8382

83+
private LogEvent event;
8484
/**
8585
* Release references held by ring buffer to allow objects to be garbage-collected.
8686
*/
@@ -98,11 +98,11 @@ public String toString() {
9898
return String.valueOf(event);
9999
}
100100
}
101-
102101
/**
103102
* EventHandler performs the work in a separate thread.
104103
*/
105104
private static class Log4jEventWrapperHandler implements EventHandler<Log4jEventWrapper> {
105+
106106
private static final int NOTIFY_PROGRESS_THRESHOLD = 50;
107107
private Sequence sequenceCallback;
108108
private int counter;
@@ -132,7 +132,6 @@ private void notifyIntermediateProgress(final long sequence) {
132132
}
133133
}
134134
}
135-
136135
/**
137136
* Factory used to populate the RingBuffer with events. These event objects are then re-used during the life of the
138137
* RingBuffer.
@@ -166,34 +165,36 @@ private void notifyIntermediateProgress(final long sequence) {
166165
ringBufferElement.loggerConfig = loggerConfig;
167166
};
168167

169-
private AsyncQueueFullPolicy asyncQueueFullPolicy;
170-
171168
private volatile Disruptor<Log4jEventWrapper> disruptor;
169+
172170
private long backgroundThreadId; // LOG4J2-471
173171
private EventTranslatorTwoArg<Log4jEventWrapper, LogEvent, AsyncLoggerConfig> translator;
174172
private static volatile boolean alreadyLoggedWarning;
175-
private final AsyncLoggerKeys.AsyncLoggerConfig propsConfig;
173+
private final AsyncQueueFullPolicy asyncQueueFullPolicy;
176174
private final WaitStrategy waitStrategy;
175+
private final AsyncLoggerProperties properties;
177176
private final boolean mutable;
178-
179177
private final Lock startLock = new ReentrantLock();
178+
180179
private final Lock queueFullEnqueueLock = new ReentrantLock();
181180

182181
@Inject
183182
public AsyncLoggerConfigDisruptor(
183+
final AsyncQueueFullPolicy asyncQueueFullPolicy,
184184
final WaitStrategy waitStrategy,
185185
final LogEventFactory logEventFactory,
186-
final AsyncLoggerKeys.AsyncLoggerConfig propsConfig) {
186+
final AsyncLoggerProperties properties) {
187+
this.asyncQueueFullPolicy = asyncQueueFullPolicy;
187188
this.waitStrategy = waitStrategy;
188189
this.mutable = logEventFactory instanceof ReusableLogEventFactory;
189-
this.propsConfig = propsConfig;
190+
this.properties = properties;
190191
}
191192

192193
// package-protected for testing
194+
193195
WaitStrategy getWaitStrategy() {
194196
return waitStrategy;
195197
}
196-
197198
/**
198199
* Increases the reference count and creates and starts a new Disruptor and associated thread if none currently
199200
* exists.
@@ -210,7 +211,7 @@ public void start() {
210211
return;
211212
}
212213
LOGGER.trace("AsyncLoggerConfigDisruptor creating new disruptor for this configuration.");
213-
final int ringBufferSize = DisruptorUtil.calculateRingBufferSize(propsConfig);
214+
final int ringBufferSize = properties.roundedRingBufferSize();
214215

215216
final ThreadFactory threadFactory =
216217
new Log4jThreadFactory("AsyncLoggerConfig", true, Thread.NORM_PRIORITY) {
@@ -221,14 +222,12 @@ public Thread newThread(final Runnable r) {
221222
return result;
222223
}
223224
};
224-
asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();
225225

226226
translator = mutable ? MUTABLE_TRANSLATOR : TRANSLATOR;
227227
final EventFactory<Log4jEventWrapper> factory = mutable ? MUTABLE_FACTORY : FACTORY;
228228
disruptor = new Disruptor<>(factory, ringBufferSize, threadFactory, ProducerType.MULTI, waitStrategy);
229229

230-
final ExceptionHandler<Log4jEventWrapper> errorHandler =
231-
DisruptorUtil.getAsyncLoggerConfigExceptionHandler(propsConfig);
230+
final AsyncLoggerConfigExceptionHandler errorHandler = getExceptionHandler();
232231
disruptor.setDefaultExceptionHandler(errorHandler);
233232

234233
final Log4jEventWrapperHandler[] handlers = {new Log4jEventWrapperHandler()};
@@ -308,6 +307,18 @@ public EventRoute getEventRoute(final Level logLevel) {
308307
return asyncQueueFullPolicy.getRoute(backgroundThreadId, logLevel);
309308
}
310309

310+
private AsyncLoggerConfigExceptionHandler getExceptionHandler() {
311+
try {
312+
final Class<? extends AsyncLoggerConfigExceptionHandler> handlerClass = properties.configExceptionHandler();
313+
if (handlerClass != null) {
314+
return LoaderUtil.newInstanceOf(handlerClass);
315+
}
316+
} catch (final ReflectiveOperationException e) {
317+
LOGGER.debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e);
318+
}
319+
return new AsyncLoggerConfigDefaultExceptionHandler();
320+
}
321+
311322
private int remainingDisruptorCapacity() {
312323
final Disruptor<Log4jEventWrapper> temp = disruptor;
313324
if (hasLog4jBeenShutDown(temp)) {
@@ -400,7 +411,7 @@ private void enqueue(final LogEvent logEvent, final AsyncLoggerConfig asyncLogge
400411
}
401412

402413
private boolean synchronizeEnqueueWhenQueueFull() {
403-
return propsConfig.synchronizeEnqueueWhenQueueFull()
414+
return properties.synchronizeEnqueueWhenQueueFull()
404415
// Background thread must never block
405416
&& backgroundThreadId != Thread.currentThread().getId()
406417
// Threads owned by log4j are most likely to result in
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.async.logger;
18+
19+
import com.lmax.disruptor.ExceptionHandler;
20+
21+
/**
22+
* Specialization of {@link ExceptionHandler} to be used by the {@link AsyncLoggerConfig} class.
23+
*/
24+
public interface AsyncLoggerConfigExceptionHandler
25+
extends ExceptionHandler<AsyncLoggerConfigDisruptor.Log4jEventWrapper> {}

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/AsyncLoggerContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.net.URI;
2020
import java.util.concurrent.TimeUnit;
21-
import org.apache.logging.log4j.async.logger.internal.DefaultBundle;
21+
import org.apache.logging.log4j.async.logger.internal.AsyncLoggerDefaultBundle;
2222
import org.apache.logging.log4j.core.Logger;
2323
import org.apache.logging.log4j.core.LoggerContext;
2424
import org.apache.logging.log4j.core.config.Configuration;
@@ -43,7 +43,7 @@ public AsyncLoggerContext(
4343
final URI configLocation,
4444
final ConfigurableInstanceFactory instanceFactory) {
4545
super(name, externalContext, configLocation, instanceFactory);
46-
instanceFactory.registerBundle(DefaultBundle.class);
46+
instanceFactory.registerBundle(AsyncLoggerDefaultBundle.class);
4747
instanceFactory.registerBinding(Key.forClass(AsyncLoggerDisruptor.class), loggerDisruptor);
4848
}
4949

log4j-async-logger/src/main/java/org/apache/logging/log4j/async/logger/AsyncLoggerDisruptor.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
package org.apache.logging.log4j.async.logger;
1818

19-
import com.lmax.disruptor.ExceptionHandler;
2019
import com.lmax.disruptor.RingBuffer;
2120
import com.lmax.disruptor.TimeoutException;
2221
import com.lmax.disruptor.WaitStrategy;
@@ -27,16 +26,17 @@
2726
import java.util.concurrent.locks.Lock;
2827
import java.util.concurrent.locks.ReentrantLock;
2928
import org.apache.logging.log4j.Level;
30-
import org.apache.logging.log4j.async.logger.internal.DisruptorUtil;
29+
import org.apache.logging.log4j.async.logger.internal.AsyncLoggerDefaultExceptionHandler;
3130
import org.apache.logging.log4j.core.AbstractLifeCycle;
3231
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicy;
33-
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicyFactory;
3432
import org.apache.logging.log4j.core.async.DiscardingAsyncQueueFullPolicy;
3533
import org.apache.logging.log4j.core.async.EventRoute;
3634
import org.apache.logging.log4j.core.util.Log4jThread;
3735
import org.apache.logging.log4j.core.util.Log4jThreadFactory;
3836
import org.apache.logging.log4j.core.util.Throwables;
3937
import org.apache.logging.log4j.message.Message;
38+
import org.apache.logging.log4j.status.StatusLogger;
39+
import org.apache.logging.log4j.util.LoaderUtil;
4040

4141
/**
4242
* Helper class for async loggers: AsyncLoggerDisruptor handles the mechanics of working with the LMAX Disruptor, and
@@ -53,20 +53,25 @@ public class AsyncLoggerDisruptor extends AbstractLifeCycle {
5353

5454
private volatile Disruptor<RingBufferLogEvent> disruptor;
5555
private String contextName;
56-
private final AsyncLoggerKeys.AsyncLogger propsConfig;
56+
private final AsyncLoggerProperties properties;
5757

5858
private long backgroundThreadId;
59-
private AsyncQueueFullPolicy asyncQueueFullPolicy;
59+
private final AsyncQueueFullPolicy asyncQueueFullPolicy;
6060
private final WaitStrategy waitStrategy;
6161

6262
public AsyncLoggerDisruptor(
63-
final String contextName, final WaitStrategy waitStrategy, final AsyncLoggerKeys.AsyncLogger propsConfig) {
63+
final String contextName,
64+
final AsyncQueueFullPolicy asyncQueueFullPolicy,
65+
final WaitStrategy waitStrategy,
66+
final AsyncLoggerProperties properties) {
6467
this.contextName = contextName;
68+
this.asyncQueueFullPolicy = asyncQueueFullPolicy;
6569
this.waitStrategy = waitStrategy;
66-
this.propsConfig = propsConfig;
70+
this.properties = properties;
6771
}
6872

6973
// package-protected for testing
74+
7075
WaitStrategy getWaitStrategy() {
7176
return waitStrategy;
7277
}
@@ -100,7 +105,7 @@ public void start() {
100105
}
101106
setStarting();
102107
LOGGER.trace("[{}] AsyncLoggerDisruptor creating new disruptor for this context.", contextName);
103-
final int ringBufferSize = DisruptorUtil.calculateRingBufferSize(propsConfig);
108+
final int ringBufferSize = properties.roundedRingBufferSize();
104109

105110
final ThreadFactory threadFactory =
106111
new Log4jThreadFactory("AsyncLogger[" + contextName + "]", true, Thread.NORM_PRIORITY) {
@@ -111,13 +116,11 @@ public Thread newThread(final Runnable r) {
111116
return result;
112117
}
113118
};
114-
asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();
115119

116120
disruptor = new Disruptor<>(
117121
RingBufferLogEvent.FACTORY, ringBufferSize, threadFactory, ProducerType.MULTI, waitStrategy);
118122

119-
final ExceptionHandler<RingBufferLogEvent> errorHandler =
120-
DisruptorUtil.getAsyncLoggerExceptionHandler(propsConfig);
123+
final AsyncLoggerExceptionHandler errorHandler = getExceptionHandler();
121124
disruptor.setDefaultExceptionHandler(errorHandler);
122125

123126
final RingBufferLogEventHandler[] handlers = {new RingBufferLogEventHandler()};
@@ -200,6 +203,18 @@ EventRoute getEventRoute(final Level logLevel) {
200203
return asyncQueueFullPolicy.getRoute(backgroundThreadId, logLevel);
201204
}
202205

206+
private AsyncLoggerExceptionHandler getExceptionHandler() {
207+
try {
208+
final Class<? extends AsyncLoggerExceptionHandler> handlerClass = properties.exceptionHandler();
209+
if (handlerClass != null) {
210+
return LoaderUtil.newInstanceOf(handlerClass);
211+
}
212+
} catch (final ReflectiveOperationException e) {
213+
StatusLogger.getLogger().debug("Invalid AsyncLogger.ExceptionHandler value: {}", e.getMessage(), e);
214+
}
215+
return new AsyncLoggerDefaultExceptionHandler();
216+
}
217+
203218
private int remainingDisruptorCapacity() {
204219
final Disruptor<RingBufferLogEvent> temp = disruptor;
205220
if (hasLog4jBeenShutDown(temp)) {
@@ -253,7 +268,7 @@ void enqueueLogMessageWhenQueueFull(final RingBufferLogEventTranslator translato
253268
}
254269

255270
private boolean synchronizeEnqueueWhenQueueFull() {
256-
return propsConfig.synchronizeEnqueueWhenQueueFull()
271+
return properties.synchronizeEnqueueWhenQueueFull()
257272
// Background thread must never block
258273
&& backgroundThreadId != Thread.currentThread().getId()
259274
// Threads owned by log4j are most likely to result in

0 commit comments

Comments
 (0)