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

add logger with configurable log level #16

Merged
merged 1 commit into from
Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/sirupsen/logrus"
"net/url"
"sort"
"sync"
Expand All @@ -73,6 +74,8 @@ var (
mu sync.RWMutex
sqlDrivers = make(map[string]driver.Driver)
strategies = make(map[string]Strategy)

logger *logrus.Logger
)

// RegisterSQLDriver makes a database driver available by the provided name.
Expand Down Expand Up @@ -137,8 +140,14 @@ func Strategies() []string {
return list
}

// SetLogLevel specifies the logrus.Level for the hotload driver's logger
func SetLogLevel(level logrus.Level) {
logger.SetLevel(level)
}

func init() {
sql.Register("hotload", &hdriver{ctx: context.Background(), cgroup: make(map[string]*chanGroup)})
logger = logrus.New()
}

// hdriver is the hotload driver.
Expand Down Expand Up @@ -167,13 +176,15 @@ func (cg *chanGroup) run() {
select {
case <-cg.parentCtx.Done():
cg.cancel()
logger.Debug("cancelling chanGroup context")
return
case v := <-cg.values:
if v == cg.value {
// next update is the same, just ignore it
continue
}
cg.valueChanged(v)
logger.Debug("connection information changed")
}
}
}
Expand All @@ -186,7 +197,6 @@ func (cg *chanGroup) valueChanged(v string) {
cg.resetConnections()

cg.value = v

}

func (cg *chanGroup) resetConnections() {
Expand Down Expand Up @@ -219,10 +229,11 @@ func (cg *chanGroup) Open() (driver.Conn, error) {
func (cg *chanGroup) parseValues(vs url.Values) {
cg.mu.Lock()
defer cg.mu.Unlock()

logger.WithFields(logrus.Fields{"urlValues": vs}).Debug("parsing values")
if v, ok := vs[forceKill]; ok {
firstValue := v[0]
cg.forceKill = firstValue == "true"
logger.Debug("forceKill set to true")
}
}

Expand Down