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) {
msgs := splitMsgs(v)
log.Error(msg).
Str("method", msgs["method"].(string)).
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
evt := log.Error(msg)
sendEvt(evt, msgs)
}
func (retryableLogger) Info(msg string, v ...any) {
msgs := splitMsgs(v)
log.Info(msg).
Str("method", msgs["method"].(string)).
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
evt := log.Info(msg)
sendEvt(evt, msgs)
}
func (retryableLogger) Debug(msg string, v ...any) {
msgs := splitMsgs(v)
log.Debug(msg).
Str("method", msgs["method"].(string)).
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
evt := log.Debug(msg)
sendEvt(evt, msgs)
}
func (retryableLogger) Warn(msg string, v ...any) {
msgs := splitMsgs(v)
log.Warn(msg).
Str("method", msgs["method"].(string)).
Stringer("url", msgs["url"].(fmt.Stringer)).
Send()
evt := log.Warn(msg)
sendEvt(evt, msgs)
}
func splitMsgs(v []any) map[string]any {
@ -65,3 +58,19 @@ func splitMsgs(v []any) map[string]any {
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 {
i, err := strconv.Atoi(s)
if err != nil {
log.Debug("Message variable is not an integer, returning empty string").Str("var", s).Send()
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 ""
}
log.Debug("Message variable found, returning").Int("var", i).Str("found", matches[i]).Send()
return matches[i]
})