Skip to content

Commit 423876f

Browse files
committed
Split new jul-to-log4j artifact from log4j-jul
This splits `log4j-jul` into two artifacts: - `jul-to-log4j` that contains a `j.u.l.LogManager` implementation, but does not depend on Log4j Core. - `log4j-jul` that contains a `j.u.l.Handler` implementation and depends on Log4j Core. We also update the `j.u.l.LogManager` implementation to: - implement methods introduced in Java 9, - remove methods deprecated in Java 9, - remove the support for `j.u.l.Filter`.
1 parent fe7914b commit 423876f

File tree

56 files changed

+2224
-1795
lines changed

Some content is hidden

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

56 files changed

+2224
-1795
lines changed

jul-to-log4j/pom.xml

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<parent>
21+
<groupId>org.apache.logging.log4j</groupId>
22+
<artifactId>log4j</artifactId>
23+
<version>${revision}</version>
24+
<relativePath>../log4j-parent</relativePath>
25+
</parent>
26+
27+
<artifactId>jul-to-log4j</artifactId>
28+
<name>Apache Log4j JUL LogManager</name>
29+
<description>A `java.util.logging` LogManager that forwards events to the Log4j API.</description>
30+
31+
<properties>
32+
<!--
33+
~ OSGi and JPMS options
34+
-->
35+
<bnd-extra-package-options>
36+
<!-- Optional annotations -->
37+
org.jspecify.*;resolution:=optional
38+
</bnd-extra-package-options>
39+
<bnd-extra-module-options>
40+
<!-- Optional modules can not be `transitive` -->
41+
org.jspecify;transitive=false
42+
</bnd-extra-module-options>
43+
</properties>
44+
45+
<dependencies>
46+
47+
<dependency>
48+
<groupId>org.apache.logging.log4j</groupId>
49+
<artifactId>log4j-api</artifactId>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>org.apache.logging.log4j</groupId>
54+
<artifactId>log4j-kit</artifactId>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>org.assertj</groupId>
59+
<artifactId>assertj-core</artifactId>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.hamcrest</groupId>
65+
<artifactId>hamcrest</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<scope>test</scope>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.apache.logging.log4j</groupId>
77+
<artifactId>log4j-async-logger</artifactId>
78+
<scope>test</scope>
79+
</dependency>
80+
81+
<dependency>
82+
<groupId>org.apache.logging.log4j</groupId>
83+
<artifactId>log4j-core</artifactId>
84+
<scope>test</scope>
85+
</dependency>
86+
87+
<dependency>
88+
<groupId>org.apache.logging.log4j</groupId>
89+
<artifactId>log4j-core-test</artifactId>
90+
<scope>test</scope>
91+
</dependency>
92+
</dependencies>
93+
94+
<build>
95+
<plugins>
96+
<plugin>
97+
<groupId>org.apache.maven.plugins</groupId>
98+
<artifactId>maven-surefire-plugin</artifactId>
99+
<configuration>
100+
<systemPropertyVariables>
101+
<java.awt.headless>true</java.awt.headless>
102+
</systemPropertyVariables>
103+
<argLine>-Xms256m -Xmx1024m</argLine>
104+
<forkCount>1</forkCount>
105+
<reuseForks>false</reuseForks>
106+
</configuration>
107+
<dependencies>
108+
<!-- The `surefire-junit-platform` provider initializes JUL before tests start -->
109+
<dependency>
110+
<groupId>org.apache.maven.surefire</groupId>
111+
<artifactId>surefire-junit47</artifactId>
112+
<version>${surefire.version}</version>
113+
</dependency>
114+
</dependencies>
115+
<executions>
116+
<execution>
117+
<id>default-test</id>
118+
<goals>
119+
<goal>test</goal>
120+
</goals>
121+
<phase>test</phase>
122+
<configuration>
123+
<!-- Use custom `j.u.l.LogManager` -->
124+
<systemPropertyVariables>
125+
<java.util.logging.manager>org.apache.logging.jul.tolog4j.LogManager</java.util.logging.manager>
126+
</systemPropertyVariables>
127+
</configuration>
128+
</execution>
129+
</executions>
130+
</plugin>
131+
</plugins>
132+
</build>
133+
</project>

log4j-jul/src/main/java/org/apache/logging/log4j/jul/LevelTranslator.java renamed to jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LevelTranslator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.jul;
17+
package org.apache.logging.jul.tolog4j;
1818

19+
import org.apache.logging.jul.tolog4j.internal.DefaultLevelConverter;
20+
import org.apache.logging.jul.tolog4j.internal.JulProperties;
21+
import org.apache.logging.jul.tolog4j.spi.LevelConverter;
1922
import org.apache.logging.log4j.Level;
2023
import org.apache.logging.log4j.Logger;
2124
import org.apache.logging.log4j.kit.env.PropertyEnvironment;

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

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,40 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.jul;
17+
package org.apache.logging.jul.tolog4j;
1818

1919
import java.util.Collections;
2020
import java.util.Enumeration;
2121
import java.util.HashSet;
2222
import java.util.Set;
2323
import java.util.logging.Logger;
24-
import org.apache.logging.log4j.LoggingException;
24+
import org.apache.logging.jul.tolog4j.internal.ApiLoggerAdapter;
25+
import org.apache.logging.jul.tolog4j.internal.JulProperties;
26+
import org.apache.logging.jul.tolog4j.internal.NoOpLogger;
27+
import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter;
2528
import org.apache.logging.log4j.kit.env.PropertyEnvironment;
2629
import org.apache.logging.log4j.status.StatusLogger;
2730
import org.apache.logging.log4j.util.LoaderUtil;
2831

2932
/**
30-
* Log4j implementation of {@link java.util.logging.LogManager}. Note that the system property
31-
* {@code java.util.logging.manager} must be set to {@code org.apache.logging.log4j.jul.LogManager} in order to use
32-
* this adaptor. This LogManager requires the {@code log4j-api} library to be available. If {@code log4j-core} is
33-
* also available, then more features of {@link java.util.logging.Logger} are supported.
34-
*
35-
* <p>To override the default {@link AbstractLoggerAdapter} that is used, specify the Log4j property
36-
* {@code log4j.jul.LoggerAdapter} and set it to the fully qualified class name of a custom
37-
* implementation. All implementations must have a default constructor.</p>
33+
* Log4j implementation of {@link java.util.logging.LogManager}.
34+
* <p>
35+
* Note that the system property {@code java.util.logging.manager} must be set to
36+
* {@code org.apache.logging.jul.tolog4j.LogManager} in order to use this adaptor.
37+
* This LogManager requires the {@code log4j-api} library to be available.
38+
* </p>
39+
* <p>
40+
* To override the default {@link AbstractLoggerAdapter} that is used, specify the Log4j property
41+
* {@code log4j.jul.LoggerAdapter} and set it to the fully qualified class name of a custom
42+
* implementation.
43+
* All implementations must have a default constructor.
44+
* </p>
3845
*
3946
* @since 2.1
4047
*/
4148
public class LogManager extends java.util.logging.LogManager {
4249

4350
private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
44-
private static final String CORE_LOGGER_CLASS_NAME = "org.apache.logging.log4j.core.Logger";
45-
private static final String CORE_LOGGER_ADAPTER_CLASS_NAME = "org.apache.logging.log4j.jul.CoreLoggerAdapter";
46-
private static final String API_LOGGER_ADAPTER_CLASS_NAME = "org.apache.logging.log4j.jul.ApiLoggerAdapter";
4751
private final AbstractLoggerAdapter loggerAdapter;
4852
// Contains the set of logger names that are actively being requested using getLogger.
4953
private final ThreadLocal<Set<String>> recursive = ThreadLocal.withInitial(HashSet::new);
@@ -62,21 +66,9 @@ public LogManager() {
6266
}
6367
}
6468
if (adapter == null) {
65-
// default adapter
66-
String adapterClassName;
67-
try {
68-
// find out if log4j-core is available
69-
LoaderUtil.loadClass(CORE_LOGGER_CLASS_NAME);
70-
adapterClassName = CORE_LOGGER_ADAPTER_CLASS_NAME;
71-
} catch (final ClassNotFoundException ignored) {
72-
adapterClassName = API_LOGGER_ADAPTER_CLASS_NAME;
73-
}
74-
LOGGER.debug("Attempting to use {}", adapterClassName);
75-
try {
76-
adapter = LoaderUtil.newCheckedInstanceOf(adapterClassName, AbstractLoggerAdapter.class);
77-
} catch (final Exception e) {
78-
throw LOGGER.throwing(new LoggingException(e));
79-
}
69+
// Use API by default
70+
// See https://github.com/apache/logging-log4j2/issues/2353
71+
adapter = new ApiLoggerAdapter();
8072
}
8173
loggerAdapter = adapter;
8274
LOGGER.info("Registered Log4j as the java.util.logging.LogManager.");
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.jul.tolog4j.internal;
18+
19+
import java.util.logging.Filter;
20+
import java.util.logging.Handler;
21+
import java.util.logging.Level;
22+
import java.util.logging.Logger;
23+
import org.apache.logging.jul.tolog4j.support.AbstractLogger;
24+
import org.apache.logging.log4j.spi.ExtendedLogger;
25+
import org.apache.logging.log4j.status.StatusLogger;
26+
27+
/**
28+
* Implementation of {@link java.util.logging.Logger} that ignores all method calls that do not have an equivalent in
29+
* the Log4j API.
30+
*/
31+
public class ApiLogger extends AbstractLogger {
32+
33+
private static final String MUTATOR_DISABLED =
34+
"""
35+
Ignoring call to `j.ul.Logger.{}()`, since the Log4j API does not provide methods to modify the underlying implementation.
36+
To modify the configuration using JUL, use an `AbstractLoggerAdapter` appropriate for your logging implementation.
37+
See https://logging.apache.org/log4j/3.x/log4j-jul.html#log4j.jul.loggerAdapter for more information.""";
38+
private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger();
39+
40+
public ApiLogger(ExtendedLogger logger) {
41+
super(logger);
42+
}
43+
44+
@Override
45+
public void setFilter(Filter newFilter) throws SecurityException {
46+
LOGGER.warn(MUTATOR_DISABLED, "setFilter");
47+
}
48+
49+
@Override
50+
public void setLevel(Level newLevel) throws SecurityException {
51+
LOGGER.warn(MUTATOR_DISABLED, "setLevel");
52+
}
53+
54+
@Override
55+
public void addHandler(Handler handler) throws SecurityException {
56+
LOGGER.warn(MUTATOR_DISABLED, "addHandler");
57+
}
58+
59+
@Override
60+
public void removeHandler(Handler handler) throws SecurityException {
61+
LOGGER.warn(MUTATOR_DISABLED, "removeHandler");
62+
}
63+
64+
@Override
65+
public void setUseParentHandlers(boolean useParentHandlers) {
66+
LOGGER.warn(MUTATOR_DISABLED, "setUseParentHandlers");
67+
}
68+
69+
@Override
70+
public void setParent(Logger parent) {
71+
throw new UnsupportedOperationException(
72+
ApiLogger.class.getSimpleName() + " does not support `j.u.l.Logger#setParent()`.");
73+
}
74+
}

log4j-jul/src/main/java/org/apache/logging/log4j/jul/ApiLoggerAdapter.java renamed to jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLoggerAdapter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.jul;
17+
package org.apache.logging.jul.tolog4j.internal;
1818

1919
import java.util.logging.Logger;
20+
import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter;
2021
import org.apache.logging.log4j.message.MessageFactory;
2122
import org.apache.logging.log4j.message.MessageFormatMessageFactory;
2223
import org.apache.logging.log4j.spi.LoggerContext;
@@ -32,7 +33,7 @@ public class ApiLoggerAdapter extends AbstractLoggerAdapter {
3233
private static final MessageFactory MESSAGE_FACTORY = new MessageFormatMessageFactory();
3334

3435
@Override
35-
protected Logger newLogger(final String name, final LoggerContext context) {
36+
public Logger newLogger(final String name, final LoggerContext context) {
3637
return new ApiLogger(context.getLogger(name, MESSAGE_FACTORY));
3738
}
3839
}

log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java renamed to jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/DefaultLevelConverter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.jul;
17+
package org.apache.logging.jul.tolog4j.internal;
1818

1919
import java.util.ArrayList;
2020
import java.util.Collections;
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import java.util.concurrent.ConcurrentMap;
27+
import org.apache.logging.jul.tolog4j.LevelTranslator;
28+
import org.apache.logging.jul.tolog4j.spi.LevelConverter;
2729
import org.apache.logging.log4j.Level;
2830

2931
/**

log4j-jul/src/main/java/org/apache/logging/log4j/jul/JulProperties.java renamed to jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/JulProperties.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.jul;
17+
package org.apache.logging.jul.tolog4j.internal;
1818

19+
import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter;
20+
import org.apache.logging.jul.tolog4j.spi.LevelConverter;
1921
import org.apache.logging.log4j.kit.env.Log4jProperty;
2022
import org.jspecify.annotations.Nullable;
2123

log4j-jul/src/main/java/org/apache/logging/log4j/jul/NoOpLogger.java renamed to jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/NoOpLogger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
package org.apache.logging.log4j.jul;
17+
package org.apache.logging.jul.tolog4j.internal;
1818

1919
import java.util.ResourceBundle;
2020
import java.util.function.Supplier;
@@ -27,7 +27,7 @@
2727
*/
2828
public class NoOpLogger extends Logger {
2929

30-
protected NoOpLogger(final String name) {
30+
public NoOpLogger(final String name) {
3131
super(name, null);
3232
}
3333

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
@Export
18+
@Version("3.0.0")
19+
package org.apache.logging.jul.tolog4j;
20+
21+
import org.osgi.annotation.bundle.Export;
22+
import org.osgi.annotation.versioning.Version;

0 commit comments

Comments
 (0)