Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement quiet mode #142

Merged
merged 1 commit into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
implement quiet mode
  • Loading branch information
tomoyamachi committed Sep 9, 2021
commit 199a820ea505680369a812357dd1548c523eaea2
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