LogErrorHealthCheck.java
3.37 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
74
75
76
77
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.PropertyUnbounded
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.hc.api.HealthCheck
* org.apache.sling.hc.api.Result
* org.apache.sling.hc.api.ResultLog
* org.apache.sling.hc.util.FormattingResultLog
*/
package com.adobe.granite.logging.impl;
import com.adobe.granite.logging.LogAnalyser;
import com.adobe.granite.logging.LogEntry;
import com.adobe.granite.logging.LogLevel;
import java.util.HashSet;
import java.util.List;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.hc.api.HealthCheck;
import org.apache.sling.hc.api.Result;
import org.apache.sling.hc.api.ResultLog;
import org.apache.sling.hc.util.FormattingResultLog;
@Component(metatype=1, label="Adobe Granite Log Health Check", description="This health check checks for error log messages in the log output.")
@Properties(value={@Property(name="hc.name", value={"Log Errors"}, propertyPrivate=1), @Property(name="hc.tags", unbounded=PropertyUnbounded.ARRAY, label="Tags", description="The tags for this health check to be used in a composite health check"), @Property(name="hc.mbean.name", value={"logErrorHealthCheck"}, propertyPrivate=1)})
@Service(value={HealthCheck.class})
public class LogErrorHealthCheck
implements HealthCheck {
@Reference
private LogAnalyser logAnalyser;
public Result execute() {
FormattingResultLog resultLog = new FormattingResultLog();
int numErrors = 0;
List<LogEntry> entries = this.logAnalyser.getLastLogEntries(-1);
HashSet<String> logCategories = new HashSet<String>();
for (LogEntry entry : entries) {
if (LogLevel.ERROR != entry.getLogLevel()) continue;
++numErrors;
if (logCategories.contains(entry.getLoggerName())) continue;
logCategories.add(entry.getLoggerName());
}
if (numErrors > 0) {
Object[] arrobject = new Object[4];
arrobject[0] = numErrors;
arrobject[1] = LogLevel.ERROR;
arrobject[2] = numErrors > 1 ? "s" : "";
arrobject[3] = entries.size();
resultLog.warn("[{} {} log message{} found (from a total of {} messages)]( )", arrobject);
} else {
resultLog.debug("No {} log messages found (from a total of {} messages)", new Object[]{LogLevel.ERROR, entries.size()});
}
resultLog.debug("[Check the Log Messages section in the Diagnosis page to change the log analyzer level.](/libs/granite/operations/content/diagnosis/tool.html/granite:logmessages)", new Object[0]);
return new Result((ResultLog)resultLog);
}
protected void bindLogAnalyser(LogAnalyser logAnalyser) {
this.logAnalyser = logAnalyser;
}
protected void unbindLogAnalyser(LogAnalyser logAnalyser) {
if (this.logAnalyser == logAnalyser) {
this.logAnalyser = null;
}
}
}