From 6d68be7df8a9db4f118cef7d4a4ae79077d08a45 Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Tue, 25 Aug 2020 11:44:26 +0200 Subject: [PATCH] Disable HTML escaping in JSON (#1) --- .github/workflows/golangci-lint.yml | 4 ++-- .github/workflows/test.yml | 2 +- Makefile | 2 +- _examples/zzap/main.go | 2 +- logzpage/handler.go | 14 +++++++++----- observer.go | 1 + 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 4ff2337..b44c93a 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -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/... diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f3efc1..fedecd1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/Makefile b/Makefile index 73e0603..fc86e7e 100644 --- a/Makefile +++ b/Makefile @@ -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)) diff --git a/_examples/zzap/main.go b/_examples/zzap/main.go index 6cfee83..a2f959c 100644 --- a/_examples/zzap/main.go +++ b/_examples/zzap/main.go @@ -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) diff --git a/logzpage/handler.go b/logzpage/handler.go index e6cac16..c0e212c 100644 --- a/logzpage/handler.go +++ b/logzpage/handler.go @@ -2,6 +2,7 @@ package logzpage import ( + "bytes" "encoding/json" "fmt" "html/template" @@ -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*/ -}} @@ -77,7 +78,7 @@ func Handler(observers ...*logz.Observer) http.Handler { {{ range .Entries }} - {{ .Message }} + {{ .Message }} {{ time .First }} {{ time .Last }} {{ .Count }} @@ -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)) @@ -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) } diff --git a/observer.go b/observer.go index 3b22412..756351b 100644 --- a/observer.go +++ b/observer.go @@ -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 }