Skip to content

Commit 27ed30f

Browse files
rgoersphilwebb
authored andcommitted
Support profile specific Log4j2 configuration
Add a `SpringProfileArbiter` Log4j2 plugin which allows Log4j2 configuration to be included or skipped based on the active Spring `Environment` profiles. See gh-32734
1 parent 890c364 commit 27ed30f

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging.log4j2;
18+
19+
import org.apache.logging.log4j.Logger;
20+
import org.apache.logging.log4j.core.LoggerContext;
21+
import org.apache.logging.log4j.core.config.Configuration;
22+
import org.apache.logging.log4j.core.config.Node;
23+
import org.apache.logging.log4j.core.config.arbiters.Arbiter;
24+
import org.apache.logging.log4j.core.config.plugins.Plugin;
25+
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
26+
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
27+
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
28+
import org.apache.logging.log4j.core.config.plugins.PluginLoggerContext;
29+
import org.apache.logging.log4j.status.StatusLogger;
30+
31+
import org.springframework.core.env.Environment;
32+
import org.springframework.core.env.Profiles;
33+
import org.springframework.util.StringUtils;
34+
35+
/**
36+
* An Arbiter that uses the active Spring profile to determine if configuration should be
37+
* included.
38+
*
39+
* @author Ralph Goers
40+
* @since 3.0.0
41+
*/
42+
@Plugin(name = "SpringProfile", category = Node.CATEGORY, elementType = Arbiter.ELEMENT_TYPE, deferChildren = true,
43+
printObject = true)
44+
public final class SpringProfileArbiter implements Arbiter {
45+
46+
private final String[] profileNames;
47+
48+
private final Environment environment;
49+
50+
private SpringProfileArbiter(final String[] profiles, Environment environment) {
51+
this.profileNames = profiles;
52+
this.environment = environment;
53+
}
54+
55+
@Override
56+
public boolean isCondition() {
57+
if (this.environment == null) {
58+
return false;
59+
}
60+
61+
if (this.profileNames.length == 0) {
62+
return false;
63+
}
64+
return this.environment.acceptsProfiles(Profiles.of(this.profileNames));
65+
}
66+
67+
@PluginBuilderFactory
68+
public static Builder newBuilder() {
69+
return new Builder();
70+
}
71+
72+
/**
73+
* Standard Builder to create the Arbiter.
74+
*/
75+
public static class Builder implements org.apache.logging.log4j.core.util.Builder<SpringProfileArbiter> {
76+
77+
private static final Logger LOGGER = StatusLogger.getLogger();
78+
79+
/**
80+
* Attribute name identifier.
81+
*/
82+
public static final String ATTR_NAME = "name";
83+
84+
@PluginBuilderAttribute(ATTR_NAME)
85+
private String name;
86+
87+
@PluginConfiguration
88+
private Configuration configuration;
89+
90+
@PluginLoggerContext
91+
private LoggerContext loggerContext;
92+
93+
/**
94+
* Sets the Profile Name or Names.
95+
* @param name the profile name(s).
96+
* @return this
97+
*/
98+
public Builder setName(final String name) {
99+
this.name = name;
100+
return asBuilder();
101+
}
102+
103+
public Builder setConfiguration(final Configuration configuration) {
104+
this.configuration = configuration;
105+
return asBuilder();
106+
}
107+
108+
public Builder setLoggerContext(final LoggerContext loggerContext) {
109+
this.loggerContext = loggerContext;
110+
return asBuilder();
111+
}
112+
113+
private SpringProfileArbiter.Builder asBuilder() {
114+
return this;
115+
}
116+
117+
public SpringProfileArbiter build() {
118+
String[] profileNames = StringUtils.trimArrayElements(StringUtils
119+
.commaDelimitedListToStringArray(this.configuration.getStrSubstitutor().replace(this.name)));
120+
Environment environment = null;
121+
if (this.loggerContext != null) {
122+
environment = (Environment) this.loggerContext.getObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
123+
if (environment == null) {
124+
LOGGER.warn("Cannot create Arbiter, no Spring Environment provided");
125+
return null;
126+
}
127+
128+
return new SpringProfileArbiter(profileNames, environment);
129+
}
130+
else {
131+
LOGGER.warn("Cannot create Arbiter, LoggerContext is not available");
132+
}
133+
return null;
134+
}
135+
136+
}
137+
138+
}

0 commit comments

Comments
 (0)