aboutsummaryrefslogtreecommitdiff
path: root/cmd/monfront
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2023-08-17 22:05:46 +0200
committerGibheer <gibheer+git@zero-knowledge.org>2023-08-17 22:05:46 +0200
commit8e6e01f47c320f862b9ba44815587006ba80cf77 (patch)
treecb59d7d3a3c84a4d54ac2a26e436f2b2a83cdfab /cmd/monfront
parent10f7eb53f4370cab6c1ff390773772389d500e59 (diff)
switch from log to slog
This commit replaces the various log entry points with a common logger provided via context. This is helpful as it groups all log entries together via the txid and should help in the future when debugging cascading errors.
Diffstat (limited to 'cmd/monfront')
-rw-r--r--cmd/monfront/authenticater.go3
-rw-r--r--cmd/monfront/checks.go19
-rw-r--r--cmd/monfront/create.go7
-rw-r--r--cmd/monfront/groups.go7
-rw-r--r--cmd/monfront/main.go16
5 files changed, 24 insertions, 28 deletions
diff --git a/cmd/monfront/authenticater.go b/cmd/monfront/authenticater.go
index 8453264..e43c6b2 100644
--- a/cmd/monfront/authenticater.go
+++ b/cmd/monfront/authenticater.go
@@ -7,7 +7,6 @@ import (
"database/sql"
"encoding/base64"
"fmt"
- "log"
"net/http"
"strings"
"time"
@@ -84,7 +83,7 @@ func (a *Authenticator) Handler() (func(*Context) error, error) {
}
p := pwHash{}
if err := p.Parse(found); err != nil {
- log.Printf("could not parse hash for user '%s': %s", user, err)
+ c.log.Warn("could not parse hash for user", "user", user, "error", err)
return a.Unauthorized(c)
}
if ok, err := p.compare(pass); err != nil {
diff --git a/cmd/monfront/checks.go b/cmd/monfront/checks.go
index d61d337..e6668fd 100644
--- a/cmd/monfront/checks.go
+++ b/cmd/monfront/checks.go
@@ -2,7 +2,6 @@ package main
import (
"database/sql"
- "log"
"net/http"
"time"
@@ -101,7 +100,7 @@ func showCheck(con *Context) {
} else if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problems with the database"))
- log.Printf("could not get check details for check id %s: %s", id[0], err)
+ con.log.Info("could not get check details for check id", "id", id[0], "error", err)
return
}
@@ -114,7 +113,7 @@ func showCheck(con *Context) {
rows, err := DB.Query(query, cd.Id)
defer rows.Close()
if err != nil {
- log.Printf("could not load notifications: %s", err)
+ con.log.Info("could not load notifications", "error", err)
con.Error = "could not load notification information"
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -122,7 +121,7 @@ func showCheck(con *Context) {
cd.Notifications = []notification{}
for rows.Next() {
if err := rows.Err(); err != nil {
- log.Printf("could not load notifications: %s", err)
+ con.log.Info("could not load notifications", "error", err)
con.Error = "could not load notification information"
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -130,7 +129,7 @@ func showCheck(con *Context) {
no := notification{}
if err := rows.Scan(&no.Id, &no.State, &no.Output, &no.Inserted,
&no.Sent, &no.NotifierName, &no.MappingId); err != nil {
- log.Printf("could not scan notifications: %s", err)
+ con.log.Info("could not scan notifications", "error", err)
con.Error = "could not load notification information"
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -141,7 +140,7 @@ func showCheck(con *Context) {
if err := con.loadMappings(); err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problem with the mappings"))
- log.Printf("could not load mappings: %s", err)
+ con.log.Info("could not load mappings", "error", err)
return
}
@@ -188,7 +187,7 @@ func showChecks(con *Context) {
if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problems with the database"))
- log.Printf("could not get check list: %s", err)
+ con.log.Info("could not get check list", "error", err)
return
}
defer rows.Close()
@@ -201,7 +200,7 @@ func showChecks(con *Context) {
if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
returnError(http.StatusInternalServerError, con, con.w)
- log.Printf("could not get check list: %s", err)
+ con.log.Info("could not get check list", "error", err)
return
}
checks = append(checks, c)
@@ -210,14 +209,14 @@ func showChecks(con *Context) {
if err := con.loadCommands(); err != nil {
con.Error = "could not load commands"
returnError(http.StatusInternalServerError, con, con.w)
- log.Printf("could not get commands: %s", err)
+ con.log.Info("could not get commands", "error", err)
return
}
if err := con.loadMappings(); err != nil {
con.Error = "could not load mapping data"
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problem with the mappings"))
- log.Printf("could not load mappings: %s", err)
+ con.log.Info("could not load mappings", "error", err)
return
}
con.w.Header()["Content-Type"] = []string{"text/html"}
diff --git a/cmd/monfront/create.go b/cmd/monfront/create.go
index 43805a1..e11679f 100644
--- a/cmd/monfront/create.go
+++ b/cmd/monfront/create.go
@@ -3,7 +3,6 @@ package main
import (
"database/sql"
"fmt"
- "log"
"net/http"
"strings"
)
@@ -38,14 +37,14 @@ func showCreate(con *Context) {
rows, err := DB.Query(prim.query)
defer rows.Close()
if err != nil {
- log.Printf("could not get commands: %s", err)
+ con.log.Info("could not get commands", "error", err)
con.Error = "could not get commands"
returnError(http.StatusInternalServerError, con, con.w)
return
}
result, err := rowsToResult(rows)
if err != nil {
- log.Printf("could not get %s: %s", prim.name, err)
+ con.log.Info("could not get", "primitive", prim.name, "error", err)
con.Error = "could not get " + prim.name
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -137,7 +136,7 @@ func addCreate(con *Context) {
stmt := `insert into ` + t.Table + `(` + strings.Join(t.Fields, ",") + `) values (` + strings.Join(vals, ",") + `)`
_, err := DB.Exec(stmt, fields...)
if err != nil {
- log.Printf("could not insert new %s: %s", addType, err)
+ con.log.Info("could not insert new type", "type", addType, "error", err)
con.Error = "could not insert new " + addType
returnError(http.StatusInternalServerError, con, con.w)
return
diff --git a/cmd/monfront/groups.go b/cmd/monfront/groups.go
index 0dfa40c..ee13226 100644
--- a/cmd/monfront/groups.go
+++ b/cmd/monfront/groups.go
@@ -2,7 +2,6 @@ package main
import (
"fmt"
- "log"
"net/http"
"strings"
)
@@ -56,7 +55,7 @@ where maxstate = 1`
if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problems with the database"))
- log.Printf("could not get check list: %s", err)
+ con.log.Info("could not get check list", "error", err)
return
}
@@ -67,7 +66,7 @@ where maxstate = 1`
if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problems with the database"))
- log.Printf("could not get check list: %s", err)
+ con.log.Info("could not get check list", "error", err)
return
}
groups = append(groups, g)
@@ -76,7 +75,7 @@ where maxstate = 1`
if err := con.loadMappings(); err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
con.w.Write([]byte("problem with the mappings"))
- log.Printf("could not load mappings: %s", err)
+ con.log.Info("could not load mappings", "error", err)
return
}
con.w.Header()["Content-Type"] = []string{"text/html"}
diff --git a/cmd/monfront/main.go b/cmd/monfront/main.go
index 19be6e0..2fcf15b 100644
--- a/cmd/monfront/main.go
+++ b/cmd/monfront/main.go
@@ -283,7 +283,7 @@ func checkAction(con *Context) {
setClause = "enabled = false, updated = now()"
case "delete_check":
if _, err := DB.Exec(`delete from checks where id = any ($1::bigint[])`, pq.Array(checks)); err != nil {
- log.Printf("could not delete checks '%s': %s", checks, err)
+ con.log.Info("could not delete checks", "checks", checks, "error", err)
con.Error = "could not delete checks"
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -315,7 +315,7 @@ func checkAction(con *Context) {
hostname, err := os.Hostname()
if err != nil {
- log.Printf("could not resolve hostname: %s", err)
+ con.log.Info("could not resolve hostname", "error", err)
con.Error = "could not resolve hostname"
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -326,7 +326,7 @@ func checkAction(con *Context) {
from checks_notify cn
join active_checks ac on cn.check_id = ac.check_id
where cn.check_id = any ($1::bigint[])`, pq.Array(&checks), &hostname); err != nil {
- log.Printf("could not acknowledge check: %s", err)
+ con.log.Info("could not acknowledge check", "error", err)
con.Error = "could not acknowledge check"
returnError(http.StatusInternalServerError, con, con.w)
return
@@ -343,7 +343,7 @@ func checkAction(con *Context) {
if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(con.w, "could not store changes")
- log.Printf("could not adjust checks %#v: %s", checks, err)
+ con.log.Info("could not adjust checks", "error", err, "checks", checks)
return
}
con.w.WriteHeader(http.StatusSeeOther)
@@ -354,7 +354,7 @@ func checkAction(con *Context) {
if err != nil {
con.Error = "could not uncomment checks"
returnError(http.StatusInternalServerError, con, con.w)
- log.Printf("could not uncomment checks: %s", err)
+ con.log.Info("could not uncomment checks", "error", err)
return
}
con.w.WriteHeader(http.StatusSeeOther)
@@ -381,20 +381,20 @@ func checkAction(con *Context) {
if err != nil {
con.w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(con.w, "could not store changes")
- log.Printf("could not adjust checks %#v: %s", checks, err)
+ con.log.Info("could not adjust checks", "checks", checks, "error", err)
return
}
con.w.WriteHeader(http.StatusSeeOther)
return
}
-func returnError(status int, con interface{}, w http.ResponseWriter) {
+func returnError(status int, con *Context, w http.ResponseWriter) {
w.Header()["Content-Type"] = []string{"text/html"}
w.WriteHeader(status)
if err := Tmpl.ExecuteTemplate(w, "error", con); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("problem with a template"))
- log.Printf("could not execute template: %s", err)
+ con.log.Info("could not execute template", "error", err)
}
}