generated from nathanwoodburn/go-webserver-template
feat: Add better logging
All checks were successful
Build Docker / BuildSite (push) Successful in 1m24s
All checks were successful
Build Docker / BuildSite (push) Successful in 1m24s
This commit is contained in:
parent
903f602119
commit
d8b1ac3fb0
7
main.go
7
main.go
@ -72,6 +72,7 @@ func getHandler(relPath string, w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Try to get monitor.log file
|
||||
logData, err := os.ReadFile(logFilePath)
|
||||
var latestLine []byte
|
||||
if err != nil {
|
||||
logData = []byte("No log file found")
|
||||
} else {
|
||||
@ -79,18 +80,18 @@ func getHandler(relPath string, w http.ResponseWriter, r *http.Request) {
|
||||
lines := bytes.Split(logData, []byte("\n"))
|
||||
var newLines [][]byte
|
||||
for i := len(lines) - 1; i >= 0; i-- {
|
||||
if !bytes.Contains(lines[i], []byte("Test")) {
|
||||
if !bytes.Contains(lines[i], []byte("Latest Check")) {
|
||||
newLines = append(newLines, lines[i])
|
||||
}
|
||||
}
|
||||
// Add latest test line (excluding the last line which is empty)
|
||||
newLines = append(newLines, lines[len(lines)-2])
|
||||
latestLine = lines[len(lines)-2]
|
||||
|
||||
logData = bytes.Join(newLines, []byte("\n"))
|
||||
}
|
||||
|
||||
// Create a map to hold the data
|
||||
requestData := map[string]interface{}{"envMessage": message, "logContent": string(logData)}
|
||||
requestData := map[string]interface{}{"envMessage": message, "logContent": string(logData), "latestLine": string(latestLine)}
|
||||
|
||||
// Execute the template
|
||||
err = tmpl.Execute(w, requestData)
|
||||
|
@ -1 +1,10 @@
|
||||
|
||||
Sat 22 Jun 2024 19:18:41 AEST - Latest Check
|
||||
Sat 22 Jun 2024 19:18:53 AEST - Latest Check
|
||||
Sat 22 Jun 2024 19:19:06 AEST - Latest Check
|
||||
Sat 22 Jun 2024 19:19:17 AEST - Latest Check
|
||||
Sat 22 Jun 2024 19:19:28 AEST - Latest Check
|
||||
Sat 22 Jun 2024 19:19:40 AEST - Latest Check
|
||||
Sat 22 Jun 2024 19:19:56 AEST - Latest Check
|
||||
Latest Check: Sat 22 Jun 2024 19:20:09 AEST
|
||||
Latest Check: Sat 22 Jun 2024 19:20:20 AEST
|
||||
|
@ -9,6 +9,10 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>HNSDoH Node Status</h1>
|
||||
{{ if .latestLine}}
|
||||
<h3>{{ .latestLine}}</h3>
|
||||
{{end}}
|
||||
|
||||
{{ if .logContent}}
|
||||
<h2>Log Output</h2>
|
||||
<pre>{{.logContent}}</pre>
|
||||
|
Loading…
Reference in New Issue
Block a user