Skip to content

Commit 88d2899

Browse files
committed
Port log4j-jul changes from 2.x
1 parent 865c908 commit 88d2899

13 files changed

+33
-329
lines changed

log4j-jul/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@
7171
<artifactId>junit</artifactId>
7272
<scope>test</scope>
7373
</dependency>
74+
<dependency>
75+
<groupId>org.apache.logging.log4j</groupId>
76+
<artifactId>log4j-core-test</artifactId>
77+
<scope>test</scope>
78+
</dependency>
7479
</dependencies>
7580

7681
<build>

log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLogger.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,9 @@ public class ApiLogger extends Logger {
5050
super(logger.getName(), null);
5151
final Level javaLevel = LevelTranslator.toJavaLevel(logger.getLevel());
5252
// "java.util.logging.LoggingPermission" "control"
53-
AccessController.doPrivileged(new PrivilegedAction<Object>() {
54-
@Override
55-
public Object run() {
56-
ApiLogger.super.setLevel(javaLevel);
57-
return null;
58-
}
53+
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
54+
ApiLogger.super.setLevel(javaLevel);
55+
return null;
5956
});
6057
this.logger = new WrappedLogger(logger);
6158
}

log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.logging.log4j.jul;
1818

1919
// note: NO import of Logger, Level, LogManager to prevent conflicts JUL/log4j
20-
2120
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2221
import java.util.Enumeration;
2322
import java.util.HashSet;
@@ -183,8 +182,9 @@ public void publish(final LogRecord record) {
183182
synchronized (this) {
184183
// Check again to make sure we still have to propagate the levels at this point
185184
if (this.installAsLevelPropagator) {
186-
@SuppressWarnings("resource") // no need to close the AutoCloseable ctx here
187-
LoggerContext context = LoggerContext.getContext(false);
185+
// no need to close the AutoCloseable ctx here
186+
@SuppressWarnings("resource")
187+
final LoggerContext context = LoggerContext.getContext(false);
188188
context.addConfigurationStartedListener(this);
189189
propagateLogLevels(context.getConfiguration());
190190
// note: java.util.logging.LogManager.addPropertyChangeListener() could also
@@ -194,8 +194,8 @@ public void publish(final LogRecord record) {
194194
}
195195
}
196196

197-
org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record);
198-
String msg = julFormatter.formatMessage(record); // use JUL's implementation to get real msg
197+
final org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record);
198+
final String msg = julFormatter.formatMessage(record); // use JUL's implementation to get real msg
199199
/* log4j allows nulls:
200200
if (msg == null) {
201201
// JUL allows nulls, but other log system may not

log4j-jul/src/main/java/org/apache/logging/log4j/jul/LogManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public class LogManager extends java.util.logging.LogManager {
4949
private final ThreadLocal<Set<String>> recursive = ThreadLocal.withInitial(HashSet::new);
5050

5151
public LogManager() {
52-
super();
5352
AbstractLoggerAdapter adapter = null;
5453
final String overrideAdaptorClassName =
5554
PropertiesUtil.getProperties().getStringProperty(JulPropertyKey.LOGGER_ADAPTER);
@@ -99,10 +98,9 @@ public Logger getLogger(final String name) {
9998
} finally {
10099
activeRequests.remove(name);
101100
}
102-
} else {
103-
LOGGER.warn("Recursive call to getLogger for {} ignored.", name);
104-
return new NoOpLogger(name);
105101
}
102+
LOGGER.warn("Recursive call to getLogger for {} ignored.", name);
103+
return new NoOpLogger(name);
106104
}
107105

108106
@Override

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/AbstractLoggerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.logging.log4j.Level;
2424
import org.apache.logging.log4j.core.LogEvent;
2525
import org.apache.logging.log4j.core.impl.MementoLogEvent;
26+
import org.apache.logging.log4j.core.test.appender.ListAppender;
2627
import org.apache.logging.log4j.jul.ApiLogger;
2728
import org.apache.logging.log4j.jul.LevelTranslator;
2829
import org.junit.Test;

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/ApiLoggerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.Assert.assertThat;
2323

2424
import java.util.logging.Logger;
25+
import org.apache.logging.log4j.core.test.appender.ListAppender;
2526
import org.apache.logging.log4j.jul.ApiLoggerAdapter;
2627
import org.apache.logging.log4j.jul.JulPropertyKey;
2728
import org.apache.logging.log4j.jul.LogManager;

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/AsyncLoggerThreadsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
import org.apache.logging.log4j.LogManager;
2424
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
2525
import org.apache.logging.log4j.core.impl.Log4jPropertyKey;
26+
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
2627
import org.junit.AfterClass;
2728
import org.junit.BeforeClass;
28-
import org.junit.Ignore;
2929
import org.junit.Test;
30+
import org.junit.experimental.categories.Category;
3031

31-
// @Category(AsyncLoggers.class)
32-
@Ignore("https://issues.apache.org/jira/browse/LOG4J2-3523")
32+
@Category(AsyncLoggers.class)
3333
public class AsyncLoggerThreadsTest {
3434

3535
@BeforeClass

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/BracketInNotInterpolatedMessageTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.logging.LogRecord;
2525
import java.util.logging.Logger;
2626
import org.apache.logging.log4j.core.LogEvent;
27+
import org.apache.logging.log4j.core.test.appender.ListAppender;
2728
import org.apache.logging.log4j.jul.LogManager;
2829
import org.junit.AfterClass;
2930
import org.junit.BeforeClass;

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CallerInformationTest.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,25 @@
1616
*/
1717
package org.apache.logging.log4j.jul.test;
1818

19-
import static org.junit.Assert.*;
19+
import static org.junit.Assert.assertEquals;
2020

21-
import java.net.URI;
2221
import java.util.List;
2322
import java.util.logging.Logger;
24-
import org.apache.logging.log4j.core.LoggerContext;
23+
import org.apache.logging.log4j.core.test.appender.ListAppender;
24+
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
2525
import org.apache.logging.log4j.jul.LogManager;
26-
import org.junit.After;
2726
import org.junit.AfterClass;
28-
import org.junit.Before;
2927
import org.junit.BeforeClass;
28+
import org.junit.Rule;
3029
import org.junit.Test;
3130

3231
public class CallerInformationTest {
3332

3433
// config from log4j-core test-jar
3534
private static final String CONFIG = "log4j2-calling-class.xml";
3635

37-
private LoggerContext ctx;
38-
private ListAppender app;
36+
@Rule
37+
public final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
3938

4039
@BeforeClass
4140
public static void setUpClass() {
@@ -47,27 +46,9 @@ public static void tearDownClass() {
4746
System.clearProperty("java.util.logging.manager");
4847
}
4948

50-
@Before
51-
public void beforeEach() throws Exception {
52-
final URI uri = this.getClass().getClassLoader().getResource(CONFIG).toURI();
53-
ctx = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(null, false, uri);
54-
assertNotNull("No LoggerContext", ctx);
55-
}
56-
57-
@After
58-
public void afterEach() throws Exception {
59-
if (ctx != null) {
60-
ctx.stop();
61-
ctx = null;
62-
app = null;
63-
}
64-
}
65-
6649
@Test
6750
public void testClassLogger() throws Exception {
68-
app = ctx.getConfiguration().getAppender("Class");
69-
assertNotNull("No ListAppender", app);
70-
app.clear();
51+
final ListAppender app = ctx.getListAppender("Class").clear();
7152
final Logger logger = Logger.getLogger("ClassLogger");
7253
logger.info("Ignored message contents.");
7354
logger.warning("Verifying the caller class is still correct.");
@@ -81,9 +62,7 @@ public void testClassLogger() throws Exception {
8162

8263
@Test
8364
public void testMethodLogger() throws Exception {
84-
app = ctx.getConfiguration().getAppender("Method");
85-
assertNotNull("No ListAppender", app);
86-
app.clear();
65+
final ListAppender app = ctx.getListAppender("Method").clear();
8766
final Logger logger = Logger.getLogger("MethodLogger");
8867
logger.info("More messages.");
8968
logger.warning("CATASTROPHE INCOMING!");

log4j-jul/src/test/java/org/apache/logging/log4j/jul/test/CoreLoggerTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.logging.Level;
2525
import java.util.logging.Logger;
26+
import org.apache.logging.log4j.core.test.appender.ListAppender;
2627
import org.apache.logging.log4j.jul.LogManager;
2728
import org.apache.logging.log4j.util.Strings;
2829
import org.junit.After;

0 commit comments

Comments
 (0)