Skip to content

Commit 7107d14

Browse files
committed
Add warnings for other mutator methods
1 parent ca06bc0 commit 7107d14

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package org.apache.logging.log4j.jul;
1818

19+
import java.util.ResourceBundle;
1920
import java.util.logging.Filter;
21+
import java.util.logging.Handler;
2022
import java.util.logging.Level;
2123
import java.util.logging.LogRecord;
2224
import java.util.logging.Logger;
@@ -41,6 +43,12 @@
4143
*/
4244
public class ApiLogger extends Logger {
4345

46+
private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
47+
private static final String MUTATOR_DISABLED =
48+
"Ignoring call to `j.ul.Logger.{}({})`, since the Log4j API does not provide methods to modify the underlying implementation.\n"
49+
+ "To modify the configuration using JUL, use an `AbstractLoggerAdapter` appropriate for your logging implementation.\n"
50+
+ "See https://logging.apache.org/log4j/3.x/log4j-jul.html#log4j.jul.loggerAdapter for more information.";
51+
4452
private final WrappedLogger logger;
4553
private static final String FQCN = ApiLogger.class.getName();
4654

@@ -94,10 +102,7 @@ public Level getLevel() {
94102

95103
@Override
96104
public void setLevel(final Level newLevel) throws SecurityException {
97-
StatusLogger.getLogger()
98-
.error(
99-
"Cannot set JUL log level through log4j-api: " + "ignoring call to Logger.setLevel({})",
100-
newLevel);
105+
LOGGER.warn(MUTATOR_DISABLED, "setLevel", newLevel);
101106
// Some libraries rely on the following assertion:
102107
//
103108
// logger.setLevel(level);
@@ -118,6 +123,32 @@ protected void doSetLevel(final Level newLevel) throws SecurityException {
118123
super.setLevel(newLevel);
119124
}
120125

126+
@Override
127+
public void setUseParentHandlers(boolean useParentHandlers) {
128+
LOGGER.warn(MUTATOR_DISABLED, "setLevel", useParentHandlers);
129+
super.setUseParentHandlers(useParentHandlers);
130+
}
131+
132+
@Override
133+
public void addHandler(Handler handler) throws SecurityException {
134+
LOGGER.warn(MUTATOR_DISABLED, "addHandler", handler);
135+
super.addHandler(handler);
136+
}
137+
138+
@Override
139+
public void removeHandler(Handler handler) throws SecurityException {
140+
LOGGER.warn(MUTATOR_DISABLED, "removeHandler", handler);
141+
super.removeHandler(handler);
142+
}
143+
144+
@Override
145+
public void setResourceBundle(ResourceBundle bundle) {
146+
LOGGER.warn(
147+
"Ignoring call to `j.u.l.Logger.setResourceBundle({})`, since `o.a.l.l.jul.LogManager` currently does not support resource bundles.",
148+
bundle);
149+
super.setResourceBundle(bundle);
150+
}
151+
121152
/**
122153
* Unsupported operation.
123154
*

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020

2121
import java.util.List;
22+
import java.util.ResourceBundle;
23+
import java.util.logging.ConsoleHandler;
24+
import java.util.logging.Filter;
2225
import java.util.logging.Logger;
2326
import org.apache.logging.log4j.Level;
2427
import org.apache.logging.log4j.core.LogEvent;
@@ -240,6 +243,67 @@ public void testSetAndGetLevel() {
240243
}
241244
}
242245

246+
/**
247+
* The value of `useParentHandlers` should be recorded even if it is not effective.
248+
*/
249+
@Test
250+
public void testSetUseParentHandlers() {
251+
final Logger logger = Logger.getLogger(AbstractLoggerTest.class.getName() + ".testSetUseParentHandlers");
252+
253+
for (boolean useParentHandlers : new boolean[] {false, true}) {
254+
logger.setUseParentHandlers(useParentHandlers);
255+
assertThat(logger.getUseParentHandlers()).isEqualTo(useParentHandlers);
256+
}
257+
}
258+
259+
/**
260+
* The programmatically configured handlers should be recorded, even if they are not used.
261+
*/
262+
@Test
263+
public void testAddAndRemoveHandlers() {
264+
final Logger logger = Logger.getLogger(AbstractLoggerTest.class.getName() + ".testAddAndRemoveHandlers");
265+
266+
assertThat(logger.getHandlers()).isEmpty();
267+
// Add a handler
268+
ConsoleHandler handler = new ConsoleHandler();
269+
logger.addHandler(handler);
270+
assertThat(logger.getHandlers()).hasSize(1).containsExactly(handler);
271+
// Remove handler
272+
logger.removeHandler(handler);
273+
assertThat(logger.getHandlers()).isEmpty();
274+
}
275+
276+
/**
277+
* The programmatically configured filters should be recorded, even if they are not used.
278+
*/
279+
@Test
280+
public void testSetFilter() {
281+
final Logger logger = Logger.getLogger(AbstractLoggerTest.class.getName() + ".testSetFilter");
282+
283+
assertThat(logger.getFilter()).isNull();
284+
// Set filter
285+
Filter denyAllFilter = record -> false;
286+
logger.setFilter(denyAllFilter);
287+
assertThat(logger.getFilter()).isEqualTo(denyAllFilter);
288+
// Remove filter
289+
logger.setFilter(null);
290+
assertThat(logger.getFilter()).isNull();
291+
}
292+
293+
/**
294+
* The programmatically configured resource bundles should be recorded, even if they are not used.
295+
*/
296+
@Test
297+
public void testSetResourceBundle() {
298+
final Logger logger = Logger.getLogger(AbstractLoggerTest.class.getName() + ".testSetResourceBundle");
299+
300+
assertThat(logger.getResourceBundle()).isNull();
301+
// Set resource bundle
302+
ResourceBundle bundle = ResourceBundle.getBundle("testResourceBundle");
303+
logger.setResourceBundle(bundle);
304+
assertThat(logger.getResourceBundle()).isSameAs(bundle);
305+
}
306+
243307
private void testLambdaMessages(final String string) {
244308
final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
245309
root.info(() -> "Test info " + string);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
###
18+
# An empty resource bundle for tests

0 commit comments

Comments
 (0)