63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package db
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/go-logr/logr"
|
|
)
|
|
|
|
// EventReceiver logs db stuff if neccessary
|
|
type EventReceiver struct {
|
|
logger logr.Logger
|
|
s Settings
|
|
}
|
|
|
|
// Event receives a simple notification when various events occur
|
|
func (n *EventReceiver) Event(eventName string) {
|
|
if n.s.LogLevel == 0 {
|
|
return
|
|
}
|
|
n.logger.WithCallDepth(2).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) {
|
|
if n.s.LogLevel == 0 {
|
|
return
|
|
}
|
|
n.logger.WithCallDepth(2).V(1).Info(eventName)
|
|
}
|
|
|
|
// EventErr receives a notification of an error if one occurs
|
|
func (n *EventReceiver) EventErr(eventName string, err error) error { return err }
|
|
|
|
// EventErrKv receives a notification of an error if one occurs along with
|
|
// optional key/value data
|
|
func (n *EventReceiver) EventErrKv(eventName string, err error, kvs map[string]string) error {
|
|
return err
|
|
}
|
|
|
|
// Timing receives the time an event took to happen
|
|
func (n *EventReceiver) Timing(eventName string, nanoseconds int64) {
|
|
}
|
|
|
|
// TimingKv receives the time an event took to happen along with optional key/value data
|
|
func (n *EventReceiver) TimingKv(eventName string, nanoseconds int64, kvs map[string]string) {
|
|
if n.s.LogLevel == 0 {
|
|
return
|
|
}
|
|
sql := kvs["sql"]
|
|
sp := strings.Index(sql, " ")
|
|
if sp != -1 {
|
|
if n.s.LogLevel > 1 {
|
|
n.logger.WithCallDepth(3).V(1).Info(sql)
|
|
} else {
|
|
if len(sql) > 50 {
|
|
sql = sql[:50] + "..."
|
|
}
|
|
n.logger.WithCallDepth(3).V(1).Info(sql)
|
|
}
|
|
}
|
|
}
|