From 2c39828fe277906c5a4abb5b474fe989b1be6c01 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 9 Jun 2020 06:30:12 +0300 Subject: [PATCH] update kataras/golog and kataras/pio deps Former-commit-id: 0f736f181b01771701b43cf4eef46b7086838fc7 --- _examples/logging/json-logger/main_test.go | 55 +++++++++++++++++++ .../request-logger-file-json/main.go | 2 +- .../request-logger-file/main.go | 2 +- _examples/logging/rollbar/main.go | 2 +- go.mod | 4 +- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 _examples/logging/json-logger/main_test.go diff --git a/_examples/logging/json-logger/main_test.go b/_examples/logging/json-logger/main_test.go new file mode 100644 index 00000000..a63262ee --- /dev/null +++ b/_examples/logging/json-logger/main_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "encoding/json" + "strings" + "sync" + "testing" + + "github.com/kataras/golog" + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/httptest" +) + +func TestJSONLogger(t *testing.T) { + iters := 500 + + out := new(bytes.Buffer) + + app := iris.New() + app.Logger().SetTimeFormat("") // disable timestamps. + app.Logger().SetStacktraceLimit(1) // limit debug stacktrace to 1, show only the first caller. + app.Logger().SetOutput(out) + + app.Logger().Handle(func(l *golog.Log) bool { + enc := json.NewEncoder(l.Logger.Printer) // you can change the output to a file as well. + err := enc.Encode(l) + return err == nil + }) + + app.Get("/ping", ping) + + const expectedLogStr = `{"level":"debug","message":"Request path: /ping","fields":{"request_id":null},"stacktrace":[{"function":"json-logger/ping","source":"C:/mygopath/src/github.com/kataras/iris/_examples/logging/json-logger/main.go:82"}]}` + e := httptest.New(t, app, httptest.LogLevel("debug")) + wg := new(sync.WaitGroup) + wg.Add(iters) + for i := 0; i < iters; i++ { + go func() { + e.GET("/ping").Expect().Status(httptest.StatusOK).Body().Equal("pong") + wg.Done() + }() + } + + wg.Wait() + expected := "" + for i := 0; i < iters; i++ { + expected += expectedLogStr + "\n" + } + + got := out.String() + got = got[strings.Index(got, "{"):] // take only the json we care and after. + if expected != got { + t.Fatalf("expected:\n%s\nbut got:\n%s", expected, got) + } +} diff --git a/_examples/logging/request-logger/request-logger-file-json/main.go b/_examples/logging/request-logger/request-logger-file-json/main.go index da18c256..67480502 100644 --- a/_examples/logging/request-logger/request-logger-file-json/main.go +++ b/_examples/logging/request-logger/request-logger-file-json/main.go @@ -61,8 +61,8 @@ func main() { }() r := newRequestLogger(logFile) - app.Use(r) + app.OnAnyErrorCode(r, func(ctx iris.Context) { ctx.HTML("

Error: Please try this instead.

") }) diff --git a/_examples/logging/request-logger/request-logger-file/main.go b/_examples/logging/request-logger/request-logger-file/main.go index 11293f0f..ac387963 100644 --- a/_examples/logging/request-logger/request-logger-file/main.go +++ b/_examples/logging/request-logger/request-logger-file/main.go @@ -9,7 +9,7 @@ import ( "github.com/kataras/iris/v12/middleware/logger" ) -const deleteFileOnExit = true +const deleteFileOnExit = false func main() { app := iris.New() diff --git a/_examples/logging/rollbar/main.go b/_examples/logging/rollbar/main.go index d2b37c11..0903f886 100644 --- a/_examples/logging/rollbar/main.go +++ b/_examples/logging/rollbar/main.go @@ -80,7 +80,7 @@ func main() { // * a critical error message here // // custom.handler.file - // * C:/mygopath/src/github.com/kataras/iris/_examples/loggin/rollbar/main.go:76 + // * C:/mygopath/src/github.com/kataras/iris/_examples/logging/rollbar/main.go:76 // // custom.handler.name // * main.panicMe diff --git a/go.mod b/go.mod index 436b7c19..0bb90e95 100644 --- a/go.mod +++ b/go.mod @@ -20,9 +20,9 @@ require ( github.com/iris-contrib/pongo2 v0.0.1 github.com/iris-contrib/schema v0.0.1 github.com/json-iterator/go v1.1.9 - github.com/kataras/golog v0.0.16 + github.com/kataras/golog v0.0.17 github.com/kataras/neffos v0.0.16 - github.com/kataras/pio v0.0.6 + github.com/kataras/pio v0.0.7 github.com/kataras/sitemap v0.0.5 github.com/klauspost/compress v1.10.6 github.com/mediocregopher/radix/v3 v3.5.1