go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.66k stars 3.92k forks source link

为什么使用First未查询到数据的时候,框架要打印ERROR日志? #7065

Closed yangtuooc closed 3 months ago

yangtuooc commented 3 months ago

当我使用Gorm作为存储层查询框架时,我通常会使用First来查询我指定的数据,但是我有一个疑问:为什么First在未查询到指定数据的时候会通过Logger输出一个级别为ERROR的日志?

这总是会为我带来困扰,因为它会触发我的日志监控告警。


首先,First未查询到数据的时候,需要返回一个error,这是一种已知可能发生的错误,并且已经在业务中进行了处理。 而面对我在上面描述的这种情况,我不得不采用其他的查询api来避免ERROR日志告警,例如:Find + Limit 或 FirstOrInit,但是这样会增加额外的业务层处理判断...

github-actions[bot] commented 3 months ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

yangtuooc commented 3 months ago

这是来自QQ邮箱的假期自动回复邮件。 您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

github-actions[bot] commented 3 months ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

ivila commented 3 months ago

当我使用Gorm作为存储层查询框架时,我通常会使用First来查询我指定的数据,但是我有一个疑问:为什么First在未查询到指定数据的时候会通过Logger输出一个级别为ERROR的日志?

这总是会为我带来困扰,因为它会触发我的日志监控告警。

首先,First未查询到数据的时候,需要返回一个error,这是一种已知可能发生的错误,并且已经在业务中进行了处理。 而面对我在上面描述的这种情况,我不得不采用其他的查询api来避免ERROR日志告警,例如:Find + Limit 或 FirstOrInit,但是这样会增加额外的业务层处理判断...

@yangtuooc 你可以选择自定义Logger,仿照这个自己写一个就行了:https://github.com/go-gorm/gorm/blob/master/logger/logger.go 实际上业务上很少有人会用默认的Logger,因为实际使用时要注入各种监控组件的内容,比如Prometheus,Open Tracing之类的。

github-actions[bot] commented 3 months ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

jinzhu commented 3 months ago

https://gorm.io/docs/query.html#Retrieving-a-single-object