Migrating to unified logger interface

This commit is contained in:
Pavel Shevaev 2022-11-03 14:47:45 +03:00
parent 7756ae11cf
commit 399ad0a796
5 changed files with 31 additions and 19 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tags

3
go.mod
View File

@ -3,10 +3,11 @@ module git.bit5.ru/backend/rdb
go 1.13
require (
git.bit5.ru/backend/colog v1.0.0
git.bit5.ru/backend/redigo v1.0.1
git.bit5.ru/backend/redsync v1.0.1
git.bit5.ru/backend/res_tracker v1.0.0
github.com/go-logr/logr v1.2.3
github.com/go-logr/stdr v1.2.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.7.0
)

7
go.sum
View File

@ -1,5 +1,3 @@
git.bit5.ru/backend/colog v1.0.0 h1:FLykTQuRfnhWYQ+dZhoaOfkxjHNEfWp4AwP/KD+ob+M=
git.bit5.ru/backend/colog v1.0.0/go.mod h1:fiOrMQ7SPBD5Pn/Tb7rG1phsDQbZaA7S0vtdnlM6BK4=
git.bit5.ru/backend/redigo v1.0.1 h1:1LyyjHWfeed+0LEjMYqFnTuyr5ri2f+UDCV3vdAMh4Q=
git.bit5.ru/backend/redigo v1.0.1/go.mod h1:nGfr55wovFNsbAleS5hMmtD/X3X5FZ52HJdbRses5WA=
git.bit5.ru/backend/redsync v1.0.1 h1:NcXwMw36riODqi6sMyvRCFVpIfRY63oNIGJ2szdaLoI=
@ -8,6 +6,11 @@ git.bit5.ru/backend/res_tracker v1.0.0 h1:0FWwzP3yGOe9L39VuH9LIqyQABo/P+rd3kA112
git.bit5.ru/backend/res_tracker v1.0.0/go.mod h1:ffjnItxqkGc6rxOK9XgrQDirGhmIBwoqibmyLJ4TZtQ=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

19
rdb.go
View File

@ -6,11 +6,11 @@ import (
"strconv"
"time"
"git.bit5.ru/backend/colog"
"git.bit5.ru/backend/redigo/redis"
"git.bit5.ru/backend/redsync"
"git.bit5.ru/backend/res_tracker"
"github.com/go-logr/logr"
"github.com/pkg/errors"
)
@ -38,7 +38,7 @@ type Pool struct {
RP *redis.Pool
}
func OpenPool(s RdSettings, logger *colog.CoLog) *Pool {
func OpenPool(s RdSettings, logger logr.Logger) *Pool {
p := &Pool{S: s, RP: newRedisPool(s, logger)}
return p
}
@ -70,7 +70,7 @@ func (p *Pool) Do(cmd string, args ...interface{}) (interface{}, error) {
type rdb struct {
orig redis.Conn
name string
logger *colog.CoLog
logger logr.Logger
s RdSettings
}
@ -93,7 +93,7 @@ func (rd *rdb) Do(command string, args ...interface{}) (interface{}, error) {
for i := 0; i < len(args); i++ {
s += fmt.Sprintf("%v ", args[i])
}
rd.logger.Output(colog.LDebug, 5, fmt.Sprintf("%s %s", command, s))
rd.logger.WithCallDepth(2).V(1).Info(command + " " + s)
}
} else if rd.s.LogLevel > 0 {
//for this level not logging too verbose commands
@ -105,7 +105,7 @@ func (rd *rdb) Do(command string, args ...interface{}) (interface{}, error) {
if len(args) > 5 {
s += " ..."
}
rd.logger.Output(colog.LDebug, 5, fmt.Sprintf("%s %s", command, s))
rd.logger.WithCallDepth(2).V(1).Info(command + " " + s)
}
}
return rd.orig.Do(command, args...)
@ -118,7 +118,7 @@ func (rd *rdb) Send(command string, args ...interface{}) error {
for i := 0; i < len(args); i++ {
s += fmt.Sprintf("%v ", args[i])
}
rd.logger.Output(colog.LDebug, 5, fmt.Sprintf("%s %s", command, s))
rd.logger.WithCallDepth(2).V(1).Info(command + " " + s)
}
}
return rd.orig.Send(command, args...)
@ -128,7 +128,7 @@ func (rd *rdb) Receive() (interface{}, error) {
return rd.orig.Receive()
}
func newRedisPool(s RdSettings, logger *colog.CoLog) *redis.Pool {
func newRedisPool(s RdSettings, logger logr.Logger) *redis.Pool {
maxIdle := s.MaxIdle
if maxIdle == 0 {
@ -149,10 +149,13 @@ func newRedisPool(s RdSettings, logger *colog.CoLog) *redis.Pool {
if err != nil {
return nil, errors.WithStack(err)
}
if len(s.Prefix) > 0 {
logger = logger.WithValues("rd_prefix", s.Prefix)
}
c := &rdb{
orig: orig,
name: s.Prefix,
logger: logger.Clone().AddPrefix("[" + s.Prefix + "] "),
logger: logger,
s: s,
}

View File

@ -1,25 +1,29 @@
package rdb_test
import (
"log"
"os"
"testing"
"git.bit5.ru/backend/colog"
"git.bit5.ru/backend/rdb"
"github.com/stretchr/testify/assert"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
)
const verbosity = 0
func getPool() *rdb.Pool {
pool := rdb.OpenPool(rdb.RdSettings{Host: "localhost", Port: 6379, Db: 10, ClientName: "test"}, getLogger())
//TODO: use ENV settings as well
pool := rdb.OpenPool(rdb.RdSettings{Host: "localhost", Port: 6379, Db: 10, ClientName: "test", LogLevel: verbosity}, getLogger())
return pool
}
func getLogger() *colog.CoLog {
tlog := colog.NewCoLog(os.Stderr, "", 0)
ft := &colog.StdFormatter{Flag: 0}
ft.ColorSupported(true)
tlog.SetFormatter(ft)
return tlog
func getLogger() logr.Logger {
//TODO: use ENV settings as well
stdr.SetVerbosity(verbosity)
return stdr.New(log.New(os.Stdout, "", log.Lshortfile))
}
func TestBadConnection(t *testing.T) {