-
Notifications
You must be signed in to change notification settings - Fork 41.7k
Add Log4j2 rolling policy configuration #47260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hojooo
wants to merge
13
commits into
spring-projects:main
Choose a base branch
from
hojooo:log4j2-rolling-policy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+731
−34
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ec926ad
Add Log4j2 rolling policy configuration support
hojooo 2dee5b2
Fix Log4J2 rolling policy metadata ordering
hojooo 15c71cf
Format Log4J2 logging system sources
hojooo 25aad9e
Replace Objects.requireNonNull in Log4J2 policy
hojooo 5b6ded7
Annotate Log4J2 test helper as nullable
hojooo ec627ff
Format Author
hojooo c0674a2
Format Import
hojooo 26db70e
Move Log4J2 test inner types
hojooo dd4e6b3
Move Log4J2 test inner types
hojooo 83aff52
Remove Redundant Import
hojooo ad04ebb
Ensure logging systems use their specific system properties
hojooo dcd09d2
Change SpringBootTriggeringPolicy to Abstract Class
hojooo a5e0f47
Remove unsupported Log4j2 rolling policy properties
hojooo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
.../src/main/java/org/springframework/boot/logging/log4j2/Log4j2LoggingSystemProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.boot.logging.log4j2; | ||
|
|
||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Function; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import org.springframework.boot.logging.LogFile; | ||
| import org.springframework.boot.logging.LoggingSystemProperties; | ||
| import org.springframework.core.convert.ConversionFailedException; | ||
| import org.springframework.core.convert.ConverterNotFoundException; | ||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.core.env.PropertyResolver; | ||
| import org.springframework.util.unit.DataSize; | ||
|
|
||
| /** | ||
| * {@link LoggingSystemProperties} for Log4j2. | ||
| * | ||
| * @author HoJoo Moon | ||
| * @since 4.0.0 | ||
| * @see Log4j2RollingPolicySystemProperty | ||
| */ | ||
| public class Log4j2LoggingSystemProperties extends LoggingSystemProperties { | ||
|
|
||
| public Log4j2LoggingSystemProperties(Environment environment) { | ||
| super(environment); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link Log4j2LoggingSystemProperties} instance. | ||
| * @param environment the source environment | ||
| * @param setter setter used to apply the property | ||
| */ | ||
| public Log4j2LoggingSystemProperties(Environment environment, | ||
| @Nullable BiConsumer<String, @Nullable String> setter) { | ||
| super(environment, setter); | ||
| } | ||
|
|
||
| /** | ||
| * Create a new {@link Log4j2LoggingSystemProperties} instance. | ||
| * @param environment the source environment | ||
| * @param defaultValueResolver function used to resolve default values or {@code null} | ||
| * @param setter setter used to apply the property or {@code null} for system | ||
| * properties | ||
| */ | ||
| public Log4j2LoggingSystemProperties(Environment environment, | ||
| Function<@Nullable String, @Nullable String> defaultValueResolver, | ||
| @Nullable BiConsumer<String, @Nullable String> setter) { | ||
| super(environment, defaultValueResolver, setter); | ||
| } | ||
|
|
||
| @Override | ||
| protected void apply(@Nullable LogFile logFile, PropertyResolver resolver) { | ||
| super.apply(logFile, resolver); | ||
| applyRollingPolicyProperties(resolver); | ||
| } | ||
|
|
||
| private void applyRollingPolicyProperties(PropertyResolver resolver) { | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.STRATEGY, resolver); | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.TIME_INTERVAL, resolver, Integer.class); | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.TIME_MODULATE, resolver, Boolean.class); | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.CRON_SCHEDULE, resolver); | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.FILE_NAME_PATTERN, resolver); | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.MAX_FILE_SIZE, resolver, DataSize.class); | ||
| applyRollingPolicy(Log4j2RollingPolicySystemProperty.MAX_HISTORY, resolver); | ||
| } | ||
|
|
||
| private void applyRollingPolicy(Log4j2RollingPolicySystemProperty property, PropertyResolver resolver) { | ||
| applyRollingPolicy(property, resolver, String.class); | ||
| } | ||
|
|
||
| private <T> void applyRollingPolicy(Log4j2RollingPolicySystemProperty property, PropertyResolver resolver, | ||
| Class<T> type) { | ||
| T value = getProperty(resolver, property.getApplicationPropertyName(), type); | ||
| if (value == null && property.getDeprecatedApplicationPropertyName() != null) { | ||
| value = getProperty(resolver, property.getDeprecatedApplicationPropertyName(), type); | ||
| } | ||
| if (value != null) { | ||
| String stringValue = String.valueOf((value instanceof DataSize dataSize) ? dataSize.toBytes() : value); | ||
| setSystemProperty(property.getEnvironmentVariableName(), stringValue); | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private <T> @Nullable T getProperty(PropertyResolver resolver, String key, Class<T> type) { | ||
| try { | ||
| return resolver.getProperty(key, type); | ||
| } | ||
| catch (ConversionFailedException | ConverterNotFoundException ex) { | ||
| if (type != DataSize.class) { | ||
| throw ex; | ||
| } | ||
| // Fallback for Log4j2 compatibility - try parsing as string if DataSize | ||
| // conversion fails | ||
| String value = resolver.getProperty(key); | ||
| if (value != null) { | ||
| try { | ||
| return (T) DataSize.parse(value); | ||
| } | ||
| catch (Exception parseEx) { | ||
| ex.addSuppressed(parseEx); | ||
| throw ex; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| } | ||
95 changes: 95 additions & 0 deletions
95
.../main/java/org/springframework/boot/logging/log4j2/Log4j2RollingPolicySystemProperty.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright 2012-present the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.boot.logging.log4j2; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Log4j2 rolling policy system properties that can later be used by log configuration | ||
| * files. | ||
| * | ||
| * @author HoJoo Moon | ||
| * @since 4.0.0 | ||
| * @see Log4j2LoggingSystemProperties | ||
| */ | ||
| public enum Log4j2RollingPolicySystemProperty { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be public? |
||
|
|
||
| /** | ||
| * Logging system property for the rolled-over log file name pattern. | ||
| */ | ||
| FILE_NAME_PATTERN("file-name-pattern", "logging.pattern.rolling-file-name"), | ||
|
|
||
| /** | ||
| * Logging system property for the file log max size. | ||
| */ | ||
| MAX_FILE_SIZE("max-file-size", "logging.file.max-size"), | ||
|
|
||
| /** | ||
| * Logging system property for the file log max history. | ||
| */ | ||
| MAX_HISTORY("max-history", "logging.file.max-history"), | ||
|
|
||
| /** | ||
| * Logging system property for the rolling policy strategy. | ||
| */ | ||
| STRATEGY("strategy", null), | ||
|
|
||
| /** | ||
| * Logging system property for the rolling policy time interval. | ||
| */ | ||
| TIME_INTERVAL("time-based.interval", null), | ||
|
|
||
| /** | ||
| * Logging system property for the rolling policy time modulate flag. | ||
| */ | ||
| TIME_MODULATE("time-based.modulate", null), | ||
|
|
||
| /** | ||
| * Logging system property for the cron based schedule. | ||
| */ | ||
| CRON_SCHEDULE("cron.schedule", null); | ||
|
|
||
| private final String environmentVariableName; | ||
|
|
||
| private final String applicationPropertyName; | ||
|
|
||
| private final @Nullable String deprecatedApplicationPropertyName; | ||
|
|
||
| Log4j2RollingPolicySystemProperty(String applicationPropertyName, | ||
| @Nullable String deprecatedApplicationPropertyName) { | ||
| this.environmentVariableName = "LOG4J2_ROLLINGPOLICY_" + name(); | ||
| this.applicationPropertyName = "logging.log4j2.rollingpolicy." + applicationPropertyName; | ||
| this.deprecatedApplicationPropertyName = deprecatedApplicationPropertyName; | ||
| } | ||
|
|
||
| /** | ||
| * Return the name of environment variable that can be used to access this property. | ||
| * @return the environment variable name | ||
| */ | ||
| public String getEnvironmentVariableName() { | ||
| return this.environmentVariableName; | ||
| } | ||
|
|
||
| String getApplicationPropertyName() { | ||
| return this.applicationPropertyName; | ||
| } | ||
|
|
||
| @Nullable String getDeprecatedApplicationPropertyName() { | ||
| return this.deprecatedApplicationPropertyName; | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.