Skip to content

Commit

Permalink
Disable HTML escaping in JSON (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Aug 25, 2020
1 parent f6ced06 commit 6d68be7
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v0.2.0
uses: golangci/golangci-lint-action@v2.2.0
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.26
version: v1.30

# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x]
go-version: [1.11.x, 1.12.x, 1.13.x, 1.14.x, 1.15.x]
runs-on: ubuntu-latest
steps:
- name: Install Go
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
GOLANGCI_LINT_VERSION := "v1.27.0"
GOLANGCI_LINT_VERSION := "v1.30.0"

lint:
@test -s $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION) || (curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /tmp $(GOLANGCI_LINT_VERSION) && mv /tmp/golangci-lint $(GOPATH)/bin/golangci-lint-$(GOLANGCI_LINT_VERSION))
Expand Down
2 changes: 1 addition & 1 deletion _examples/zzap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
}

l.Debug("starting example")
l.Sugar().Infow("sample info", "one", 1, "two", 2)
l.Sugar().Infow("sample info", "one", 1, "two", 2, "special", "<?&>")
l.Error("unexpected end of the world")

lorem.Sentence(3, 6)
Expand Down
14 changes: 9 additions & 5 deletions logzpage/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package logzpage

import (
"bytes"
"encoding/json"
"fmt"
"html/template"
Expand All @@ -23,7 +24,7 @@ type tplData struct {

// Handler creates HTTP handler to expose entries from observers.
func Handler(observers ...*logz.Observer) http.Handler {
//language=GoTemplate
// language=GoTemplate
tpl := `{{- /*gotype: github.com/bool64/logz/logzpage.tplData*/ -}}
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -77,7 +78,7 @@ func Handler(observers ...*logz.Observer) http.Handler {
<tbody>
{{ range .Entries }}
<tr>
<td><a href="?msg={{ .Message }}&level={{ $.Level }}">{{ .Message }}</a></td>
<td><a href="?msg={{ .Message }}&amp;level={{ $.Level }}">{{ .Message }}</a></td>
<td>{{ time .First }}</td>
<td>{{ time .Last }}</td>
<td>{{ .Count }}</td>
Expand Down Expand Up @@ -148,12 +149,16 @@ func Handler(observers ...*logz.Observer) http.Handler {
return template.JS(bb)
}

j, err := json.MarshalIndent(v, "", " ")
b := bytes.Buffer{}
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
err := enc.Encode(v)
if err != nil {
return template.JS(err.Error())
}

return template.JS(j)
return template.JS(b.Bytes())
},
"histogram": func(buckets []logz.Bucket) template.HTML {
return template.HTML(Histogram(buckets))
Expand Down Expand Up @@ -209,7 +214,6 @@ func Handler(observers ...*logz.Observer) http.Handler {
}

err := t.Execute(w, data)

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down
1 change: 1 addition & 0 deletions observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func (l *Observer) Find(msg string) Entry {
l.entries.Range(func(key, value interface{}) bool {
if value.(*entry).msg == msg {
e = l.exportEntry(value.(*entry), true)

return false
}

Expand Down

0 comments on commit 6d68be7

Please sign in to comment.