From 713f1effcee15c07719b62f326b3ff4bd2b55ad3 Mon Sep 17 00:00:00 2001 From: sudipto baral Date: Mon, 10 Jun 2024 11:50:23 -0400 Subject: [PATCH] initial zap logger configuration. --- .gitignore | 1 + go.mod | 3 +- go.sum | 2 ++ internal/webservice/webservice.go | 5 ++- internal/ykclient/client.go | 4 ++- log/logger.go | 56 +++++++++++++++++++++++++++++++ 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 log/logger.go diff --git a/.gitignore b/.gitignore index 936202c6..951141aa 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ .idea /coverage.txt /shellcheck +temp diff --git a/go.mod b/go.mod index 9b55ad83..8a1d20a1 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,8 @@ require ( github.com/knadh/koanf/v2 v2.1.1 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.8.1 + go.uber.org/zap v1.26.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( @@ -29,7 +31,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect go.uber.org/multierr v1.10.0 // indirect - go.uber.org/zap v1.26.0 // indirect golang.org/x/crypto v0.19.0 // indirect golang.org/x/net v0.21.0 // indirect golang.org/x/sync v0.3.0 // indirect diff --git a/go.sum b/go.sum index 6d3a4d46..5cefcc19 100644 --- a/go.sum +++ b/go.sum @@ -89,6 +89,8 @@ google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHh gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/webservice/webservice.go b/internal/webservice/webservice.go index 28c19149..d42f1f51 100644 --- a/internal/webservice/webservice.go +++ b/internal/webservice/webservice.go @@ -4,10 +4,13 @@ import ( "context" "encoding/json" "fmt" + "net/http" "os" "time" + "github.com/G-Research/yunikorn-history-server/log" + "github.com/G-Research/yunikorn-history-server/internal/config" "github.com/G-Research/yunikorn-history-server/internal/repository" @@ -65,7 +68,7 @@ func (ws *WebService) Start(ctx context.Context) { }) ws.server.Handler = router go func() { - fmt.Printf("Starting webservice on %s\n", ws.server.Addr) + log.Logger.Info(fmt.Sprintf("Starting webservice on %s", ws.server.Addr)) err := ws.server.ListenAndServe() if err != nil && err != http.ErrServerClosed { fmt.Fprintf(os.Stderr, "HTTP serving error: %v\n", err) diff --git a/internal/ykclient/client.go b/internal/ykclient/client.go index 2102440d..7ff0a00f 100644 --- a/internal/ykclient/client.go +++ b/internal/ykclient/client.go @@ -9,6 +9,8 @@ import ( "net/http" "os" + "github.com/G-Research/yunikorn-history-server/log" + "github.com/google/uuid" "github.com/G-Research/yunikorn-history-server/internal/config" @@ -47,7 +49,7 @@ func (c *Client) Run(ctx context.Context) { } go func() { - fmt.Println("Starting YuniKorn event stream client") + log.Logger.Info("Starting YuniKorn event stream client") c.FetchEventStream(ctx, streamURL, evCounts) }() } diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 00000000..869b9290 --- /dev/null +++ b/log/logger.go @@ -0,0 +1,56 @@ +package log + +import ( + "os" + "sync" + + "gopkg.in/natefinch/lumberjack.v2" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +// TODO: implement a mechanism to load this values from configuration +const ( + logFilePath = "temp/logs/yhs.log" + maxSize = 5 + maxBackups = 10 + maxAge = 14 + compress = true + logLevel = zap.InfoLevel +) + +var ( + once sync.Once + Logger *zap.Logger +) + +func init() { + once.Do(func() { + stdout := zapcore.AddSync(os.Stdout) + file := zapcore.AddSync(&lumberjack.Logger{ + Filename: logFilePath, + MaxSize: maxSize, + MaxBackups: maxBackups, + MaxAge: maxAge, + Compress: compress, + }) + + productionCfg := zap.NewProductionEncoderConfig() + productionCfg.TimeKey = "timestamp" + productionCfg.EncodeTime = zapcore.ISO8601TimeEncoder + + developmentCfg := zap.NewDevelopmentEncoderConfig() + developmentCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder + + consoleEncoder := zapcore.NewConsoleEncoder(developmentCfg) + fileEncoder := zapcore.NewJSONEncoder(productionCfg) + + core := zapcore.NewTee( + zapcore.NewCore(consoleEncoder, stdout, logLevel), + zapcore.NewCore(fileEncoder, file, logLevel), + ) + + Logger = zap.New(core) + }) +}