Skip to content

Commit

Permalink
implement quiet mode (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoyamachi authored Sep 9, 2021
1 parent d619c56 commit deee6fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cmd/dockle/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ OPTIONS:
Name: "debug, d",
Usage: "debug mode",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "suppress log output",
},

// Registry flag
cli.DurationFlag{
Expand Down
23 changes: 16 additions & 7 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,43 @@ package log

import (
"fmt"

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

var (
Logger *zap.SugaredLogger
debugOption bool
)

func InitLogger(debug bool) (err error) {
func InitLogger(debug, quiet bool) (err error) {
debugOption = debug
Logger, err = newLogger(debug)
Logger, err = newLogger(debug, quiet)
if err != nil {
return fmt.Errorf("error in new logger: %w", err)
}
return nil

}

func newLogger(debug bool) (*zap.SugaredLogger, error) {
func newLogger(debug, quiet bool) (*zap.SugaredLogger, error) {
level := zap.NewAtomicLevel()
if debug {
level.SetLevel(zapcore.DebugLevel)
} else {
level.SetLevel(zapcore.InfoLevel)
}

stdout := "stdout"
stderr := "stderr"
if quiet {
if _, err := os.Create(os.DevNull); err != nil {
return nil, err
}
stdout = os.DevNull
stderr = os.DevNull
}

myConfig := zap.Config{
Level: level,
Encoding: "console",
Expand All @@ -48,8 +57,8 @@ func newLogger(debug bool) (*zap.SugaredLogger, error) {
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
OutputPaths: []string{stdout},
ErrorOutputPaths: []string{stderr},
}
logger, err := myConfig.Build()
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func Run(c *cli.Context) (err error) {
ctx, cancel := context.WithTimeout(context.Background(), c.Duration("timeout"))
defer cancel()
debug := c.Bool("debug")
if err = log.InitLogger(debug); err != nil {
quiet := c.Bool("quiet")
if err = log.InitLogger(debug, quiet); err != nil {
l.Fatal(err)
}

Expand Down

0 comments on commit deee6fe

Please sign in to comment.