Allow debug level logs at startup.

Added a -debug flag which causes the logger in main to start with debug
level logs.
This commit is contained in:
Victor Vrantchan
2020-07-22 22:49:23 -04:00
parent 68d5c88941
commit a9900916bf
3 changed files with 25 additions and 4 deletions

View File

@@ -31,6 +31,7 @@ func writePID(path string) error {
}
type cliFlags struct {
debug bool
siteName string
http string
pidfile string
@@ -38,14 +39,15 @@ type cliFlags struct {
func micromdm(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
var (
logger log.Logger
ctx = context.Background()
logger = log.New(log.Output(stderr))
cli = &cliFlags{}
rootfs = flag.NewFlagSet("micromdm", flag.ContinueOnError)
_ = rootfs.String("config", "", "Path to config file (optional)")
)
rootfs.StringVar(&cli.pidfile, "pidfile", "/tmp/micromdm.pid", "Path to server pidfile")
rootfs.BoolVar(&cli.debug, "debug", false, "Allow debug level")
rootfs.StringVar(&cli.siteName, "site_name", "Acme", "Name of the site as it would appear in the top left of the HTML UI")
rootfs.StringVar(&cli.http, "http", "localhost:9000", "HTTP service address")
@@ -80,6 +82,14 @@ func micromdm(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
Options: []ff.Option{ff.WithEnvVarPrefix("MICROMDM"), ff.WithConfigFileParser(ff.PlainParser), ff.WithConfigFileFlag("config")},
Subcommands: []*ffcli.Command{helpCmd, version},
Exec: func(context.Context, []string) error {
logOpts := []log.Option{log.Output(stderr)}
if cli.debug {
logOpts = append(logOpts, log.StartDebug())
}
logger = log.New(logOpts...)
if err := writePID(cli.pidfile); err != nil {
return err
}

View File

@@ -44,7 +44,7 @@ func checkExitAfterSignal(t *testing.T, stdin, stdout, stderr *bytes.Buffer) {
func checkLogSwap(t *testing.T, stdin, stdout, stderr *bytes.Buffer) {
output := stderr.String()
debugsub := `level=debug msg="swapping level" debug=true`
debugsub := `level=info msg="swapping level" debug=true`
if !strings.Contains(output, debugsub) {
t.Errorf("want %q in output, got:\n%s", debugsub, output)
}

View File

@@ -67,6 +67,13 @@ func JSON() Option {
}
}
// StartDebug creates a logger configured to allow debug level logs from the start.
func StartDebug() Option {
return func(c *config) {
c.debug = true
}
}
// Output configures the log output. Stderr is default.
func Output(w io.Writer) Option {
return func(c *config) {
@@ -78,6 +85,7 @@ type config struct {
w io.Writer
format func(io.Writer) log.Logger
sig os.Signal
debug bool
}
// New creates a Logger.
@@ -96,11 +104,14 @@ func New(opts ...Option) *log.SwapLogger {
base = log.With(base, "ts", log.DefaultTimestampUTC)
base = level.NewInjector(base, level.InfoValue())
lev := level.AllowInfo()
if c.debug {
lev = level.AllowDebug()
}
var swapLogger log.SwapLogger
swapLogger.Swap(level.NewFilter(base, lev))
go c.swapLevelHandler(base, &swapLogger, false)
go c.swapLevelHandler(base, &swapLogger, c.debug)
return &swapLogger
}
@@ -116,7 +127,7 @@ func (c *config) swapLevelHandler(base Logger, swapLogger *log.SwapLogger, debug
newLogger := level.NewFilter(base, level.AllowDebug())
swapLogger.Swap(newLogger)
}
Debug(swapLogger).Log("msg", "swapping level", "debug", !debug)
Info(swapLogger).Log("msg", "swapping level", "debug", !debug)
debug = !debug
}
}