Skip to content

Commit

Permalink
Add options for customizing threadiness, logger encoding, and global …
Browse files Browse the repository at this point in the history
…logger level
  • Loading branch information
gmemcc committed Mar 5, 2019
1 parent 296015f commit 104e8ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion cmd/flagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
slackURL string
slackUser string
slackChannel string
threadiness int
)

func init() {
Expand All @@ -43,6 +44,7 @@ func init() {
flag.StringVar(&slackURL, "slack-url", "", "Slack hook URL.")
flag.StringVar(&slackUser, "slack-user", "flagger", "Slack user name.")
flag.StringVar(&slackChannel, "slack-channel", "", "Slack channel.")
flag.IntVar(&threadiness, "threadiness", 2, "Worker concurrency.")
}

func main() {
Expand All @@ -52,6 +54,7 @@ func main() {
if err != nil {
log.Fatalf("Error creating logger: %v", err)
}
logging.ReplaceGlobalIf(logger.Desugar())
defer logger.Sync()

stopCh := signals.SetupSignalHandler()
Expand Down Expand Up @@ -132,7 +135,7 @@ func main() {

// start controller
go func(ctrl *controller.Controller) {
if err := ctrl.Run(2, stopCh); err != nil {
if err := ctrl.Run(threadiness, stopCh); err != nil {
logger.Fatalf("Error running controller: %v", err)
}
}(c)
Expand Down
20 changes: 19 additions & 1 deletion pkg/logging/logger.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
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) {
level := zap.NewAtomicLevelAt(zapcore.InfoLevel)
Expand Down Expand Up @@ -47,7 +59,7 @@ func NewLogger(logLevel string) (*zap.SugaredLogger, error) {
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
Encoding: encoding,
EncoderConfig: zapEncoderConfig,
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
Expand All @@ -60,6 +72,12 @@ 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 104e8ef

Please sign in to comment.