Skip to content

Commit

Permalink
ref: add log formating to grpc cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
kenriortega committed Nov 11, 2021
1 parent 5d081e2 commit 7fa706d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
15 changes: 8 additions & 7 deletions cmd/cli/grpcxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package cli
import (
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"time"

"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
"github.com/spf13/cobra"
"github.com/talos-systems/grpc-proxy/proxy"
Expand All @@ -29,7 +29,7 @@ var grpcCmd = &cobra.Command{

lis, err := net.Listen("tcp", configFromYaml.GrpcProxy.Listener)
if err != nil {
log.Fatalf("failed to listen: %v", err)
logger.LogError(errors.Errorf("grpc: failed to listen: %v", err).Error())
}

logger.LogInfo(fmt.Sprintf("Proxy running at %q\n", configFromYaml.GrpcProxy.Listener))
Expand All @@ -43,7 +43,7 @@ var grpcCmd = &cobra.Command{
creds, sslErr := credentials.NewClientTLSFromFile(
configFromYaml.GrpcClientCert, "")
if sslErr != nil {
log.Fatalf("Failed to parse credentials: %v", sslErr)
logger.LogError(errors.Errorf("grpc: failed to parse credentials: %v", sslErr).Error())
}
conn, err := grpc.DialContext(
ctx,
Expand Down Expand Up @@ -94,7 +94,7 @@ var grpcCmd = &cobra.Command{
configFromYaml.GrpcSSL.KeyFile,
)
if sslErr != nil {
log.Fatalf("Failed to parse credentials: %v", sslErr)
logger.LogError(errors.Errorf("grpc: failed to parse credentials: %v", sslErr).Error())
return
}
opts = append(opts, grpc.Creds(creds))
Expand All @@ -104,7 +104,7 @@ var grpcCmd = &cobra.Command{

go gracefulShutdown(server)
if err := server.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
logger.LogError(errors.Errorf("grpc: failed to serve: %v", err).Error())
}
},
}
Expand All @@ -119,11 +119,12 @@ func gracefulShutdown(server *grpc.Server) {

signal.Notify(quit, os.Interrupt)
sig := <-quit
logger.LogInfo(fmt.Sprintf("server is shutting down %s", sig.String()))
logger.LogWarn(fmt.Sprintf("grpc: server is shutting down %s", sig.String()))

_, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

server.GracefulStop()
logger.LogInfo("server stopped")
logger.LogWarn(fmt.Sprintf("grpc: server is shutting down %s", sig.String()))

}
5 changes: 1 addition & 4 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@ import (
var log *zap.Logger

func init() {
// var err error
// config := zap.NewProductionConfig()

encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "timestamp"
encoderConfig.StacktraceKey = ""
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder

// lumberjack.Logger is already safe for concurrent use, so we don't need to
// lock it.
w := zapcore.AddSync(&lumberjack.Logger{
Filename: "./ngonx-log/ngonx.log",
MaxSize: 500, // megabytes
Expand Down

0 comments on commit 7fa706d

Please sign in to comment.