1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-18 04:40:03 +02:00

Switch logging from standard log package to logrus.

This commit is contained in:
Henrik Hautakoski 2022-11-28 17:04:44 +01:00
parent a4caf6c003
commit 46b0db2ff2
2 changed files with 39 additions and 27 deletions

47
main.go
View file

@ -3,11 +3,12 @@ package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"time"
log "github.com/sirupsen/logrus"
"eosio-ship-trace-reader/config"
"eosio-ship-trace-reader/redis"
"eosio-ship-trace-reader/telegram"
@ -45,7 +46,7 @@ func readerLoop() {
switch state {
case RS_CONNECT:
recon_cnt++
log.Printf("Connecting to ship at: %s (Try %d)", conf.ShipApi, recon_cnt)
log.Infof("Connecting to ship at: %s (Try %d)", conf.ShipApi, recon_cnt)
err := shClient.Connect(conf.ShipApi)
if err != nil {
log.Println(err)
@ -53,12 +54,12 @@ func readerLoop() {
if recon_cnt >= 3 {
msg := fmt.Sprintf("Failed to connect to ship at '%s'", conf.ShipApi)
if err = telegram.Send(msg); err != nil {
log.Println(err)
log.WithError(err).Error("Failed to send to telegram")
}
recon_cnt = 0
}
log.Printf("Trying again in 5 seconds ....")
log.Info("Trying again in 5 seconds ....")
time.Sleep(5 * time.Second)
break
}
@ -70,13 +71,13 @@ func readerLoop() {
}
// Connected
log.Printf("Connected, Start: %d, End: %d", shClient.StartBlock, shClient.EndBlock)
log.Infof("Connected, Start: %d, End: %d", shClient.StartBlock, shClient.EndBlock)
state = RS_READ
recon_cnt = 0
case RS_READ:
err := shClient.Read()
if err != nil {
log.Print(err.Error())
log.WithError(err).Error("Failed to read from ship")
// Reconnect
if err.Type == shipclient.ErrSockRead {
@ -109,10 +110,10 @@ func run() {
for {
select {
case <-interrupt:
log.Println("Interrupt, closing")
log.Info("Interrupt, closing")
if !shClient.IsOpen() {
log.Println("ship client not connected, exiting...")
log.Info("ship client not connected, exiting...")
return
}
@ -120,23 +121,33 @@ func run() {
// waiting (with timeout) for the server to close the connection.
err := shClient.SendCloseMessage()
if err != nil {
log.Println("failed to send close message to ship server", err)
log.WithError(err).Info("failed to send close message to ship server")
}
select {
case <-done:
log.Println("Closed")
log.Info("Closed")
case <-time.After(time.Second * 10):
log.Println("Timeout")
log.Info("Timeout")
}
return
case <-done:
log.Println("Closed")
log.Info("Closed")
return
}
}
}
func init() {
// Initialize logger
formatter := log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.0000",
}
log.SetFormatter(&formatter)
}
func main() {
var err error
@ -159,10 +170,10 @@ func main() {
// Write PID file
if len(*pidFile) > 0 {
log.Printf("Writing pid to: %s", *pidFile)
log.Infof("Writing pid to: %s", *pidFile)
err = pid.Save(*pidFile)
if err != nil {
log.Println(err)
log.WithError(err).Fatal("failed to write pid file")
return
}
}
@ -170,21 +181,21 @@ func main() {
// Parse config
conf, err = config.Load(*configFile)
if err != nil {
log.Println(err)
log.WithError(err).Fatal("failed to read config file")
return
}
// Init telegram
err = telegram.Init(conf.Name, conf.Telegram.Id, conf.Telegram.Channel)
if err != nil {
log.Println("Failed to initialize telegram", err)
log.WithError(err).Fatal("Failed to initialize telegram")
return
}
// Connect to redis
err = redis.Connect(conf.Redis.Addr, conf.Redis.Password, conf.Redis.DB)
if err != nil {
log.Println("Failed to connect to redis:", err)
log.WithError(err).Fatal("Failed to connect to redis")
return
}
@ -196,7 +207,7 @@ func main() {
eosClient = eos.New(conf.Api)
chainInfo, err = eosClient.GetInfo(eosClientCtx)
if err != nil {
log.Println("Failed to get info:", err)
log.WithError(err).Fatal("Failed to get info")
return
}

View file

@ -2,7 +2,8 @@ package main
import (
"encoding/json"
"log"
log "github.com/sirupsen/logrus"
"eosio-ship-trace-reader/redis"
"github.com/eoscanada/eos-go/ship"
@ -10,7 +11,7 @@ import (
func processBlock(block *ship.GetBlocksResultV0) {
if block.ThisBlock.BlockNum%100 == 0 {
log.Printf("Current: %d, Head: %d\n", block.ThisBlock.BlockNum, block.Head.BlockNum)
log.Infof("Current: %d, Head: %d\n", block.ThisBlock.BlockNum, block.Head.BlockNum)
}
}
@ -21,10 +22,10 @@ func processTraces(traces []*ship.TransactionTraceV0) {
if err == nil {
channel := redis.Key("transactions")
if err := redis.Publish(channel, payload).Err(); err != nil {
log.Printf("Failed to post to channel '%s': %s", channel, err)
log.WithError(err).Errorf("Failed to post to channel '%s'", channel)
}
} else {
log.Println("Failed to encode transaction:", err)
log.WithError(err).Warn("Failed to encode transaction")
}
// Actions
@ -41,16 +42,16 @@ func processTraces(traces []*ship.TransactionTraceV0) {
if err == nil {
v, err := DecodeAction(abi, trace.Act.Data, trace.Act.Name)
if err != nil {
log.Print(err)
log.WithError(err).Warn("Failed to decode action")
}
act.Data = v
} else {
log.Printf("Failed to get abi for contract %s: %s\n", trace.Act.Account, err)
log.WithError(err).Errorf("Failed to get abi for contract %s", trace.Act.Account)
}
payload, err := json.Marshal(act)
if err != nil {
log.Println("Failed to encode action:", err)
log.WithError(err).Error("Failed to encode action")
continue
}
@ -62,7 +63,7 @@ func processTraces(traces []*ship.TransactionTraceV0) {
for _, channel := range channels {
if err := redis.RegisterPublish(channel, payload).Err(); err != nil {
log.Printf("Failed to post to channel '%s': %s", channel, err)
log.WithError(err).Errorf("Failed to post to channel '%s'", channel)
}
}
}
@ -70,6 +71,6 @@ func processTraces(traces []*ship.TransactionTraceV0) {
_, err := redis.Send()
if err != nil {
log.Println("Failed to send redis. command:", err)
log.WithError(err).Error("Failed to send redis")
}
}