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

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

Closed
yangtuooc opened this issue Jun 13, 2024 · 6 comments
Assignees
Labels

Comments

@yangtuooc
Copy link

yangtuooc commented Jun 13, 2024

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

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


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

@yangtuooc yangtuooc added the type:question general questions label Jun 13, 2024
@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps and removed type:question general questions labels Jun 13, 2024
Copy link

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
Copy link
Author

yangtuooc commented Jun 13, 2024 via email

Copy link

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
Copy link

ivila commented Jun 13, 2024

当我使用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之类的。

Copy link

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
Copy link
Member

jinzhu commented Jun 19, 2024

@jinzhu jinzhu closed this as completed Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants