-
Notifications
You must be signed in to change notification settings - Fork 40.2k
/
ConditionEvaluationReportLoggingListener.java
146 lines (125 loc) · 5.1 KB
/
ConditionEvaluationReportLoggingListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2012-2022 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.autoconfigure.logging;
import java.util.function.Supplier;
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.logging.LogLevel;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.util.Assert;
/**
* {@link ApplicationContextInitializer} that writes the {@link ConditionEvaluationReport}
* to the log. Reports are logged at the {@link LogLevel#DEBUG DEBUG} level. A crash
* report triggers an info output suggesting the user runs again with debug enabled to
* display the report.
* <p>
* This initializer is not intended to be shared across multiple application context
* instances.
*
* @author Greg Turnquist
* @author Dave Syer
* @author Phillip Webb
* @author Andy Wilkinson
* @author Madhura Bhave
* @since 2.0.0
*/
public class ConditionEvaluationReportLoggingListener
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private final LogLevel logLevelForReport;
public ConditionEvaluationReportLoggingListener() {
this(LogLevel.DEBUG);
}
private ConditionEvaluationReportLoggingListener(LogLevel logLevelForReport) {
Assert.isTrue(isInfoOrDebug(logLevelForReport), "LogLevel must be INFO or DEBUG");
this.logLevelForReport = logLevelForReport;
}
private boolean isInfoOrDebug(LogLevel logLevelForReport) {
return LogLevel.INFO.equals(logLevelForReport) || LogLevel.DEBUG.equals(logLevelForReport);
}
/**
* Static factory method that creates a
* {@link ConditionEvaluationReportLoggingListener} which logs the report at the
* specified log level.
* @param logLevelForReport the log level to log the report at
* @return a {@link ConditionEvaluationReportLoggingListener} instance.
* @since 3.0.0
*/
public static ConditionEvaluationReportLoggingListener forLogLevel(LogLevel logLevelForReport) {
return new ConditionEvaluationReportLoggingListener(logLevelForReport);
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(new ConditionEvaluationReportListener(applicationContext));
}
private final class ConditionEvaluationReportListener implements GenericApplicationListener {
private final ConfigurableApplicationContext context;
private final ConditionEvaluationReportLogger logger;
private ConditionEvaluationReportListener(ConfigurableApplicationContext context) {
this.context = context;
Supplier<ConditionEvaluationReport> reportSupplier;
if (context instanceof GenericApplicationContext) {
// Get the report early when the context allows early access to the bean
// factory in case the context subsequently fails to load
ConditionEvaluationReport report = getReport();
reportSupplier = () -> report;
}
else {
reportSupplier = this::getReport;
}
this.logger = new ConditionEvaluationReportLogger(
ConditionEvaluationReportLoggingListener.this.logLevelForReport, reportSupplier);
}
private ConditionEvaluationReport getReport() {
return ConditionEvaluationReport.get(this.context.getBeanFactory());
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
@Override
public boolean supportsEventType(ResolvableType resolvableType) {
Class<?> type = resolvableType.getRawClass();
if (type == null) {
return false;
}
return ContextRefreshedEvent.class.isAssignableFrom(type)
|| ApplicationFailedEvent.class.isAssignableFrom(type);
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return true;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent contextRefreshedEvent) {
if (contextRefreshedEvent.getApplicationContext() == this.context) {
this.logger.logReport(false);
}
}
else if (event instanceof ApplicationFailedEvent applicationFailedEvent
&& applicationFailedEvent.getApplicationContext() == this.context) {
this.logger.logReport(false);
}
}
}
}