frontend: improve log display

This commit is contained in:
Florian Klink 2019-12-04 15:21:17 +01:00
parent bdfdad2585
commit ea04792c39
3 changed files with 39 additions and 6 deletions

View file

@ -4,12 +4,15 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"encoding/json"
"html/template" "html/template"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rakyll/statik/fs" "github.com/rakyll/statik/fs"
"github.com/apex/log"
"github.com/tweag/gerrit-queue/gerrit" "github.com/tweag/gerrit-queue/gerrit"
"github.com/tweag/gerrit-queue/misc" "github.com/tweag/gerrit-queue/misc"
_ "github.com/tweag/gerrit-queue/statik" // register static assets _ "github.com/tweag/gerrit-queue/statik" // register static assets
@ -68,6 +71,26 @@ func MakeFrontend(rotatingLogHandler *misc.RotatingLogHandler, gerritClient *ger
"changesetURL": func(changeset *gerrit.Changeset) string { "changesetURL": func(changeset *gerrit.Changeset) string {
return gerritClient.GetChangesetURL(changeset) return gerritClient.GetChangesetURL(changeset)
}, },
"levelToClasses": func(level log.Level) string {
switch level {
case log.DebugLevel:
return "text-muted"
case log.InfoLevel:
return "text-info"
case log.WarnLevel:
return "text-warning"
case log.ErrorLevel:
return "text-danger"
case log.FatalLevel:
return "text-danger"
default:
return "text-white"
}
},
"fieldsToJSON": func(fields log.Fields) string {
jsonData, _ := json.Marshal(fields)
return string(jsonData)
},
} }
tmpl := template.Must(loadTemplate([]string{ tmpl := template.Must(loadTemplate([]string{

1
go.sum
View file

@ -71,6 +71,7 @@ golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=

View file

@ -63,15 +63,24 @@
{{ end }} {{ end }}
<h2 id="region-log">Log</h2> <h2 id="region-log">Log</h2>
<table class="table"> <table class="table .table-sm">
<tbody> <tbody>
<tr>
<td class="bg-dark text-white">
{{ range $entry := .memory.Entries }} {{ range $entry := .memory.Entries }}
<code>{{ $entry }}</code><br /> <tr class="bg-dark {{ levelToClasses $entry.Level }}">
{{ end }} <td rowspan="2">
{{ $entry.Timestamp.Format "2006-01-02 15:04:05 UTC"}}
</td>
<td>{{ $entry.Level }}</td>
<td>
<code class="{{ levelToClasses $entry.Level }}">{{ $entry.Message }}</code><br />
</td> </td>
</tr> </tr>
<tr class="bg-dark {{ levelToClasses $entry.Level }}">
<td colspan="2">
<code class="{{ levelToClasses $entry.Level }}">{{ fieldsToJSON $entry.Fields }}</code><br />
</td>
</tr>
{{ end }}
</tbody> </tbody>
</table> </table>
</body> </body>