Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zerolog interface not print logs #16

Open
hellodit opened this issue Mar 22, 2022 · 5 comments
Open

Zerolog interface not print logs #16

hellodit opened this issue Mar 22, 2022 · 5 comments
Labels
question Further information is requested

Comments

@hellodit
Copy link

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

@ziflex
Copy link
Owner

ziflex commented Mar 24, 2022

Hey,

Are the versions of zerolog the same?

@hellodit
Copy link
Author

si @ziflex

here is the verison

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

@ziflex
Copy link
Owner

ziflex commented Mar 24, 2022

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)
	}
}

@hellodit
Copy link
Author

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"}

@ziflex
Copy link
Owner

ziflex commented Mar 27, 2022

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

@ziflex ziflex added the question Further information is requested label Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants