Use go-logr
This commit is contained in:
parent
7e92a042ff
commit
962c533fff
19
app/app.go
19
app/app.go
@ -63,20 +63,21 @@ func Run(configPath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgram telegram.TelegramSender) {
|
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)
|
sd, err := scheduler.ShouldDownload(feed.Name, feed.Cron)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err, "shouldDownload failed", "feed", feed.Name)
|
log.Error(err, "shouldDownload failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sd {
|
if !sd {
|
||||||
log.Info("skipping feed due to schedule", "feed", feed.Name)
|
log.Info("skipping feed due to schedule")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fdb, err := db.NewDB(dbDir, feed.Name)
|
fdb, err := db.NewDB(dbDir, feed.Name)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,13 +85,13 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
|
|||||||
for _, url := range feed.Url {
|
for _, url := range feed.Url {
|
||||||
data, err := Download(url, feed.Proxy)
|
data, err := Download(url, feed.Proxy)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
currentEntries, err := parser.ParseFeed(feed.Type, data)
|
currentEntries, err := parser.ParseFeed(feed.Type, data)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
entries = append(entries, currentEntries...)
|
entries = append(entries, currentEntries...)
|
||||||
@ -106,7 +107,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
|
|||||||
}{}
|
}{}
|
||||||
|
|
||||||
defer func() {
|
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)
|
stat.Total = len(entries)
|
||||||
@ -134,7 +135,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
|
|||||||
} else {
|
} else {
|
||||||
newEntries, err = fdb.Filter(entries)
|
newEntries, err = fdb.Filter(entries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err, "failed to filter entries", "feed", feed.Name)
|
log.Error(err, "failed to filter entries")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
stat.New = len(newEntries)
|
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)
|
err := tgram.SendLink(entry.Link, feed.Channel, feed.Rhash, feed.Title)
|
||||||
if err != nil {
|
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
|
r.Status = db.Error
|
||||||
stat.Error++
|
stat.Error++
|
||||||
} else {
|
} else {
|
||||||
@ -157,7 +158,7 @@ func ProcessFeed(feed *FeedCfg, scheduler schedule.Scheduler, dbDir string, tgra
|
|||||||
}
|
}
|
||||||
err = fdb.Save(records)
|
err = fdb.Save(records)
|
||||||
if err != nil {
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
db/db.go
11
db/db.go
@ -9,10 +9,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.balki.me/tss/log"
|
"github.com/go-logr/logr"
|
||||||
"go.balki.me/tss/parser"
|
"go.balki.me/tss/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logr.Discard()
|
||||||
|
|
||||||
|
func SetLogger(log logr.Logger) {
|
||||||
|
log = log
|
||||||
|
}
|
||||||
|
|
||||||
type Status string
|
type Status string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -64,7 +70,8 @@ func NewDB(storageDir, feedName string) (DB, error) {
|
|||||||
reader := csv.NewReader(f)
|
reader := csv.NewReader(f)
|
||||||
records, err := reader.ReadAll()
|
records, err := reader.ReadAll()
|
||||||
if err != nil {
|
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 {
|
for _, rec := range records {
|
||||||
var recStatus Status = Status(rec[2])
|
var recStatus Status = Status(rec[2])
|
||||||
|
37
log/log.go
37
log/log.go
@ -6,40 +6,6 @@ import (
|
|||||||
"go.uber.org/zap"
|
"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 {
|
func GetZapLogger() logr.Logger {
|
||||||
//json logging
|
//json logging
|
||||||
cfg := zap.NewProductionConfig()
|
cfg := zap.NewProductionConfig()
|
||||||
@ -48,9 +14,6 @@ func GetZapLogger() logr.Logger {
|
|||||||
// Readable time stamp
|
// Readable time stamp
|
||||||
cfg.EncoderConfig.EncodeTime = devCfg.EncoderConfig.EncodeTime
|
cfg.EncoderConfig.EncodeTime = devCfg.EncoderConfig.EncodeTime
|
||||||
|
|
||||||
// Uncomment to enable debug logging
|
|
||||||
// cfg.Level = devCfg.Level
|
|
||||||
|
|
||||||
logger, err := cfg.Build()
|
logger, err := cfg.Build()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println("unable to intialize zap log")
|
println("unable to intialize zap log")
|
||||||
|
4
main.go
4
main.go
@ -9,7 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
schedule.SetLogger(log.GetZapLogger())
|
logger := log.GetZapLogger()
|
||||||
|
schedule.SetLogger(logger.WithName("schedule"))
|
||||||
|
app.SetLogger(logger.WithName("app"))
|
||||||
|
|
||||||
var configPath string
|
var configPath string
|
||||||
flag.StringVar(&configPath, "config", "./tss.yml", "path to tss.yml config")
|
flag.StringVar(&configPath, "config", "./tss.yml", "path to tss.yml config")
|
||||||
|
@ -10,10 +10,16 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-logr/logr"
|
||||||
"go.balki.me/tss/limiter"
|
"go.balki.me/tss/limiter"
|
||||||
"go.balki.me/tss/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var log = logr.Discard()
|
||||||
|
|
||||||
|
func SetLogger(log logr.Logger) {
|
||||||
|
log = log
|
||||||
|
}
|
||||||
|
|
||||||
type TelegramSender interface {
|
type TelegramSender interface {
|
||||||
SendLink(link, channel, rhash, title string) error
|
SendLink(link, channel, rhash, title string) error
|
||||||
}
|
}
|
||||||
@ -26,6 +32,7 @@ type telegramSender struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
|
func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
|
||||||
|
log := log.WithValues("link", link, "channel", channel)
|
||||||
if title == "" {
|
if title == "" {
|
||||||
title = "Link"
|
title = "Link"
|
||||||
}
|
}
|
||||||
@ -59,10 +66,10 @@ func (ts *telegramSender) SendLink(link, channel, rhash, title string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if res.StatusCode != http.StatusOK {
|
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")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user