lecho icon indicating copy to clipboard operation
lecho copied to clipboard

Zerolog interface not print logs

Open hellodit opened this issue 2 years ago • 5 comments

In short, according to the title, when I use a zero log interface it can't print the logs, here the code snippet

	e.Logger().Print("Echo interface")
	zerolog.Ctx(e.Request().Context()).Print("Zerolog interface🔥🔥🔥🔥🔥")

and here is the log generated

{"time":"2022-03-22T14:22:32+07:00","message":"⇨ http server started on [::]:1213"}
{"id":"mv4c2Pbvxl8KxshWvz7qs5TP8iSRlCxS","level":"-","time":"2022-03-22T14:22:46+07:00","message":"Echo interface"}
{"level":"debug","id":"mv4c2Pbvxl8KxshWvz7qs5TP8iSRlCxS","remote_ip":"::1","host":"localhost:1213","method":"POST","uri":"/dialogflow/webhook","user_agent":"PostmanRuntime/7.29.0","status":200,"referer":"","latency":632.1662,"latency_human":"632.1662ms","bytes_in":"1827","bytes_out":"448","time":"2022-03-22T14:22:46+07:00"}

why did it happen? I want to print the log using a zero log interface

hellodit avatar Mar 22 '22 07:03 hellodit

Hey,

Are the versions of zerolog the same?

ziflex avatar Mar 24 '22 03:03 ziflex

si @ziflex

here is the verison

require (
...	github.com/rs/zerolog v1.26.0
	github.com/ziflex/lecho/v3 v3.1.0
...
)

hellodit avatar Mar 24 '22 09:03 hellodit

Could you show me how you set it up?

Here is a workable snippet:

package main

import (
	"github.com/labstack/echo/v4"
	"github.com/rs/zerolog"
	"github.com/ziflex/lecho/v3"
	"os"
)

func main() {
	serv := echo.New()
	logger := lecho.New(os.Stdout)
	serv.Logger = logger

	serv.Use(lecho.Middleware(lecho.Config{
		Logger: logger,
	}))

	serv.GET("/", func(c echo.Context) error {
		zerolog.Ctx(c.Request().Context()).Print("Zerolog interface🔥🔥🔥🔥🔥")

		return c.String(200, "OK")
	})

	if err := serv.Start("0.0.0.0:8181"); err != nil {
		panic(err)
	}
}

ziflex avatar Mar 24 '22 19:03 ziflex

Oh I see i use this config to setup

package main

import (
	"github.com/labstack/echo/v4"
	"github.com/rs/zerolog"
	"github.com/ziflex/lecho/v3"
	"os"
	"github.com/labstack/gommon/log"
)

func main() {
	serv := echo.New()
	logger := lecho.New(os.Stdout,
		lecho.WithLevel(log.INFO),
		lecho.WithTimestamp(),
	)

	serv.Logger = logger

	serv.Use(lecho.Middleware(lecho.Config{
		Logger: logger,
	}))

	serv.GET("/", func(c echo.Context) error {
		zerolog.Ctx(c.Request().Context()).Print("Zerolog interface🔥🔥🔥🔥🔥")

		return c.String(200, "OK")
	})

	if err := serv.Start("0.0.0.0:8181"); err != nil {
		panic(err)
	}
}

can you explain why?

the result is

{"time":"2022-03-25T09:37:54+07:00","message":"⇨ http server started on [::]:8181"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.006412,"latency_human":"6.412µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:37:56+07:00"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.025659,"latency_human":"25.659µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:37:58+07:00"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.007784,"latency_human":"7.784µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:37:58+07:00"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.007574,"latency_human":"7.574µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:37:59+07:00"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.007064,"latency_human":"7.064µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:38:01+07:00"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.007194,"latency_human":"7.194µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:38:01+07:00"}
{"level":"info","remote_ip":"::1","host":"localhost:8181","method":"GET","uri":"/","user_agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36","status":200,"referer":"","latency":0.025539,"latency_human":"25.539µs","bytes_in":"0","bytes_out":"2","time":"2022-03-25T09:38:02+07:00"}

hellodit avatar Mar 25 '22 02:03 hellodit

What if you remove this line lecho.WithLevel(log.INFO) ?

ziflex avatar Mar 27 '22 16:03 ziflex