check interface assertion, reduce condition complexity
@@ -9,6 +9,8 @@ import (
var _ logrus.Hook = &CustomLogs{}
+const customLogField = "custom"
+
type CustomLogs struct{}
func (c *CustomLogs) Levels() []logrus.Level {
@@ -30,7 +32,7 @@ func (c *CustomLogs) Fire(entry *logrus.Entry) error {
return nil
}
-func fire(entry *logrus.Entry, fieldsKey, ctxKey request.ContextKey) {
+func fire(entry *logrus.Entry, bodyKey, ctxKey request.ContextKey) {
var evalCtx *eval.Context
if ctx, ok := entry.Context.Value(ctxKey).(*eval.Context); ok {
@@ -39,9 +41,17 @@ func fire(entry *logrus.Entry, fieldsKey, ctxKey request.ContextKey) {
evalCtx = eval.NewContext(nil, nil)
}
- if bodies := entry.Context.Value(fieldsKey); bodies != nil {
- if fields := eval.ApplyCustomLogs(evalCtx.HCLContext(), bodies.([]hcl.Body), entry); len(fields) > 0 {
- entry.Data["custom"] = fields
+ bodies := entry.Context.Value(bodyKey)
+ if bodies == nil {
+ return
+ }
+
+ hclBodies, ok := bodies.([]hcl.Body)
+ if !ok {
+ return
}
+
+ if fields := eval.ApplyCustomLogs(evalCtx.HCLContext(), hclBodies, entry); len(fields) > 0 {
+ entry.Data[customLogField] = fields
}
}
