Commit 76a1295e authored by liaozan's avatar liaozan 🏀

Ensure the PropertiesPreparedEventListener can print log correctly

parent a1c2069a
...@@ -35,10 +35,13 @@ class ConfigurablePropertiesLoader { ...@@ -35,10 +35,13 @@ class ConfigurablePropertiesLoader {
*/ */
private static final String PROPERTIES_PROPERTY_SOURCE = "ConfigurablePropertiesPropertySource"; private static final String PROPERTIES_PROPERTY_SOURCE = "ConfigurablePropertiesPropertySource";
private final DeferredLogFactory deferredLogFactory;
private final Log log; private final Log log;
ConfigurablePropertiesLoader(DeferredLogFactory logFactory) { ConfigurablePropertiesLoader(DeferredLogFactory deferredLogFactory) {
this.log = logFactory.getLog(ConfigurablePropertiesLoader.class); this.deferredLogFactory = deferredLogFactory;
this.log = deferredLogFactory.getLog(ConfigurablePropertiesLoader.class);
} }
void load(ConfigurableEnvironment environment, SpringApplication application) { void load(ConfigurableEnvironment environment, SpringApplication application) {
...@@ -74,7 +77,7 @@ class ConfigurablePropertiesLoader { ...@@ -74,7 +77,7 @@ class ConfigurablePropertiesLoader {
compositePropertySource.addPropertySource(propertySource); compositePropertySource.addPropertySource(propertySource);
ConfigurableProperties boundProperties = properties.bind(environment); ConfigurableProperties boundProperties = properties.bind(environment);
eventMulticaster.multicastEvent(new PropertiesPreparedEvent(boundProperties, propertySource, environment, application)); eventMulticaster.multicastEvent(new PropertiesPreparedEvent(environment, deferredLogFactory, propertySource, boundProperties, application));
}); });
} }
......
...@@ -4,6 +4,7 @@ import com.schbrain.common.util.properties.SchbrainMapPropertySource; ...@@ -4,6 +4,7 @@ import com.schbrain.common.util.properties.SchbrainMapPropertySource;
import com.schbrain.common.util.support.ConfigurableProperties; import com.schbrain.common.util.support.ConfigurableProperties;
import lombok.Getter; import lombok.Getter;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
...@@ -18,15 +19,21 @@ public class PropertiesPreparedEvent extends ApplicationEvent { ...@@ -18,15 +19,21 @@ public class PropertiesPreparedEvent extends ApplicationEvent {
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
private final DeferredLogFactory deferredLogFactory;
private final SchbrainMapPropertySource propertySource; private final SchbrainMapPropertySource propertySource;
private final SpringApplication application; private final SpringApplication application;
public PropertiesPreparedEvent(ConfigurableProperties properties, SchbrainMapPropertySource propertySource, public PropertiesPreparedEvent(ConfigurableEnvironment environment,
ConfigurableEnvironment environment, SpringApplication application) { DeferredLogFactory deferredLogFactory,
SchbrainMapPropertySource propertySource,
ConfigurableProperties properties,
SpringApplication application) {
super(properties); super(properties);
this.environment = environment; this.environment = environment;
this.propertySource = propertySource; this.propertySource = propertySource;
this.deferredLogFactory = deferredLogFactory;
this.application = application; this.application = application;
} }
......
package com.schbrain.framework.autoconfigure.apollo.listener; package com.schbrain.framework.autoconfigure.apollo.listener;
import com.schbrain.common.util.support.ConfigurableProperties; import com.schbrain.common.util.support.ConfigurableProperties;
import org.apache.commons.logging.Log;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
...@@ -14,6 +15,8 @@ public class PropertiesPreparedEventListenerAdapter<T extends ConfigurableProper ...@@ -14,6 +15,8 @@ public class PropertiesPreparedEventListenerAdapter<T extends ConfigurableProper
private final Class<T> propertyType; private final Class<T> propertyType;
protected Log log;
public PropertiesPreparedEventListenerAdapter() { public PropertiesPreparedEventListenerAdapter() {
ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass(); ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
...@@ -23,7 +26,8 @@ public class PropertiesPreparedEventListenerAdapter<T extends ConfigurableProper ...@@ -23,7 +26,8 @@ public class PropertiesPreparedEventListenerAdapter<T extends ConfigurableProper
@Override @Override
public void onApplicationEvent(PropertiesPreparedEvent event) { public void onApplicationEvent(PropertiesPreparedEvent event) {
if (event.getConfigurableProperties().getClass() == propertyType) { if (event.getConfigurableProperties().getClass() == propertyType) {
onPropertiesPrepared(event, (T) event.getConfigurableProperties()); this.log = event.getDeferredLogFactory().getLog(this.getClass());
this.onPropertiesPrepared(event, (T) event.getConfigurableProperties());
} }
} }
......
...@@ -11,7 +11,6 @@ import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEv ...@@ -11,7 +11,6 @@ import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEv
import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListenerAdapter; import com.schbrain.framework.autoconfigure.apollo.listener.PropertiesPreparedEventListenerAdapter;
import com.schbrain.framework.autoconfigure.logger.LoggerConfigurationInitializer; import com.schbrain.framework.autoconfigure.logger.LoggerConfigurationInitializer;
import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties; import com.schbrain.framework.autoconfigure.logger.properties.LoggerProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -25,7 +24,6 @@ import static org.springframework.boot.context.logging.LoggingApplicationListene ...@@ -25,7 +24,6 @@ import static org.springframework.boot.context.logging.LoggingApplicationListene
* @author liaozan * @author liaozan
* @since 2023-04-28 * @since 2023-04-28
*/ */
@Slf4j
public class LoggerPropertiesPreparedEventListener extends PropertiesPreparedEventListenerAdapter<LoggerProperties> { public class LoggerPropertiesPreparedEventListener extends PropertiesPreparedEventListenerAdapter<LoggerProperties> {
@Override @Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment