Multi-level logging (#185)

Closes #93

Summary of changes:

- Adds the [uber-go-/zap](https://github.com/uber-go/zap) library for leveled logging (69040996e0bcbd2f211d0d8d576147027e928e3b)
- A new CLI arg `--log-level` which can take the values `debug` `info` `warn` `error` `fatal` `panic`. The default is `info`, and the existing `--quiet` flag is treated as `--log-level warn`
- All `helm.exec` calls are preceded by a one-line summary. The current information on the full helm command line arguments `exec: helm exec ... ` is only output at `debug` level. This means sensitive command line arguments which may include passwords and `--set` arguments should be hidden by default.
This commit is contained in:
Simon Li
2018-07-19 11:33:57 +09:00
committed by KUOKA Yusuke
parent 35732f3a93
commit 06a1d245ae
75 changed files with 7929 additions and 63 deletions
+35 -10
View File
@@ -2,18 +2,20 @@ package main
import (
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"syscall"
"path/filepath"
"sort"
"github.com/roboll/helmfile/helmexec"
"github.com/roboll/helmfile/state"
"github.com/urfave/cli"
"path/filepath"
"sort"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
@@ -24,6 +26,28 @@ const (
var Version string
func configure_logging(c *cli.Context) error {
// Valid levels:
// https://github.com/uber-go/zap/blob/7e7e266a8dbce911a49554b945538c5b950196b8/zapcore/level.go#L126
logLevel := c.GlobalString("log-level")
if c.GlobalBool("quiet") {
logLevel = "warn"
}
var level zapcore.Level
err := level.Set(logLevel)
if err != nil {
return err
}
logger := helmexec.NewLogger(os.Stdout, logLevel)
if c.App.Metadata == nil {
// Auto-initialised in 1.19.0
// https://github.com/urfave/cli/blob/master/CHANGELOG.md#1190---2016-11-19
c.App.Metadata = make(map[string]interface{})
}
c.App.Metadata["logger"] = logger
return nil
}
func main() {
app := cli.NewApp()
@@ -42,12 +66,16 @@ func main() {
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "silence output",
Usage: "Silence output. Equivalent to log-level warn",
},
cli.StringFlag{
Name: "kube-context",
Usage: "Set kubectl context. Uses current context by default",
},
cli.StringFlag{
Name: "log-level",
Usage: "Set log level, default info",
},
cli.StringFlag{
Name: "namespace, n",
Usage: "Set namespace. Uses the namespace set in the context by default",
@@ -61,6 +89,7 @@ func main() {
},
}
app.Before = configure_logging
app.Commands = []cli.Command{
{
Name: "repos",
@@ -422,7 +451,6 @@ func directoryExistsAt(path string) bool {
}
func loadDesiredStateFromFile(c *cli.Context, file string) (*state.HelmState, helmexec.Interface, error) {
quiet := c.GlobalBool("quiet")
kubeContext := c.GlobalString("kube-context")
namespace := c.GlobalString("namespace")
labels := c.GlobalStringSlice("selector")
@@ -455,10 +483,6 @@ func loadDesiredStateFromFile(c *cli.Context, file string) (*state.HelmState, he
os.Exit(1)
}
}
var writer io.Writer
if !quiet {
writer = os.Stdout
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
@@ -469,7 +493,8 @@ func loadDesiredStateFromFile(c *cli.Context, file string) (*state.HelmState, he
clean(st, errs)
}()
return st, helmexec.New(writer, kubeContext), nil
logger := c.App.Metadata["logger"].(*zap.SugaredLogger)
return st, helmexec.New(logger, kubeContext), nil
}
func clean(state *state.HelmState, errs []error) error {