Use go-logr

This commit is contained in:
Balakrishnan Balasubramanian 2022-06-17 18:23:06 -04:00
parent 7e92a042ff
commit 962c533fff
5 changed files with 32 additions and 52 deletions

View File

@ -63,20 +63,21 @@ func Run(configPath string) error {
}
func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgram telegram.TelegramSender) {
log := log.WithValues("feed", feed.Name)
sd, err := scheduler.ShouldDownload(feed.Name, feed.Cron)
if err != nil {
log.Error(err, "shouldDownload failed", "feed", feed.Name)
log.Error(err, "shouldDownload failed")
return
}
if !sd {
log.Info("skipping feed due to schedule", "feed", feed.Name)
log.Info("skipping feed due to schedule")
return
}
fdb, err := db.NewDB(dbDir, feed.Name)
if err != nil {
log.Error(err, "failed to get db", "feed", feed.Name, "db_dir", dbDir)
log.Error(err, "failed to get db", "db_dir", dbDir)
return
}
@ -84,13 +85,13 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
for _, url := range feed.Url {
data, err := Download(url, feed.Proxy)
if err != nil {
log.Error(err, "download failed", "feed", feed.Name, "url", url, "proxy", feed.Proxy)
log.Error(err, "download failed", "url", url, "proxy", feed.Proxy)
return
}
currentEntries, err := parser.ParseFeed(feed.Type, data)
if err != nil {
log.Error(err, "feed parsing failed", "feed", feed.Name, "url", url, "data", data)
log.Error(err, "feed parsing failed", "url", url, "data", data)
return
}
entries = append(entries, currentEntries...)
@ -106,7 +107,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
}{}
defer func() {
log.Info("done processing feed", "feed", feed.Name, "total", stat.Total, "new", stat.New, "filtered", stat.Filtered, "error", stat.Error)
log.Info("done processing feed", "total", stat.Total, "new", stat.New, "filtered", stat.Filtered, "error", stat.Error)
}()
stat.Total = len(entries)
@ -134,7 +135,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
} else {
newEntries, err = fdb.Filter(entries)
if err != nil {
log.Error(err, "failed to filter entries", "feed", feed.Name)
log.Error(err, "failed to filter entries")
return
}
stat.New = len(newEntries)
@ -147,7 +148,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
}
err := tgram.SendLink(entry.Link, feed.Channel, feed.Rhash, feed.Title)
if err != nil {
log.Error(err, "failed to send to telegram", "feed", feed.Name, "link", entry.Link, "channel", feed.Channel, "rhash", feed.Rhash)
log.Error(err, "failed to send to telegram", "link", entry.Link, "channel", feed.Channel, "rhash", feed.Rhash)
r.Status = db.Error
stat.Error++
} else {
@ -157,7 +158,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
}
err = fdb.Save(records)
if err != nil {
log.Error(err, "failed to save sent records", "feed", feed.Name, "num_records", len(records))
log.Error(err, "failed to save sent records", "num_records", len(records))
}
}

View File

@ -9,10 +9,16 @@ import (
"strings"
"time"
"go.balki.me/tss/log"
"github.com/go-logr/logr"
"go.balki.me/tss/parser"
)
var log = logr.Discard()
func SetLogger(log logr.Logger) {
log = log
}
type Status string
const (
@ -64,7 +70,8 @@ func NewDB(storageDir, feedName string) (DB, error) {
reader := csv.NewReader(f)
records, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("failed to parse csv, path:%v, error:%w", dbPath, err)
log.Error(err, "failed to parse csv", "path", dbPath)
return nil, err
}
for _, rec := range records {
var recStatus Status = Status(rec[2])

View File

@ -6,40 +6,6 @@ import (
"go.uber.org/zap"
)
var Logger *zap.SugaredLogger
var GLogr logr.Logger
var Debug, Info, Warn, Error, Panic func(msg string, keysAndValues ...interface{})
func init() {
//json logging
cfg := zap.NewProductionConfig()
devCfg := zap.NewDevelopmentConfig()
// Readable time stamp
cfg.EncoderConfig.EncodeTime = devCfg.EncoderConfig.EncodeTime
// Uncomment to enable debug logging
// cfg.Level = devCfg.Level
logger, err := cfg.Build()
if err != nil {
println("unable to intialize zap log")
panic(err)
}
Logger = logger.Sugar()
Debug = Logger.Debugw
Info = Logger.Infow
Warn = Logger.Warnw
Error = Logger.Errorw
Panic = Logger.Panicw
GLogr = zapr.NewLogger(logger)
}
func GetZapLogger() logr.Logger {
//json logging
cfg := zap.NewProductionConfig()
@ -48,9 +14,6 @@ func GetZapLogger() logr.Logger {
// Readable time stamp
cfg.EncoderConfig.EncodeTime = devCfg.EncoderConfig.EncodeTime
// Uncomment to enable debug logging
// cfg.Level = devCfg.Level
logger, err := cfg.Build()
if err != nil {
println("unable to intialize zap log")

View File

@ -9,7 +9,9 @@ import (
)
func main() {
schedule.SetLogger(log.GetZapLogger())
logger := log.GetZapLogger()
schedule.SetLogger(logger.WithName("schedule"))
app.SetLogger(logger.WithName("app"))
var configPath string
flag.StringVar(&configPath, "config", "./tss.yml", "path to tss.yml config")

View File

@ -10,10 +10,16 @@ import (
"net/url"
"time"
"github.com/go-logr/logr"
"go.balki.me/tss/limiter"
"go.balki.me/tss/log"
)
var log = logr.Discard()
func SetLogger(log logr.Logger) {
log = log
}
type TelegramSender interface {
SendLink(link, channel, rhash, title string) error
}
@ -26,6 +32,7 @@ type telegramSender struct {
}
func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
log := log.WithValues("link", link, "channel", channel)
if title == "" {
title = "Link"
}
@ -59,10 +66,10 @@ func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
return err
}
if res.StatusCode != http.StatusOK {
log.Error("telegram send failed", "status", res.Status, "request", data, "response", responseText)
log.Error(nil, "telegram send failed", "status", res.Status, "request", data, "response", responseText)
return errors.New("telegram send failed")
}
log.Info("sent message on telegram", "link", link, "channel", channel, "response", responseText)
log.Info("sent message on telegram", "response", responseText)
return nil
}