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,10 +90,12 @@ func (e *executor) execute(ctx context.Context, ast *sql.Query) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
if log.IsError() {
log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{ log.WithPrefix(logKeyDB).WithFields(map[string]interface{}{
logKeyId: e.id, logKeyStack: string(debug.Stack()), logKeyId: e.id, logKeyStack: string(debug.Stack()),
}).Errorln(err) }).Errorln(err)
} }
}
}() }()
// Loop over the defined query statements and // Loop over the defined query statements and
@ -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,15 +179,29 @@ 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() {
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(), logKeyTime: time.Since(now).String(),
}).Debugln(stm) }).Debugln(stm)
}
case error: case error:
log.WithFields(map[string]interface{}{ 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(), logKeyTime: time.Since(now).String(),
logKeyError: detail(err), logKeyError: detail(err),
}).Errorln(stm) }).Errorln(stm)
} }
}
// If we are not inside a global transaction // If we are not inside a global transaction
// then we can output the statement response // then we can output the statement response

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),
} }
if log.IsTrace() {
log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{
logKeyId: e.id, logKeyId: e.id,
logKeyNS: e.ns, logKeyNS: e.ns,
logKeyDB: e.db, logKeyDB: e.db,
}).Traceln(stm) }).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),
} }
if log.IsTrace() {
log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{ log.WithPrefix(logKeyExe).WithFields(map[string]interface{}{
logKeyId: e.id, logKeyId: e.id,
logKeyNS: e.ns, logKeyNS: e.ns,
logKeyDB: e.db, logKeyDB: e.db,
}).Traceln(stm) }).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...)