Starting to migrate to unified logger

This commit is contained in:
Pavel Shevaev 2022-11-02 18:12:57 +03:00
parent 3969030dc7
commit e0a3baffb9
3 changed files with 9 additions and 7 deletions

4
db.go
View File

@ -74,7 +74,9 @@ func OpenPool(s Settings) *Pool {
func GetDBC(p *Pool, logger logr.Logger) *DBC { func GetDBC(p *Pool, logger logr.Logger) *DBC {
logger = logger.WithValues("db", p.S.Prefix) if len(p.S.Prefix) > 0 {
logger = logger.WithValues("db", p.S.Prefix)
}
con := dbr.NewConnection(p.DB, nil) con := dbr.NewConnection(p.DB, nil)
sess := con.NewSession(&EventReceiver{logger: logger, s: p.S}) sess := con.NewSession(&EventReceiver{logger: logger, s: p.S})

View File

@ -17,7 +17,7 @@ var logger = stdr.New(log.New(os.Stdout, "", log.Lshortfile))
func getSettings() db.Settings { func getSettings() db.Settings {
//TODO: use ENV settings as well //TODO: use ENV settings as well
return db.Settings{Host: "127.0.0.1", Port: "3306", User: "root", Pass: "test", Name: "tests"} return db.Settings{Host: "127.0.0.1", Port: "3306", User: "root", Pass: "test", Name: "tests", Prefix: "tests"}
} }
func getPool() *db.Pool { func getPool() *db.Pool {

View File

@ -14,13 +14,13 @@ type EventReceiver struct {
// Event receives a simple notification when various events occur // Event receives a simple notification when various events occur
func (n *EventReceiver) Event(eventName string) { func (n *EventReceiver) Event(eventName string) {
n.logger.WithCallDepth(3).V(0).Info(eventName) n.logger.WithCallDepth(3).V(1).Info(eventName)
} }
// EventKv receives a notification when various events occur along with // EventKv receives a notification when various events occur along with
// optional key/value data // optional key/value data
func (n *EventReceiver) EventKv(eventName string, kvs map[string]string) { func (n *EventReceiver) EventKv(eventName string, kvs map[string]string) {
n.logger.WithCallDepth(3).V(0).Info(eventName) n.logger.WithCallDepth(3).V(1).Info(eventName)
} }
// EventErr receives a notification of an error if one occurs // EventErr receives a notification of an error if one occurs
@ -43,15 +43,15 @@ func (n *EventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[st
if sp != -1 { if sp != -1 {
query := sql[:sp] query := sql[:sp]
if n.s.LogLevel > 1 { if n.s.LogLevel > 1 {
n.logger.WithCallDepth(3).V(0).Info(sql) n.logger.WithCallDepth(3).V(1).Info(sql)
} else { } else {
if len(sql) > 50 { if len(sql) > 50 {
sql = sql[:50] + "..." sql = sql[:50] + "..."
} }
if query == "SELECT" && n.s.LogLevel > 0 { if query == "SELECT" && n.s.LogLevel > 0 {
n.logger.WithCallDepth(3).V(0).Info(sql) n.logger.WithCallDepth(3).V(1).Info(sql)
} else { } else {
n.logger.WithCallDepth(3).V(0).Info(sql) n.logger.WithCallDepth(3).V(1).Info(sql)
} }
} }
} }