Skip to content

Commit 6ac9b4d

Browse files
gosimviewer: filter by log level
1 parent fcbdf5b commit 6ac9b4d

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

internal/gosimlog/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Log struct {
1919
Index int `json:"-"`
2020

2121
Time time.Time `json:"time"`
22-
Level string `json:"level"`
22+
Level slog.Level `json:"level"`
2323
Msg string `json:"msg"`
2424
Source *Stackframe `json:"source"`
2525
Step int `json:"step"`

internal/gosimviewer/index.html.tmpl

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ a {
1818
<div style="display: flex; overflow: hide; height: 100%">
1919
<div style="width: 50%; display: flex; flex-direction: column;">
2020
<div>
21-
<pre>focus: {{ if .Focus }}{{ .Focus }} <a href="/">all logs</a>{{ else }}all logs{{ end }}</pre>
21+
{{ $level := .LogLevel }}
22+
{{ $focus := .Focus }}
23+
<pre>focus: {{ if .Focus }}{{ .Focus }} <a href="/?level={{ $level }}">all logs</a>{{ else }}all logs{{ end }}</pre>
24+
<pre>log level:{{ range .LogLevels }} {{ if eq . $level }}{{ . }}{{ else }}<a href="/?level={{ . }}&focus={{ $focus }}">{{ . }}</a>{{ end }}{{ end }}</pre>
2225
</div>
2326
<div style="overflow: scroll; font-family: monospace;">
2427
{{ range .Logs }}
2528
<span>
2629
{{ .Step }}
27-
<a href="/?focus=machine:{{ .Machine }}">{{ .Machine }}</a>/<a href="/?focus=goroutine:{{ .Goroutine }}">{{.Goroutine}}</a>
30+
<a href="/?level={{ $level }}&focus=machine:{{ .Machine }}">{{ .Machine }}</a>/<a href="/?level={{ $level }}&focus=goroutine:{{ .Goroutine }}">{{.Goroutine}}</a>
2831
{{.Time | Time }}
2932
{{ .Level }}
3033
<a href="#" onclick="show({index: {{ .Index }}, source: {{ .Source }} })">{{ .Source | Source }}</a>

internal/gosimviewer/main.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"html/template"
99
"log"
10+
"log/slog"
1011
"net/http"
1112
"os"
1213
"path/filepath"
@@ -72,9 +73,22 @@ func filter(logs []*gosimlog.Log, f func(*gosimlog.Log) bool) []*gosimlog.Log {
7273
func (s *Server) Index(w http.ResponseWriter, r *http.Request) {
7374
q := r.URL.Query()
7475
focusStr := q.Get("focus")
76+
levelStr := q.Get("level")
7577

7678
logs := s.Logs
7779

80+
level := slog.LevelInfo
81+
if levelStr != "" {
82+
if err := level.UnmarshalText([]byte(levelStr)); err != nil {
83+
http.Error(w, fmt.Sprintf("parsing level: %s", err.Error()), http.StatusBadRequest)
84+
return
85+
}
86+
}
87+
88+
logs = filter(logs, func(l *gosimlog.Log) bool {
89+
return l.Level >= level
90+
})
91+
7892
if focusStr != "" {
7993
kind, arg, _ := strings.Cut(focusStr, ":")
8094

@@ -100,8 +114,10 @@ func (s *Server) Index(w http.ResponseWriter, r *http.Request) {
100114
}
101115

102116
if err := templates.ExecuteTemplate(w, "index.html.tmpl", map[string]any{
103-
"Logs": logs,
104-
"Focus": focusStr,
117+
"Logs": logs,
118+
"Focus": focusStr,
119+
"LogLevels": []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError},
120+
"LogLevel": level,
105121
}); err != nil {
106122
http.Error(w, fmt.Sprintf("writing template: %s", err.Error()), http.StatusInternalServerError)
107123
return

0 commit comments

Comments
 (0)