Repository navigation
Expand file tree
/
Copy pathAnalysisLogging.java
More file actions
73 lines (63 loc) · 3 KB
/
Copy pathAnalysisLogging.java
File metadata and controls
73 lines (63 loc) · 3 KB
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
package dev.archunitjava.diagnostics;
import dev.archunitjava.report.ConsoleColor;
import dev.archunitjava.report.ConsoleText;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Explicit per-check observer; no global logger, files, timestamps or implicit console writes.
* Sink runtime failures are isolated so logging cannot replace a rule result or execution error.
* Callers own sink lifetime, flushing and concurrency. Fatal VM errors are not intercepted.
*/
public final class AnalysisLogging {
private static final AnalysisLogging DISABLED = new AnalysisLogging(LogLevel.OFF, event -> {});
private final LogLevel level;
private final Consumer<AnalysisEvent> sink;
private AnalysisLogging(LogLevel level, Consumer<AnalysisEvent> sink) {
this.level = Objects.requireNonNull(level, "level");
this.sink = Objects.requireNonNull(sink, "sink");
}
public static AnalysisLogging disabled() { return DISABLED; }
public static AnalysisLogging of(LogLevel level, Consumer<AnalysisEvent> sink) {
return new AnalysisLogging(level, sink);
}
/** Plain text is the conservative default for appendable destinations such as files. */
public static AnalysisLogging text(LogLevel level, Appendable destination) {
return text(level, destination, ConsoleColor.NEVER);
}
public static AnalysisLogging text(LogLevel level, Appendable destination, ConsoleColor color) {
Objects.requireNonNull(destination, "destination");
boolean useColor = Objects.requireNonNull(color, "color").enabled();
return of(level, event -> {
try {
destination.append(format(event, useColor));
} catch (IOException failure) {
throw new UncheckedIOException(failure);
}
});
}
public LogLevel level() { return level; }
public boolean enabled(LogLevel eventLevel) { return level.includes(eventLevel); }
public void emit(LogLevel eventLevel, String stage, String message, Supplier<Map<String, String>> details) {
if (!enabled(eventLevel)) return;
try {
sink.accept(new AnalysisEvent(eventLevel, stage, message, details.get()));
} catch (RuntimeException ignored) {
// Observability is best effort and never alters architecture semantics.
}
}
public static String format(AnalysisEvent event, boolean color) {
String code = switch (event.level()) {
case ERROR -> "31";
case WARN -> "33";
case INFO -> "36";
case DEBUG, OFF -> "2";
};
return ConsoleText.style("[" + event.level() + "]", code, color) + " "
+ ConsoleText.sanitize(event.stage()) + " | " + ConsoleText.sanitize(event.message())
+ (event.details().isEmpty() ? "" : " " + ConsoleText.sanitize(event.details().toString())) + "\n";
}
}