Skip to content

Commit

Permalink
fix: add log rolling (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
viveksharmapoudel authored Jul 19, 2023
1 parent 0750a04 commit 895cc72
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
30 changes: 25 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import (
"errors"
"fmt"
"io"
"net/url"
"os"
"os/signal"
"path"
"path/filepath"
"runtime/debug"
"strings"
Expand All @@ -34,6 +36,7 @@ import (
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)

const appName = "rly"
Expand Down Expand Up @@ -171,13 +174,32 @@ func Execute() {
}
}

type lumberjackSink struct {
*lumberjack.Logger
}

func (lumberjackSink) Sync() error { return nil }

func newRootLogger(format string, debug bool) (*zap.Logger, error) {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.UTC().Format("2006-01-02T15:04:05.000000Z07:00"))
}
config.LevelKey = "lvl"

ll := lumberjack.Logger{
Filename: path.Join(defaultHome, "relay.log"),
MaxSize: 50, //MB
MaxBackups: 30,
MaxAge: 28, //days
Compress: true,
}
zap.RegisterSink("lumberjack", func(*url.URL) (zap.Sink, error) {
return lumberjackSink{
Logger: &ll,
}, nil
})

var enc zapcore.Encoder
switch format {
case "json":
Expand All @@ -195,15 +217,13 @@ func newRootLogger(format string, debug bool) (*zap.Logger, error) {
level = zap.DebugLevel
}

logFile, _ := os.OpenFile("log.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
writer := zapcore.AddSync(logFile)
w := zapcore.AddSync(&ll)

core := zapcore.NewTee(
zapcore.NewCore(enc,
writer,
level),
zapcore.NewCore(enc, w, level),
zapcore.NewCore(enc, os.Stderr, level),
)

return zap.New(core), nil
}

Expand Down
5 changes: 3 additions & 2 deletions relayer/chains/archway/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,9 @@ func (ap *ArchwayProvider) SendMessagesToMempool(
ap.updateNextAccountSequence(sequence + 1)
}

//TODO: comment this on production
SaveMsgToFile(ArchwayDebugMessagePath, msgs)
//uncomment for saving msg
// SaveMsgToFile(ArchwayDebugMessagePath, msgs)

return nil

}
Expand Down

0 comments on commit 895cc72

Please sign in to comment.