diff --git a/db/executor.go b/db/executor.go index 5cf23845..cfe7decb 100644 --- a/db/executor.go +++ b/db/executor.go @@ -90,9 +90,11 @@ func (e *executor) execute(ctx context.Context, ast *sql.Query) { defer func() { if err := recover(); err != nil { - log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{ - logKeyId: e.id, logKeyStack: string(debug.Stack()), - }).Errorln(err) + if log.IsError() { + log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{ + 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 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 // then reset the error to nil so that the // next statement is not ignored. @@ -195,14 +179,28 @@ func (e *executor) conduct(ctx context.Context, stm sql.Statement) { switch err.(type) { default: - log.WithFields(map[string]interface{}{ - logKeyTime: time.Since(now).String(), - }).Debugln(stm) + if log.IsDebug() { + log.WithPrefix(logKeySql).WithFields(map[string]interface{}{ + 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: - log.WithFields(map[string]interface{}{ - logKeyTime: time.Since(now).String(), - logKeyError: detail(err), - }).Errorln(stm) + if log.IsError() { + log.WithPrefix(logKeySql).WithFields(map[string]interface{}{ + logKeyId: e.id, + 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 diff --git a/db/fetch.go b/db/fetch.go index 3b6600a8..aa5c7ca1 100644 --- a/db/fetch.go +++ b/db/fetch.go @@ -356,11 +356,13 @@ func (e *executor) fetchThing(ctx context.Context, val *sql.Thing, doc *data.Doc Version: sql.Expr(ver), } - log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ - logKeyId: e.id, - logKeyNS: e.ns, - logKeyDB: e.db, - }).Traceln(stm) + if log.IsTrace() { + log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ + logKeyId: e.id, + logKeyNS: e.ns, + logKeyDB: e.db, + }).Traceln(stm) + } res, err := e.executeSelect(ctx, stm) if err != nil { @@ -395,11 +397,13 @@ func (e *executor) fetchArray(ctx context.Context, val []interface{}, doc *data. Version: sql.Expr(ver), } - log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ - logKeyId: e.id, - logKeyNS: e.ns, - logKeyDB: e.db, - }).Traceln(stm) + if log.IsTrace() { + log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ + logKeyId: e.id, + logKeyNS: e.ns, + logKeyDB: e.db, + }).Traceln(stm) + } res, err := e.executeSelect(ctx, stm) if err != nil { diff --git a/log/log.go b/log/log.go index 4d9b64b2..d048c3e5 100644 --- a/log/log.go +++ b/log/log.go @@ -104,6 +104,34 @@ func Hook(hook logrus.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{}) { if isTerminal { fmt.Print(v...)