Skip to content

Commit ec802f4

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 ec802f4

File tree

68 files changed

+469
-557
lines changed

Some content is hidden

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

68 files changed

+469
-557
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: 26 additions & 14 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,7 +32,7 @@
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;
@@ -50,6 +49,7 @@
5049
import org.apache.logging.log4j.core.util.Throwables;
5150
import org.apache.logging.log4j.message.ReusableMessage;
5251
import org.apache.logging.log4j.plugins.Inject;
52+
import org.apache.logging.log4j.util.LoaderUtil;
5353

5454
/**
5555
* Helper class decoupling the {@code AsyncLoggerConfig} class from the LMAX Disruptor library.
@@ -72,15 +72,16 @@ public class AsyncLoggerConfigDisruptor extends AbstractLifeCycle {
7272
* RingBuffer events contain all information necessary to perform the work in a separate thread.
7373
*/
7474
public static class Log4jEventWrapper {
75+
7576
public Log4jEventWrapper() {}
7677

7778
public Log4jEventWrapper(final LogEvent mutableLogEvent) {
7879
event = mutableLogEvent;
7980
}
8081

8182
private AsyncLoggerConfig loggerConfig;
82-
private LogEvent event;
8383

84+
private LogEvent event;
8485
/**
8586
* Release references held by ring buffer to allow objects to be garbage-collected.
8687
*/
@@ -98,11 +99,11 @@ public String toString() {
9899
return String.valueOf(event);
99100
}
100101
}
101-
102102
/**
103103
* EventHandler performs the work in a separate thread.
104104
*/
105105
private static class Log4jEventWrapperHandler implements EventHandler<Log4jEventWrapper> {
106+
106107
private static final int NOTIFY_PROGRESS_THRESHOLD = 50;
107108
private Sequence sequenceCallback;
108109
private int counter;
@@ -132,7 +133,6 @@ private void notifyIntermediateProgress(final long sequence) {
132133
}
133134
}
134135
}
135-
136136
/**
137137
* Factory used to populate the RingBuffer with events. These event objects are then re-used during the life of the
138138
* RingBuffer.
@@ -169,31 +169,32 @@ private void notifyIntermediateProgress(final long sequence) {
169169
private AsyncQueueFullPolicy asyncQueueFullPolicy;
170170

171171
private volatile Disruptor<Log4jEventWrapper> disruptor;
172+
172173
private long backgroundThreadId; // LOG4J2-471
173174
private EventTranslatorTwoArg<Log4jEventWrapper, LogEvent, AsyncLoggerConfig> translator;
174175
private static volatile boolean alreadyLoggedWarning;
175-
private final AsyncLoggerKeys.AsyncLoggerConfig propsConfig;
176+
private final AsyncLoggerProperties properties;
176177
private final WaitStrategy waitStrategy;
177178
private final boolean mutable;
178-
179179
private final Lock startLock = new ReentrantLock();
180+
180181
private final Lock queueFullEnqueueLock = new ReentrantLock();
181182

182183
@Inject
183184
public AsyncLoggerConfigDisruptor(
184185
final WaitStrategy waitStrategy,
185186
final LogEventFactory logEventFactory,
186-
final AsyncLoggerKeys.AsyncLoggerConfig propsConfig) {
187+
final AsyncLoggerProperties properties) {
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) {
@@ -227,8 +228,7 @@ public Thread newThread(final Runnable r) {
227228
final EventFactory<Log4jEventWrapper> factory = mutable ? MUTABLE_FACTORY : FACTORY;
228229
disruptor = new Disruptor<>(factory, ringBufferSize, threadFactory, ProducerType.MULTI, waitStrategy);
229230

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

234234
final Log4jEventWrapperHandler[] handlers = {new Log4jEventWrapperHandler()};
@@ -308,6 +308,18 @@ public EventRoute getEventRoute(final Level logLevel) {
308308
return asyncQueueFullPolicy.getRoute(backgroundThreadId, logLevel);
309309
}
310310

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

402414
private boolean synchronizeEnqueueWhenQueueFull() {
403-
return propsConfig.synchronizeEnqueueWhenQueueFull()
415+
return properties.synchronizeEnqueueWhenQueueFull()
404416
// Background thread must never block
405417
&& backgroundThreadId != Thread.currentThread().getId()
406418
// 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: 22 additions & 9 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,7 +26,7 @@
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;
3332
import org.apache.logging.log4j.core.async.AsyncQueueFullPolicyFactory;
@@ -37,6 +36,8 @@
3736
import org.apache.logging.log4j.core.util.Log4jThreadFactory;
3837
import org.apache.logging.log4j.core.util.Throwables;
3938
import org.apache.logging.log4j.message.Message;
39+
import org.apache.logging.log4j.status.StatusLogger;
40+
import org.apache.logging.log4j.util.LoaderUtil;
4041

4142
/**
4243
* Helper class for async loggers: AsyncLoggerDisruptor handles the mechanics of working with the LMAX Disruptor, and
@@ -53,20 +54,21 @@ public class AsyncLoggerDisruptor extends AbstractLifeCycle {
5354

5455
private volatile Disruptor<RingBufferLogEvent> disruptor;
5556
private String contextName;
56-
private final AsyncLoggerKeys.AsyncLogger propsConfig;
57+
private final AsyncLoggerProperties properties;
5758

5859
private long backgroundThreadId;
5960
private AsyncQueueFullPolicy asyncQueueFullPolicy;
6061
private final WaitStrategy waitStrategy;
6162

6263
public AsyncLoggerDisruptor(
63-
final String contextName, final WaitStrategy waitStrategy, final AsyncLoggerKeys.AsyncLogger propsConfig) {
64+
final String contextName, final WaitStrategy waitStrategy, final AsyncLoggerProperties properties) {
6465
this.contextName = contextName;
6566
this.waitStrategy = waitStrategy;
66-
this.propsConfig = propsConfig;
67+
this.properties = properties;
6768
}
6869

6970
// package-protected for testing
71+
7072
WaitStrategy getWaitStrategy() {
7173
return waitStrategy;
7274
}
@@ -100,7 +102,7 @@ public void start() {
100102
}
101103
setStarting();
102104
LOGGER.trace("[{}] AsyncLoggerDisruptor creating new disruptor for this context.", contextName);
103-
final int ringBufferSize = DisruptorUtil.calculateRingBufferSize(propsConfig);
105+
final int ringBufferSize = properties.roundedRingBufferSize();
104106

105107
final ThreadFactory threadFactory =
106108
new Log4jThreadFactory("AsyncLogger[" + contextName + "]", true, Thread.NORM_PRIORITY) {
@@ -116,8 +118,7 @@ public Thread newThread(final Runnable r) {
116118
disruptor = new Disruptor<>(
117119
RingBufferLogEvent.FACTORY, ringBufferSize, threadFactory, ProducerType.MULTI, waitStrategy);
118120

119-
final ExceptionHandler<RingBufferLogEvent> errorHandler =
120-
DisruptorUtil.getAsyncLoggerExceptionHandler(propsConfig);
121+
final AsyncLoggerExceptionHandler errorHandler = getExceptionHandler();
121122
disruptor.setDefaultExceptionHandler(errorHandler);
122123

123124
final RingBufferLogEventHandler[] handlers = {new RingBufferLogEventHandler()};
@@ -200,6 +201,18 @@ EventRoute getEventRoute(final Level logLevel) {
200201
return asyncQueueFullPolicy.getRoute(backgroundThreadId, logLevel);
201202
}
202203

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

255268
private boolean synchronizeEnqueueWhenQueueFull() {
256-
return propsConfig.synchronizeEnqueueWhenQueueFull()
269+
return properties.synchronizeEnqueueWhenQueueFull()
257270
// Background thread must never block
258271
&& backgroundThreadId != Thread.currentThread().getId()
259272
// Threads owned by log4j are most likely to result in
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 AsyncLogger} class.
23+
*/
24+
public interface AsyncLoggerExceptionHandler extends ExceptionHandler<RingBufferLogEvent> {}

0 commit comments

Comments
 (0)