|
| 1 | +/* |
| 2 | + * Copyright 2012-present 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 java.util.function.BiConsumer; |
| 20 | +import java.util.function.Function; |
| 21 | + |
| 22 | +import org.jspecify.annotations.Nullable; |
| 23 | + |
| 24 | +import org.springframework.boot.logging.LogFile; |
| 25 | +import org.springframework.boot.logging.LoggingSystemProperties; |
| 26 | +import org.springframework.core.convert.ConversionFailedException; |
| 27 | +import org.springframework.core.convert.ConverterNotFoundException; |
| 28 | +import org.springframework.core.env.Environment; |
| 29 | +import org.springframework.core.env.PropertyResolver; |
| 30 | +import org.springframework.util.unit.DataSize; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link LoggingSystemProperties} for Log4j2. |
| 34 | + * |
| 35 | + * @author HoJoo Moon |
| 36 | + * @since 4.0.0 |
| 37 | + * @see Log4j2RollingPolicySystemProperty |
| 38 | + */ |
| 39 | +public class Log4j2LoggingSystemProperties extends LoggingSystemProperties { |
| 40 | + |
| 41 | + public Log4j2LoggingSystemProperties(Environment environment) { |
| 42 | + super(environment); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Create a new {@link Log4j2LoggingSystemProperties} instance. |
| 47 | + * @param environment the source environment |
| 48 | + * @param setter setter used to apply the property |
| 49 | + */ |
| 50 | + public Log4j2LoggingSystemProperties(Environment environment, |
| 51 | + @Nullable BiConsumer<String, @Nullable String> setter) { |
| 52 | + super(environment, setter); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Create a new {@link Log4j2LoggingSystemProperties} instance. |
| 57 | + * @param environment the source environment |
| 58 | + * @param defaultValueResolver function used to resolve default values or {@code null} |
| 59 | + * @param setter setter used to apply the property or {@code null} for system |
| 60 | + * properties |
| 61 | + */ |
| 62 | + public Log4j2LoggingSystemProperties(Environment environment, |
| 63 | + Function<@Nullable String, @Nullable String> defaultValueResolver, |
| 64 | + @Nullable BiConsumer<String, @Nullable String> setter) { |
| 65 | + super(environment, defaultValueResolver, setter); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void apply(@Nullable LogFile logFile, PropertyResolver resolver) { |
| 70 | + super.apply(logFile, resolver); |
| 71 | + applyRollingPolicyProperties(resolver); |
| 72 | + } |
| 73 | + |
| 74 | + private void applyRollingPolicyProperties(PropertyResolver resolver) { |
| 75 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.STRATEGY, resolver); |
| 76 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.TIME_INTERVAL, resolver, Integer.class); |
| 77 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.TIME_MODULATE, resolver, Boolean.class); |
| 78 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.CRON_SCHEDULE, resolver); |
| 79 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.FILE_NAME_PATTERN, resolver); |
| 80 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.MAX_FILE_SIZE, resolver, DataSize.class); |
| 81 | + applyRollingPolicy(Log4j2RollingPolicySystemProperty.MAX_HISTORY, resolver); |
| 82 | + } |
| 83 | + |
| 84 | + private void applyRollingPolicy(Log4j2RollingPolicySystemProperty property, PropertyResolver resolver) { |
| 85 | + applyRollingPolicy(property, resolver, String.class); |
| 86 | + } |
| 87 | + |
| 88 | + private <T> void applyRollingPolicy(Log4j2RollingPolicySystemProperty property, PropertyResolver resolver, |
| 89 | + Class<T> type) { |
| 90 | + T value = getProperty(resolver, property.getApplicationPropertyName(), type); |
| 91 | + if (value == null && property.getDeprecatedApplicationPropertyName() != null) { |
| 92 | + value = getProperty(resolver, property.getDeprecatedApplicationPropertyName(), type); |
| 93 | + } |
| 94 | + if (value != null) { |
| 95 | + String stringValue = String.valueOf((value instanceof DataSize dataSize) ? dataSize.toBytes() : value); |
| 96 | + setSystemProperty(property.getEnvironmentVariableName(), stringValue); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @SuppressWarnings("unchecked") |
| 101 | + private <T> @Nullable T getProperty(PropertyResolver resolver, String key, Class<T> type) { |
| 102 | + try { |
| 103 | + return resolver.getProperty(key, type); |
| 104 | + } |
| 105 | + catch (ConversionFailedException | ConverterNotFoundException ex) { |
| 106 | + if (type != DataSize.class) { |
| 107 | + throw ex; |
| 108 | + } |
| 109 | + // Fallback for Log4j2 compatibility - try parsing as string if DataSize |
| 110 | + // conversion fails |
| 111 | + String value = resolver.getProperty(key); |
| 112 | + if (value != null) { |
| 113 | + try { |
| 114 | + return (T) DataSize.parse(value); |
| 115 | + } |
| 116 | + catch (Exception parseEx) { |
| 117 | + ex.addSuppressed(parseEx); |
| 118 | + throw ex; |
| 119 | + } |
| 120 | + } |
| 121 | + return null; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments