Skip to content

Commit

Permalink
update build flags and logger method to init
Browse files Browse the repository at this point in the history
Signed-off-by: Jalander Ramagiri <[email protected]>
  • Loading branch information
Jalander Ramagiri committed May 20, 2024
1 parent b8eae45 commit d4e78ea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
50 changes: 25 additions & 25 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOFMT=$(GOCMD) fmt
GOVET=$(GOCMD) vet
GOTEST=$(GOCMD) test
BINARY_NAME=gerrit-translator-cdevents
PKG_DIR=./pkg/

all: fmt vet test build

build:
$(GOBUILD) -o $(BINARY_NAME) $(PKG_DIR)

test:
$(GOTEST) -v ./...

fmt:
$(GOFMT) ./...

vet:
$(GOVET) ./...

clean:
rm -f $(BINARY_NAME)
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOFMT=$(GOCMD) fmt
GOVET=$(GOCMD) vet
GOTEST=$(GOCMD) test
BINARY_NAME=gerrit-translator-cdevents
PKG_DIR=./pkg/

all: fmt vet test build

build:
CGO_ENABLED=0 GOOS=linux $(GOBUILD) -o $(BINARY_NAME) $(PKG_DIR)

test:
$(GOTEST) -v ./...

fmt:
$(GOFMT) ./...

vet:
$(GOVET) ./...

clean:
rm -f $(BINARY_NAME)
17 changes: 14 additions & 3 deletions pkg/gerrit/logger.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
package gerrit

import (
"errors"
"syscall"

"go.uber.org/zap"
)

var sugar *zap.SugaredLogger

// InitLogger Initialize a new production zap logger
func InitLogger() {
// init Initialize a new production zap logger
func init() {
logger, err := zap.NewProduction()
if err != nil {
panic("failed to initialize logger: " + err.Error())
}
defer func(logger *zap.Logger) {
err := logger.Sync()
err := Sync(sugar)
if err != nil {
panic("failed to initialize logger Sync: " + err.Error())
}
}(logger)
sugar = logger.Sugar()
}

func Sync(logger *zap.SugaredLogger) error {
err := logger.Sync()
if !errors.Is(err, syscall.EINVAL) {
return err
}
return nil
}

// Log returns the zap logger If initialized
func Log() *zap.SugaredLogger {
if sugar == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
"net/http"

"github.com/cdevents/gerrit-translator/pkg/gerrit"
"github.com/cdevents/webhook-adapter/pkg/cdevents"
"github.com/hashicorp/go-plugin"
"net/http"
)

type EventTranslator struct{}
Expand All @@ -37,7 +38,6 @@ func (EventTranslator) TranslateEvent(event string, headers http.Header) (string
}

func main() {
gerrit.InitLogger()
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: cdevents.Handshake,
Plugins: map[string]plugin.Plugin{
Expand Down

0 comments on commit d4e78ea

Please sign in to comment.