Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Zap logger #28

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
.idea
/coverage.txt
/shellcheck
temp
*.log
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 (
Expand All @@ -30,7 +32,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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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=
Expand Down
5 changes: 4 additions & 1 deletion internal/webservice/webservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion internal/ykclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}()
}
Expand Down
56 changes: 56 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -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 = "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)
})
}