app/task/logger.go

40 lines
821 B
Go

package task
import (
"fmt"
"time"
"github.com/rs/zerolog"
)
type mylogger struct {
}
func (ml *mylogger) Info(msg string, keysAndValues ...interface{}) {
chain := l.Info()
formatKV(chain, keysAndValues...)
chain.Msg(msg)
}
func (ml *mylogger) Error(err error, msg string, keysAndValues ...interface{}) {
chain := l.Error().Err(err)
formatKV(chain, keysAndValues...)
chain.Msg(msg)
}
func formatKV(chain *zerolog.Event, keysAndValues ...interface{}) {
count := len(keysAndValues)
for i := 0; i < count; i += 2 {
switch v := keysAndValues[i+1].(type) {
case string:
chain.Str(keysAndValues[i].(string), v)
case int:
chain.Int(keysAndValues[i].(string), v)
case time.Time:
chain.Time(keysAndValues[i].(string), v)
default:
chain.Str(keysAndValues[i].(string), fmt.Sprint(v))
}
}
}