Open RyoJerryYu opened 1 month ago
I do have read this issue: https://github.com/gogf/gf/issues/553
But the answer in that issue do not fit my situation.
The point is the log message is a field in JSON, both logging in a simple way and logging with fields. We don't want to make the simple logging being so complex.
@RyoJerryYu Hello, we does not support WithFields
like logrus
, but you can try function SetPrefix
or submit a PR adding Fields/SetFields
function to support this.
Hello @RyoJerryYu. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it! 你好 @RyoJerryYu。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。
@RyoJerryYu Hello, we does not support
WithFields
likelogrus
, but you can try functionSetPrefix
or submit a PR addingFields/SetFields
function to support this.
@gqcn Thank you for answering. But I don't seem to have made my real needs clear.
What we really want is the ability of structured logging, and the simple logging should remain simple and work well with structured logging. We do not care how the codes look like. The logrus-like WithFields
method is not important at all.
In fact, we all know there's a bunch of libraries that support structured logging in different styles.
// zap styles
sugar.Infow("failed to fetch URL",
// Structured context as loosely typed key-value pairs.
"url", url,
"attempt", 3,
"backoff", time.Second,
)
logger.Info("failed to fetch URL",
// Structured context as strongly typed Field values.
zap.String("url", url),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)
// zerolog styles
log.Debug().
Str("Scale", "833 cents").
Float64("Interval", 833.09).
Msg("Fibonacci is everywhere")
Obviously we can easily convert to each style by adding a simple adapter layer.
But it seems glog
does not provide any method to log with fields? We tried to log with g.Map
, but the logs it produced were not what we want. I found a Chinese comment here also complaining about this: https://goframe.org/pages/viewpage.action?pageId=1114186
BTW, we finally found that it seems could be implemented by providing a self-defined Handler
, then log fields in zap style. However, since HandlerInput
do not provide any support for custom fields, we can only parse the fields from HandlerInput.Values
one by one.
It would be much better if glog
support structured logging as a first-class feature.
BTW, we finally found that it seems could be implemented by providing a self-defined
Handler
, then log fields in zap style. However, sinceHandlerInput
do not provide any support for custom fields, we can only parse the fields fromHandlerInput.Values
one by one.It would be much better if
glog
support structured logging as a first-class feature.
Hello, if your root requirement is just structure logging feature, it is already supported in glog
component. You can refer the document here: https://goframe.org/pages/viewpage.action?pageId=17207121
Here's a simple example:
package main
import (
"context"
"net"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/glog"
)
func main() {
ctx := context.TODO()
glog.SetDefaultHandler(glog.HandlerStructure)
g.Log().Info(ctx, "caution", "name", "admin")
glog.Error(ctx, "oops", net.ErrClosed, "status", 500)
}
When you run it, it outputs like this:
Time="2023-11-23 21:00:08.671" Level=INFO Content=caution name=admin
Time="2023-11-23 21:00:08.671" Level=ERRO oops="use of closed network connection" status=500 Stack="1. main.main\n /Users/txqiangguo/Workspace/gogf/gf/example/.test/main.go:16\n"
What do you want to ask?
We are using logrus and logging like this:
And it produce json logs like below:
So we can use the fields to filter our logs such as:
Do GoFrame support this? Or how can I implement something like this in GoFrame?