feat: support for disable mon logs like sqlx (#3606)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
shenbaise9527
2023-10-22 23:00:02 +08:00
committed by GitHub
parent 6286941ebf
commit a242fec5e1
6 changed files with 222 additions and 47 deletions

View File

@@ -2,6 +2,8 @@ package mon
import (
"context"
"encoding/json"
"errors"
"strings"
"time"
@@ -11,17 +13,53 @@ import (
const mongoAddrSep = ","
var errPlaceholder = errors.New("placeholder")
// FormatAddr formats mongo hosts to a string.
func FormatAddr(hosts []string) string {
return strings.Join(hosts, mongoAddrSep)
}
func logDuration(ctx context.Context, name, method string, startTime time.Duration, err error) {
logDurationWithDoc(ctx, name, method, startTime, err)
}
func logDurationWithDoc(ctx context.Context, name, method string,
startTime time.Duration, err error, docs ...any) {
duration := timex.Since(startTime)
logger := logx.WithContext(ctx).WithDuration(duration)
if err != nil {
logger.Infof("mongo(%s) - %s - fail(%s)", name, method, err.Error())
var content []byte
jerr := errPlaceholder
if len(docs) > 0 {
content, jerr = json.Marshal(docs)
}
if err == nil {
// jerr should not be non-nil, but we don't care much on this,
// if non-nil, we just log without docs.
if jerr != nil {
if logSlowMon.True() && duration > slowThreshold.Load() {
logger.Slowf("mongo(%s) - slowcall - %s - ok", name, method)
} else if logMon.True() {
logger.Infof("mongo(%s) - %s - ok", name, method)
}
} else {
if logSlowMon.True() && duration > slowThreshold.Load() {
logger.Slowf("mongo(%s) - slowcall - %s - ok - %s",
name, method, string(content))
} else if logMon.True() {
logger.Infof("mongo(%s) - %s - ok - %s",
name, method, string(content))
}
}
return
}
if jerr != nil {
logger.Errorf("mongo(%s) - %s - fail(%s)", name, method, err.Error())
} else {
logger.Infof("mongo(%s) - %s - ok", name, method)
logger.Errorf("mongo(%s) - %s - fail(%s) - %s",
name, method, err.Error(), string(content))
}
}