Skip to content

Commit 6fb4f90

Browse files
committed
Disable level modification via JUL by default
This PR switches the default JUL `LoggerAdapter` from `CoreLoggerAdapter` to `ApiLoggerAdapter`. Level mutators in the `java.util.logging.Logger` interface will be disabled by default, unless users reenable them explicitly. Closes #2353.
1 parent 615092b commit 6fb4f90

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.HashSet;
2222
import java.util.Set;
2323
import java.util.logging.Logger;
24-
import org.apache.logging.log4j.LoggingException;
2524
import org.apache.logging.log4j.status.StatusLogger;
2625
import org.apache.logging.log4j.util.LoaderUtil;
2726
import org.apache.logging.log4j.util.PropertiesUtil;
@@ -58,21 +57,9 @@ public LogManager() {
5857
}
5958
}
6059
if (adapter == null) {
61-
// default adapter
62-
String adapterClassName;
63-
try {
64-
// find out if log4j-core is available
65-
LoaderUtil.loadClass(Constants.CORE_LOGGER_CLASS_NAME);
66-
adapterClassName = Constants.CORE_LOGGER_ADAPTER_CLASS_NAME;
67-
} catch (final ClassNotFoundException ignored) {
68-
adapterClassName = Constants.API_LOGGER_ADAPTER_CLASS_NAME;
69-
}
70-
LOGGER.debug("Attempting to use {}", adapterClassName);
71-
try {
72-
adapter = LoaderUtil.newCheckedInstanceOf(adapterClassName, AbstractLoggerAdapter.class);
73-
} catch (final Exception e) {
74-
throw LOGGER.throwing(new LoggingException(e));
75-
}
60+
// Use API by default
61+
// See https://github.com/apache/logging-log4j2/issues/2353
62+
adapter = new ApiLoggerAdapter();
7663
}
7764
loggerAdapter = adapter;
7865
LOGGER.info("Registered Log4j as the java.util.logging.LogManager.");

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
*/
1717
package org.apache.logging.log4j.jul.test;
1818

19+
import static org.hamcrest.MatcherAssert.assertThat;
1920
import static org.hamcrest.Matchers.equalTo;
2021
import static org.junit.Assert.assertNotNull;
2122
import static org.junit.Assert.assertNull;
22-
import static org.junit.Assert.assertThat;
2323

2424
import java.util.logging.Logger;
2525
import org.apache.logging.log4j.core.test.appender.ListAppender;
26-
import org.apache.logging.log4j.jul.ApiLoggerAdapter;
27-
import org.apache.logging.log4j.jul.Constants;
2826
import org.apache.logging.log4j.jul.LogManager;
2927
import org.junit.After;
3028
import org.junit.AfterClass;
@@ -37,17 +35,15 @@ public class ApiLoggerTest extends AbstractLoggerTest {
3735
@BeforeClass
3836
public static void setUpClass() {
3937
System.setProperty("java.util.logging.manager", LogManager.class.getName());
40-
System.setProperty(Constants.LOGGER_ADAPTOR_PROPERTY, ApiLoggerAdapter.class.getName());
4138
}
4239

4340
@AfterClass
4441
public static void tearDownClass() {
4542
System.clearProperty("java.util.logging.manager");
46-
System.clearProperty(Constants.LOGGER_ADAPTOR_PROPERTY);
4743
}
4844

4945
@Before
50-
public void setUp() throws Exception {
46+
public void setUp() {
5147
logger = Logger.getLogger(LOGGER_NAME);
5248
logger.setFilter(null);
5349
assertThat(logger.getLevel(), equalTo(java.util.logging.Level.FINE));
@@ -60,7 +56,7 @@ public void setUp() throws Exception {
6056
}
6157

6258
@After
63-
public void tearDown() throws Exception {
59+
public void tearDown() {
6460
if (eventAppender != null) {
6561
eventAppender.clear();
6662
}
@@ -73,18 +69,18 @@ public void tearDown() throws Exception {
7369
}
7470

7571
@Test
76-
public void testGetParent() throws Exception {
72+
public void testGetParent() {
7773
final Logger parent = logger.getParent();
7874
assertNull("No parent logger should be automatically set up using log4j-api", parent);
7975
}
8076

8177
@Test(expected = UnsupportedOperationException.class)
82-
public void testSetParentFails() throws Exception {
78+
public void testSetParentFails() {
8379
logger.setParent(null);
8480
}
8581

8682
@Test
87-
public void testSetLevelFails() throws Exception {
83+
public void testSetLevelFails() {
8884
logger.setLevel(null);
8985
}
9086
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.logging.Logger;
2323
import org.apache.logging.log4j.core.LoggerContext;
2424
import org.apache.logging.log4j.core.test.appender.ListAppender;
25+
import org.apache.logging.log4j.jul.Constants;
26+
import org.apache.logging.log4j.jul.CoreLoggerAdapter;
2527
import org.apache.logging.log4j.jul.LogManager;
2628
import org.apache.logging.log4j.util.Strings;
2729
import org.junit.After;
@@ -56,11 +58,13 @@ private static Level getEffectiveLevel(final Logger logger) {
5658
@BeforeClass
5759
public static void setUpClass() {
5860
System.setProperty("java.util.logging.manager", LogManager.class.getName());
61+
System.setProperty(Constants.LOGGER_ADAPTOR_PROPERTY, CoreLoggerAdapter.class.getName());
5962
}
6063

6164
@AfterClass
6265
public static void tearDownClass() {
6366
System.clearProperty("java.util.logging.manager");
67+
System.clearProperty(Constants.LOGGER_ADAPTOR_PROPERTY);
6468
}
6569

6670
@Before
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="changed">
6+
<issue id="2353" link="https://github.com/apache/logging-log4j2/issues/2353"/>
7+
<description format="asciidoc">Disable level modification via JUL by default.</description>
8+
</entry>

src/site/antora/modules/ROOT/partials/manual/systemproperties/properties-log4j-jul.adoc

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,24 @@ Fully qualified name of an alternative `org.apache.logging.log4j.jul.LevelConver
6969
|===
7070
| Env. variable | `LOG4J_JUL_LOGGER_ADAPTER`
7171
| Type | `Class<? extends AbstractLoggerAdapter>`
72-
| Default value | _depends on classpath_
72+
| Default value | `org.apache.logging.log4j.jul.ApiLoggerAdapter`
7373
|===
7474
7575
Fully qualified class name of the `org.apache.logging.log4j.jul.AbstractLoggerAdapter` implementation to use.
7676
77-
This property allows users to choose between two implementation of the logging bridge:
77+
This property allows users to choose between two implementations of the logging bridge:
7878
79-
org.apache.logging.log4j.jul.CoreLoggerAdapter::
80-
The default if `log4j-core` is found in the class path.
79+
`org.apache.logging.log4j.jul.CoreLoggerAdapter`::
8180
It allows users to modify the Log4j Core configuration through the JUL https://docs.oracle.com/javase/{java-target-version}/docs/api/java/util/logging/Logger.html[`Logger`] interface.
81+
It requires the usage of the
82+
xref:manual/implementation.adoc[Log4j Core]
83+
implementation.
8284
83-
org.apache.logging.log4j.jul.ApiLoggerAdapter::
84-
The default if `log4j-core` cannot be found in the class path.
85-
It disables the level mutators in the JUL `Logger` interface.
85+
`org.apache.logging.log4j.jul.ApiLoggerAdapter`::
86+
It disables the level mutators in the JUL `Logger` interface.
87+
88+
[NOTE]
89+
====
90+
Since version 2.24.0 the default value changed to `ApiLoggerAdapter`.
91+
If you need to modify log levels via JUL, you need to select `CoreLoggerAdapter` explicitly.
92+
====

0 commit comments

Comments
 (0)