db/logger.go

65 lines
1.7 KiB
Go
Raw Permalink Normal View History

2022-10-26 17:28:42 +03:00
package db
import (
"strings"
"git.bit5.ru/backend/colog"
)
// EventReceiver logs db stuff if neccessary
type EventReceiver struct {
logger *colog.CoLog
s Settings
}
// Event receives a simple notification when various events occur
func (n *EventReceiver) Event(eventName string) {
if n.logger != nil {
n.logger.Output(colog.LDebug, 5, 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.logger != nil {
n.logger.Output(colog.LDebug, 5, 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.logger != nil {
sql := kvs["sql"]
sp := strings.Index(sql, " ")
if sp != -1 {
query := sql[:sp]
if n.s.LogLevel > 1 {
n.logger.Output(colog.LDebug, 6, sql)
} else {
if len(sql) > 50 {
sql = sql[:50] + "..."
}
if query == "SELECT" && n.s.LogLevel > 0 {
n.logger.Output(colog.LDebug, 6, sql)
} else {
n.logger.Output(colog.LDebug, 6, sql)
}
}
}
}
}