Check if log level is enabled before creating entry

This commit is contained in:
Tobie Morgan Hitchcock 2019-11-18 10:44:14 +00:00
parent 9f4b696fab
commit cca75c70fc
3 changed files with 68 additions and 38 deletions

View file

@ -90,9 +90,11 @@ func (e *executor) execute(ctx context.Context, ast *sql.Query) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{ if log.IsError() {
logKeyId: e.id, logKeyStack: string(debug.Stack()), log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{
}).Errorln(err) logKeyId: e.id, logKeyStack: string(debug.Stack()),
}).Errorln(err)
}
} }
}() }()
@ -119,24 +121,6 @@ func (e *executor) conduct(ctx context.Context, stm sql.Statement) {
var buf []*Response var buf []*Response
var res []interface{} var res []interface{}
// When in debugging mode, log every sql
// query, along with the query execution
// speed, so we can analyse slow queries.
log := log.WithPrefix(logKeySql).WithFields(map[string]interface{}{
logKeyId: e.id,
logKeyKind: ctx.Value(ctxKeyKind),
logKeyVars: ctx.Value(ctxKeyVars),
})
if len(e.ns) != 0 {
log = log.WithField(logKeyNS, e.ns)
}
if len(e.db) != 0 {
log = log.WithField(logKeyDB, e.db)
}
// If we are not inside a global transaction // If we are not inside a global transaction
// then reset the error to nil so that the // then reset the error to nil so that the
// next statement is not ignored. // next statement is not ignored.
@ -195,14 +179,28 @@ func (e *executor) conduct(ctx context.Context, stm sql.Statement) {
switch err.(type) { switch err.(type) {
default: default:
log.WithFields(map[string]interface{}{ if log.IsDebug() {
logKeyTime: time.Since(now).String(), log.WithPrefix(logKeySql).WithFields(map[string]interface{}{
}).Debugln(stm) logKeyId: e.id,
logKeyNS: e.ns,
logKeyDB: e.db,
logKeyKind: ctx.Value(ctxKeyKind),
logKeyVars: ctx.Value(ctxKeyVars),
logKeyTime: time.Since(now).String(),
}).Debugln(stm)
}
case error: case error:
log.WithFields(map[string]interface{}{ if log.IsError() {
logKeyTime: time.Since(now).String(), log.WithPrefix(logKeySql).WithFields(map[string]interface{}{
logKeyError: detail(err), logKeyId: e.id,
}).Errorln(stm) logKeyNS: e.ns,
logKeyDB: e.db,
logKeyKind: ctx.Value(ctxKeyKind),
logKeyVars: ctx.Value(ctxKeyVars),
logKeyTime: time.Since(now).String(),
logKeyError: detail(err),
}).Errorln(stm)
}
} }
// If we are not inside a global transaction // If we are not inside a global transaction

View file

@ -356,11 +356,13 @@ func (e *executor) fetchThing(ctx context.Context, val *sql.Thing, doc *data.Doc
Version: sql.Expr(ver), Version: sql.Expr(ver),
} }
log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ if log.IsTrace() {
logKeyId: e.id, log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{
logKeyNS: e.ns, logKeyId: e.id,
logKeyDB: e.db, logKeyNS: e.ns,
}).Traceln(stm) logKeyDB: e.db,
}).Traceln(stm)
}
res, err := e.executeSelect(ctx, stm) res, err := e.executeSelect(ctx, stm)
if err != nil { if err != nil {
@ -395,11 +397,13 @@ func (e *executor) fetchArray(ctx context.Context, val []interface{}, doc *data.
Version: sql.Expr(ver), Version: sql.Expr(ver),
} }
log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ if log.IsTrace() {
logKeyId: e.id, log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{
logKeyNS: e.ns, logKeyId: e.id,
logKeyDB: e.db, logKeyNS: e.ns,
}).Traceln(stm) logKeyDB: e.db,
}).Traceln(stm)
}
res, err := e.executeSelect(ctx, stm) res, err := e.executeSelect(ctx, stm)
if err != nil { if err != nil {

View file

@ -104,6 +104,34 @@ func Hook(hook logrus.Hook) {
log.AddHook(hook) log.AddHook(hook)
} }
func IsPanic() bool {
return log.IsLevelEnabled(PanicLevel)
}
func IsFatal() bool {
return log.IsLevelEnabled(FatalLevel)
}
func IsError() bool {
return log.IsLevelEnabled(ErrorLevel)
}
func IsWarn() bool {
return log.IsLevelEnabled(WarnLevel)
}
func IsInfo() bool {
return log.IsLevelEnabled(InfoLevel)
}
func IsDebug() bool {
return log.IsLevelEnabled(DebugLevel)
}
func IsTrace() bool {
return log.IsLevelEnabled(TraceLevel)
}
func Display(v ...interface{}) { func Display(v ...interface{}) {
if isTerminal { if isTerminal {
fmt.Print(v...) fmt.Print(v...)