47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
|
package logger
|
||
|
|
||
|
import (
|
||
|
"github.com/mymmrac/telego"
|
||
|
"go.uber.org/zap"
|
||
|
"go.uber.org/zap/zapcore"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type telegoWrapper struct {
|
||
|
logger *zap.SugaredLogger
|
||
|
replacer *strings.Replacer
|
||
|
debugMode bool
|
||
|
}
|
||
|
|
||
|
func WrapForTelego(logger *zap.SugaredLogger, debugMode bool) telego.BotOption {
|
||
|
return func(bot *telego.Bot) error {
|
||
|
return telego.WithLogger(&telegoWrapper{
|
||
|
logger: logger,
|
||
|
replacer: strings.NewReplacer(bot.Token(), "[token]"),
|
||
|
debugMode: debugMode,
|
||
|
})(bot)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *telegoWrapper) Debugf(format string, args ...any) {
|
||
|
if l.debugMode {
|
||
|
l.logf(zapcore.DebugLevel, format, args...)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (l *telegoWrapper) Errorf(format string, args ...any) {
|
||
|
l.logf(zapcore.ErrorLevel, format, args...)
|
||
|
}
|
||
|
|
||
|
func (l *telegoWrapper) logf(lvl zapcore.Level, template string, args ...interface{}) {
|
||
|
escapedArgs := make([]interface{}, len(args))
|
||
|
for i, arg := range args {
|
||
|
if val, ok := arg.(string); ok {
|
||
|
escapedArgs[i] = l.replacer.Replace(val)
|
||
|
continue
|
||
|
}
|
||
|
escapedArgs[i] = arg
|
||
|
}
|
||
|
l.logger.Logf(lvl, l.replacer.Replace(template), escapedArgs...)
|
||
|
}
|