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 package info.magnolia.config.registry;
35
36 import static java.util.stream.Collectors.toList;
37
38 import info.magnolia.config.registry.DefinitionProvider.Problem;
39
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.EnumMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.function.Function;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
48
49 import org.slf4j.Logger;
50
51
52
53
54
55
56
57 public final class DefinitionProviderProblemLogger {
58
59 public static DefinitionProviderProblemLogger withLoggingContext(Logger log, boolean isInDeveloperMode) {
60 return new DefinitionProviderProblemLogger(log, isInDeveloperMode);
61 }
62
63 private final Logger log;
64 private final boolean isInDeveloperMode;
65
66 DefinitionProviderProblemLogger(Logger log, boolean isInDeveloperMode) {
67 this.log = log;
68 this.isInDeveloperMode = isInDeveloperMode;
69 }
70
71 public <T> void logProblems(DefinitionProvider<T> definitionProvider) {
72
73 final Map<Problem.SeverityType, List<Problem>> problemsByType = definitionProvider
74 .getProblems()
75 .stream()
76 .collect(Collectors.groupingBy(Problem::getSeverityType, () -> new EnumMap<>(Problem.SeverityType.class), toList()));
77
78 final Function<Problem.SeverityType, List<Problem>> emptyProblemListFunction = problemType -> new ArrayList<>();
79 final List<Problem> severeProblems = problemsByType.computeIfAbsent(Problem.SeverityType.SEVERE, emptyProblemListFunction);
80 if (!severeProblems.isEmpty()) {
81 log.error("{} severe problems have been encountered:", severeProblems.size());
82 severeProblems.forEach(problem -> log.error(problem.toString(), problem.getRelatedException()));
83 }
84
85 final Collection<Problem> majorProblems = problemsByType.computeIfAbsent(Problem.SeverityType.MAJOR, emptyProblemListFunction);
86 final Collection<Problem> minorProblems = problemsByType.computeIfAbsent(Problem.SeverityType.MINOR, emptyProblemListFunction);
87
88 if (!majorProblems.isEmpty() || !minorProblems.isEmpty()) {
89 log.warn("{} major and {} minor problems have been encountered", majorProblems.size(), minorProblems.size());
90 if (!isInDeveloperMode) {
91 Stream.concat(majorProblems.stream(), minorProblems.stream()).map(Problem::toString).forEach(log::warn);
92 }
93 }
94 }
95 }