-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Make log level configurable with Logger Middleware for any logs #1989
Comments
@ymotongpoo Logger middleware and echo.Logger are 2 different things. So If you want to add hardcoded severity field to log line then add it into var CustomConfig = middleware.LoggerConfig{
Format: `{"severity":"INFO", "time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}",` +
`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` +
`"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` +
`,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n",
} |
Thanks for the reply, @aldas. I added Adding the hardcoded section in the format could be one solution, but it only works for the case where all accesses are neither 4xx nor 5xx. In many cases, 4xx and 5xx are treated as "warning" or more severe levels. For example, this is the case with go-chi/chi {"level":"warn","service":"app","httpRequest":{"proto":"HTTP/1.1","remoteIP":"127.0.0.1:49672","requestID":"yoshi/oX9X7p0J5r-000003","requestMethod":"GET","requestPath":"/favicon.ico","requestURL":"http://127.0.0.1:8888/favicon.ico"},"httpResponse":{"body":"404 page not found\n","bytes":19,"elapsed":0.04479,"header":{"content-type":"text/plain; charset=utf-8","x-content-type-options":"nosniff"},"status":404},"timestamp":"2021-09-17T20:55:16.372330036+09:00","message":"Response: 404 Client Error"} Given that, do you have any plans to support access log levels in the future? |
we could add some callback that takes in that line buff, context and error and developer can add whatever he/she chooses by that. Currently we have in unreleased state this new middleware https://github.com/labstack/echo/blob/master/middleware/request_logger.go it will be soon included in |
@ymotongpoo |
@aldas Thank you for the feature! I'll try it out and leave feedback here! |
@aldas Where can we get the timestamp from the https://github.com/labstack/echo/blob/master/middleware/request_logger.go#L126-L169 |
assumption was that timestamps are added by logging library but I get what you mean - are timestamps for beginning of the request or end of request. I'll add field to get the beginning of the request. current workaround is to subtract |
Time when Request Logger middleware started can be read here echo/middleware/request_logger.go Line 128 in c32fafa
Logger middleware is best set as first middleware and recover after that. But anyway - |
This is where latency is calculated. echo/middleware/request_logger.go Lines 213 to 225 in c32fafa
|
I would not say it is inaccurate. it is good enough without delving into philosophical discussions what is accurate request time and what is not and when does request actually starts and when it ends. |
Issue Description
As of today, Logger Middleware does not support log level in the custom logger format. This makes all access logs being without log levels.
#1649 is relevant to this issue.
Quoting all available tags for Logger Middleware from the doc:
Checklist
Expected behaviour
level
tag is available in theLoggerConfig#Format
.Actual behaviour
level
tag is not available. I tried it just in case and I confirmed it doesn't work.Steps to reproduce
I added
"severity": "${level}"
to theDefaultLoggerConfig.Format
and used that format inCustomConfig
, then I called LoggerMiddleware like this:Then launched the app and accessed to the server. The output of the access log is as the following:
As you see, because
${level}
is not available, it just generates blank strings.Working code to debug
Version/commit
v4.5.0
The text was updated successfully, but these errors were encountered: