Skip to content

Commit 9139fe9

Browse files
committed
fix: Move constants to Log4J2LoggingSystem
Signed-off-by: Piotr P. Karwasz <piotr@github.copernik.eu>
1 parent 8f0d554 commit 9139fe9

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,44 +90,50 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
9090

9191
private static final String OPTIONAL_PREFIX = "optional:";
9292

93-
private static final String LOG4J_BRIDGE_HANDLER = "org.apache.logging.log4j.jul.Log4jBridgeHandler";
93+
/**
94+
* JUL handler that routes messages to the Log4j API (optional dependency).
95+
*/
96+
static final String LOG4J_BRIDGE_HANDLER = "org.apache.logging.log4j.jul.Log4jBridgeHandler";
9497

95-
private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager";
98+
/**
99+
* JUL LogManager that routes messages to the Log4j API as the backend.
100+
*/
101+
static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager";
96102

97103
/**
98104
* JSON tree parser used by Log4j 2 (optional dependency).
99105
*/
100-
private static final String JSON_TREE_PARSER_V2 = "com.fasterxml.jackson.databind.ObjectMapper";
106+
static final String JSON_TREE_PARSER_V2 = "com.fasterxml.jackson.databind.ObjectMapper";
101107

102108
/**
103109
* JSON tree parser embedded in Log4j 3.
104110
*/
105-
private static final String JSON_TREE_PARSER_V3 = "org.apache.logging.log4j.kit.json.JsonReader";
111+
static final String JSON_TREE_PARSER_V3 = "org.apache.logging.log4j.kit.json.JsonReader";
106112

107113
/**
108114
* Configuration factory for properties files (Log4j 2).
109115
*/
110-
private static final String PROPS_CONFIGURATION_FACTORY_V2 = "org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory";
116+
static final String PROPS_CONFIGURATION_FACTORY_V2 = "org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory";
111117

112118
/**
113119
* Configuration factory for properties files (Log4j 3, optional dependency).
114120
*/
115-
private static final String PROPS_CONFIGURATION_FACTORY_V3 = "org.apache.logging.log4j.config.properties.JavaPropsConfigurationFactory";
121+
static final String PROPS_CONFIGURATION_FACTORY_V3 = "org.apache.logging.log4j.config.properties.JavaPropsConfigurationFactory";
116122

117123
/**
118124
* YAML tree parser used by Log4j 2 (optional dependency).
119125
*/
120-
private static final String YAML_TREE_PARSER_V2 = "com.fasterxml.jackson.dataformat.yaml.YAMLMapper";
126+
static final String YAML_TREE_PARSER_V2 = "com.fasterxml.jackson.dataformat.yaml.YAMLMapper";
121127

122128
/**
123129
* Configuration factory for YAML files (Log4j 2, embedded).
124130
*/
125-
private static final String YAML_CONFIGURATION_FACTORY_V2 = "org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory";
131+
static final String YAML_CONFIGURATION_FACTORY_V2 = "org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory";
126132

127133
/**
128134
* Configuration factory for YAML files (Log4j 3, optional dependency).
129135
*/
130-
private static final String YAML_CONFIGURATION_FACTORY_V3 = "org.apache.logging.log4j.config.yaml.YamlConfigurationFactory";
136+
static final String YAML_CONFIGURATION_FACTORY_V3 = "org.apache.logging.log4j.config.yaml.YamlConfigurationFactory";
131137

132138
private static final SpringEnvironmentPropertySource propertySource = new SpringEnvironmentPropertySource();
133139

@@ -260,11 +266,11 @@ private boolean isJulUsingASingleConsoleHandlerAtMost() {
260266

261267
private boolean isLog4jLogManagerInstalled() {
262268
final String logManagerClassName = java.util.logging.LogManager.getLogManager().getClass().getName();
263-
return Log4J2RuntimeHints.LOG4J_LOG_MANAGER.equals(logManagerClassName);
269+
return LOG4J_LOG_MANAGER.equals(logManagerClassName);
264270
}
265271

266272
private boolean isLog4jBridgeHandlerAvailable() {
267-
return ClassUtils.isPresent(Log4J2RuntimeHints.LOG4J_BRIDGE_HANDLER, getClassLoader());
273+
return ClassUtils.isPresent(LOG4J_BRIDGE_HANDLER, getClassLoader());
268274
}
269275

270276
private void removeLog4jBridgeHandler() {
@@ -616,8 +622,10 @@ protected String getDefaultLogCorrelationPattern() {
616622
@Order(0)
617623
public static class Factory implements LoggingSystemFactory {
618624

619-
private static final boolean PRESENT = ClassUtils
620-
.isPresent("org.apache.logging.log4j.core.impl.Log4jContextFactory", Factory.class.getClassLoader());
625+
static final String LOG4J_CORE_CONTEXT_FACTORY = "org.apache.logging.log4j.core.impl.Log4jContextFactory";
626+
627+
private static final boolean PRESENT = ClassUtils.isPresent(LOG4J_CORE_CONTEXT_FACTORY,
628+
Factory.class.getClassLoader());
621629

622630
@Override
623631
public @Nullable LoggingSystem getLoggingSystem(ClassLoader classLoader) {

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2RuntimeHints.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,27 @@
2727
*/
2828
class Log4J2RuntimeHints implements RuntimeHintsRegistrar {
2929

30-
// Log4j2LoggingSystem checks for the presence of these classes reflectively.
31-
static final String PROVIDER = "org.apache.logging.log4j.core.impl.Log4jProvider";
32-
33-
// Tree parsers used by Log4j 2 for configuration files.
34-
static final String JSON_TREE_PARSER_V2 = "com.fasterxml.jackson.databind.ObjectMapper";
35-
static final String YAML_TREE_PARSER_V2 = "com.fasterxml.jackson.dataformat.yaml.YAMLMapper";
36-
37-
// JUL implementations that use Log4j 2 API.
38-
static final String LOG4J_BRIDGE_HANDLER = "org.apache.logging.log4j.jul.Log4jBridgeHandler";
39-
static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager";
40-
4130
@Override
4231
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
43-
if (!ClassUtils.isPresent(PROVIDER, classLoader)) {
32+
if (!ClassUtils.isPresent(Log4J2LoggingSystem.Factory.LOG4J_CORE_CONTEXT_FACTORY, classLoader)) {
4433
return;
4534
}
46-
registerTypeForReachability(hints, classLoader, PROVIDER);
35+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.Factory.LOG4J_CORE_CONTEXT_FACTORY);
4736
// Register default Log4j2 configuration files
4837
hints.resources().registerPattern("org/springframework/boot/logging/log4j2/log4j2.xml");
4938
hints.resources().registerPattern("org/springframework/boot/logging/log4j2/log4j2-file.xml");
5039
hints.resources().registerPattern("log4j2.springboot");
5140
// Declares the types that Log4j2LoggingSystem checks for existence reflectively.
52-
registerTypeForReachability(hints, classLoader, JSON_TREE_PARSER_V2);
53-
registerTypeForReachability(hints, classLoader, YAML_TREE_PARSER_V2);
54-
registerTypeForReachability(hints, classLoader, LOG4J_BRIDGE_HANDLER);
55-
registerTypeForReachability(hints, classLoader, LOG4J_LOG_MANAGER);
41+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.JSON_TREE_PARSER_V2);
42+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.JSON_TREE_PARSER_V3);
43+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.PROPS_CONFIGURATION_FACTORY_V2);
44+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.PROPS_CONFIGURATION_FACTORY_V3);
45+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.YAML_TREE_PARSER_V2);
46+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.YAML_CONFIGURATION_FACTORY_V2);
47+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.YAML_CONFIGURATION_FACTORY_V3);
48+
// Register JUL to Log4j 2 bridge handler
49+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.LOG4J_BRIDGE_HANDLER);
50+
registerTypeForReachability(hints, classLoader, Log4J2LoggingSystem.LOG4J_LOG_MANAGER);
5651
// Don't need to register the custom Log4j 2 plugins,
5752
// since they will be registered by the Log4j 2 `GraalvmPluginProcessor`.
5853
}

core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2RuntimeHintsTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.logging.log4j2;
1818

19-
import com.fasterxml.jackson.databind.ObjectMapper;
20-
import org.apache.logging.log4j.core.impl.Log4jProvider;
19+
import org.apache.logging.log4j.core.impl.Log4jContextFactory;
2120
import org.apache.logging.log4j.jul.Log4jBridgeHandler;
2221
import org.apache.logging.log4j.jul.LogManager;
2322
import org.junit.jupiter.api.Test;
@@ -38,16 +37,21 @@ class Log4J2RuntimeHintsTests {
3837
@Test
3938
void registersHintsForTypesCheckedByLog4J2LoggingSystem() {
4039
ReflectionHints reflection = registerHints();
41-
assertThat(reflection.getTypeHint(Log4jProvider.class)).isNotNull();
40+
// Once Log4j Core is reachable, GraalVM will automatically
41+
// add reachability metadata embedded in the Log4j Core jar and extensions.
42+
assertThat(reflection.getTypeHint(Log4jContextFactory.class)).isNotNull();
4243
assertThat(reflection.getTypeHint(Log4jBridgeHandler.class)).isNotNull();
4344
assertThat(reflection.getTypeHint(LogManager.class)).isNotNull();
4445
}
4546

47+
/**
48+
*
49+
*/
4650
@Test
4751
void registersHintsForConfigurationFileParsers() {
4852
ReflectionHints reflection = registerHints();
4953
// JSON
50-
assertThat(reflection.getTypeHint(ObjectMapper.class)).isNotNull();
54+
assertThat(reflection.getTypeHint(TypeReference.of("com.fasterxml.jackson.databind.ObjectMapper"))).isNotNull();
5155
// YAML
5256
assertThat(reflection.getTypeHint(TypeReference.of("com.fasterxml.jackson.dataformat.yaml.YAMLMapper")))
5357
.isNotNull();

0 commit comments

Comments
 (0)