Skip to content

Commit

Permalink
move flags to main packages
Browse files Browse the repository at this point in the history
  • Loading branch information
gmemcc committed Mar 5, 2019
1 parent 104e8ef commit 231a323
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
12 changes: 10 additions & 2 deletions cmd/flagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"go.uber.org/zap"
"log"
"time"

Expand Down Expand Up @@ -32,6 +33,8 @@ var (
slackUser string
slackChannel string
threadiness int
zapReplaceGlobals bool
zapEncoding string
)

func init() {
Expand All @@ -45,16 +48,21 @@ func init() {
flag.StringVar(&slackUser, "slack-user", "flagger", "Slack user name.")
flag.StringVar(&slackChannel, "slack-channel", "", "Slack channel.")
flag.IntVar(&threadiness, "threadiness", 2, "Worker concurrency.")
flag.BoolVar(&zapReplaceGlobals, "zap-replace-globals", false, "Whether to change the logging level of the global zap logger.")
flag.StringVar(&zapEncoding, "zap-encoding", "json", "Zap logger encoding.")
}

func main() {
flag.Parse()

logger, err := logging.NewLogger(logLevel)
logger, err := logging.NewLoggerWithEncoding(logLevel, zapEncoding)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
logging.ReplaceGlobalIf(logger.Desugar())
if zapReplaceGlobals {
zap.ReplaceGlobals(logger.Desugar())
}

defer logger.Sync()

stopCh := signals.SetupSignalHandler()
Expand Down
19 changes: 14 additions & 5 deletions cmd/loadtester/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,41 @@ import (
"github.com/knative/pkg/signals"
"github.com/stefanprodan/flagger/pkg/loadtester"
"github.com/stefanprodan/flagger/pkg/logging"
"go.uber.org/zap"
"log"
"time"
)

var VERSION = "0.1.0"
var (
logLevel string
port string
timeout time.Duration
logCmdOutput bool
logLevel string
port string
timeout time.Duration
logCmdOutput bool
zapReplaceGlobals bool
zapEncoding string
)

func init() {
flag.StringVar(&logLevel, "log-level", "debug", "Log level can be: debug, info, warning, error.")
flag.StringVar(&port, "port", "9090", "Port to listen on.")
flag.DurationVar(&timeout, "timeout", time.Hour, "Command exec timeout.")
flag.BoolVar(&logCmdOutput, "log-cmd-output", true, "Log command output to stderr")
flag.BoolVar(&zapReplaceGlobals, "zap-replace-globals", false, "Whether to change the logging level of the global zap logger.")
flag.StringVar(&zapEncoding, "zap-encoding", "json", "Zap logger encoding.")
}

func main() {
flag.Parse()

logger, err := logging.NewLogger(logLevel)
logger, err := logging.NewLoggerWithEncoding(logLevel, zapEncoding)
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
if zapReplaceGlobals {
zap.ReplaceGlobals(logger.Desugar())
}

defer logger.Sync()

stopCh := signals.SetupSignalHandler()
Expand Down
25 changes: 6 additions & 19 deletions pkg/logging/logger.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package logging

import (
"flag"
"fmt"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
replaceGlobals bool
encoding string
)

func init() {
flag.BoolVar(&replaceGlobals, "zap-replace-globals", false, "Whether to change level of global zap logger.")
flag.StringVar(&encoding, "zap-encoding", "json", "Zap logger encoding.")

}

// NewLogger returns a zap sugared logger configured with json format and caller id
func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
return NewLoggerWithEncoding(logLevel, "json")
}

// NewLoggerWithEncoding returns a zap sugared logger configured with provided format, e.g. console or json, and caller id
func NewLoggerWithEncoding(logLevel, zapEncoding string) (*zap.SugaredLogger, error) {
level := zap.NewAtomicLevelAt(zapcore.InfoLevel)
switch logLevel {
case "debug":
Expand Down Expand Up @@ -59,7 +52,7 @@ func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
Initial: 100,
Thereafter: 100,
},
Encoding: encoding,
Encoding: zapEncoding,
EncoderConfig: zapEncoderConfig,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
Expand All @@ -72,12 +65,6 @@ func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
return logger.Sugar(), nil
}

func ReplaceGlobalIf(logger *zap.Logger) {
if replaceGlobals {
zap.ReplaceGlobals(logger)
}
}

// Console writes to stdout if the console env var exists
func Console(a ...interface{}) (n int, err error) {
if os.Getenv("console") != "" {
Expand Down

0 comments on commit 231a323

Please sign in to comment.