Add more debug logs and fix bounds check

This commit is contained in:
Elara 2022-12-11 13:53:26 -08:00
parent 770e6daa90
commit 65a05a3b4b
2 changed files with 29 additions and 17 deletions

View File

@ -22,34 +22,27 @@ type retryableLogger struct{}
func (retryableLogger) Error(msg string, v ...any) { func (retryableLogger) Error(msg string, v ...any) {
msgs := splitMsgs(v) msgs := splitMsgs(v)
log.Error(msg). evt := log.Error(msg)
Str("method", msgs["method"].(string)). sendEvt(evt, msgs)
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
} }
func (retryableLogger) Info(msg string, v ...any) { func (retryableLogger) Info(msg string, v ...any) {
msgs := splitMsgs(v) msgs := splitMsgs(v)
log.Info(msg). evt := log.Info(msg)
Str("method", msgs["method"].(string)). sendEvt(evt, msgs)
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
} }
func (retryableLogger) Debug(msg string, v ...any) { func (retryableLogger) Debug(msg string, v ...any) {
msgs := splitMsgs(v) msgs := splitMsgs(v)
log.Debug(msg). evt := log.Debug(msg)
Str("method", msgs["method"].(string)). sendEvt(evt, msgs)
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
} }
func (retryableLogger) Warn(msg string, v ...any) { func (retryableLogger) Warn(msg string, v ...any) {
msgs := splitMsgs(v) msgs := splitMsgs(v)
log.Warn(msg). evt := log.Warn(msg)
Str("method", msgs["method"].(string)). sendEvt(evt, msgs)
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
} }
func splitMsgs(v []any) map[string]any { func splitMsgs(v []any) map[string]any {
@ -65,3 +58,19 @@ func splitMsgs(v []any) map[string]any {
return out return out
} }
func sendEvt(evt logger.LogBuilder, msgs map[string]any) {
for name, val := range msgs {
switch val := val.(type) {
case int:
evt = evt.Int(name, val)
case string:
evt = evt.Str(name, val)
case fmt.Stringer:
evt = evt.Stringer(name, val)
default:
evt = evt.Any(name, val)
}
}
evt.Send()
}

View File

@ -109,13 +109,16 @@ func commentWorker(ctx context.Context, c *lemmy.Client, replyCh chan<- replyJob
job.Content = expandStr(reply.Msg, func(s string) string { job.Content = expandStr(reply.Msg, func(s string) string {
i, err := strconv.Atoi(s) i, err := strconv.Atoi(s)
if err != nil { if err != nil {
log.Debug("Message variable is not an integer, returning empty string").Str("var", s).Send()
return "" return ""
} }
if len(matches) > i+1 { if i+1 > len(matches) {
log.Debug("Message variable exceeds match length").Int("length", len(matches)).Int("var", i).Send()
return "" return ""
} }
log.Debug("Message variable found, returning").Int("var", i).Str("found", matches[i]).Send()
return matches[i] return matches[i]
}) })