From e0a3baffb980de3d741e7e205731370a595a205a Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 2 Nov 2022 18:12:57 +0300 Subject: [PATCH] Starting to migrate to unified logger --- db.go | 4 +++- db_test.go | 2 +- logger.go | 10 +++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/db.go b/db.go index 9068cec..74193d2 100644 --- a/db.go +++ b/db.go @@ -74,7 +74,9 @@ func OpenPool(s Settings) *Pool { 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) sess := con.NewSession(&EventReceiver{logger: logger, s: p.S}) diff --git a/db_test.go b/db_test.go index 0634efa..f884743 100644 --- a/db_test.go +++ b/db_test.go @@ -17,7 +17,7 @@ var logger = stdr.New(log.New(os.Stdout, "", log.Lshortfile)) func getSettings() db.Settings { //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 { diff --git a/logger.go b/logger.go index ce17f0c..8833756 100644 --- a/logger.go +++ b/logger.go @@ -14,13 +14,13 @@ type EventReceiver struct { // Event receives a simple notification when various events occur 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 // optional key/value data 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 @@ -43,15 +43,15 @@ func (n *EventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[st if sp != -1 { query := sql[:sp] if n.s.LogLevel > 1 { - n.logger.WithCallDepth(3).V(0).Info(sql) + n.logger.WithCallDepth(3).V(1).Info(sql) } else { if len(sql) > 50 { sql = sql[:50] + "..." } if query == "SELECT" && n.s.LogLevel > 0 { - n.logger.WithCallDepth(3).V(0).Info(sql) + n.logger.WithCallDepth(3).V(1).Info(sql) } else { - n.logger.WithCallDepth(3).V(0).Info(sql) + n.logger.WithCallDepth(3).V(1).Info(sql) } } }