Setup initializes profiling and logging based on the CLI flags. It should be called as early as possible in the program.
(ctx *cli.Context)
| 92 | // Setup initializes profiling and logging based on the CLI flags. |
| 93 | // It should be called as early as possible in the program. |
| 94 | func Setup(ctx *cli.Context) error { |
| 95 | output := io.Writer(os.Stderr) |
| 96 | if ctx.Bool(logjsonFlag.Name) { |
| 97 | glogger = log.NewGlogHandler(log.JSONHandler(output)) |
| 98 | } else { |
| 99 | usecolor := (isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())) && os.Getenv("TERM") != "dumb" |
| 100 | if usecolor { |
| 101 | output = colorable.NewColorableStderr() |
| 102 | } |
| 103 | glogger = log.NewGlogHandler(log.NewTerminalHandler(output, usecolor)) |
| 104 | } |
| 105 | |
| 106 | // logging |
| 107 | verbosity := ctx.Int(verbosityFlag.Name) |
| 108 | glogger.Verbosity(log.FromLegacyLevel(verbosity)) |
| 109 | vmodule := ctx.String(vmoduleFlag.Name) |
| 110 | glogger.Vmodule(vmodule) |
| 111 | |
| 112 | log.SetDefault(log.NewLogger(glogger)) |
| 113 | |
| 114 | // profiling, tracing |
| 115 | runtime.MemProfileRate = memprofilerateFlag.Value |
| 116 | if ctx.IsSet(memprofilerateFlag.Name) { |
| 117 | runtime.MemProfileRate = ctx.Int(memprofilerateFlag.Name) |
| 118 | } |
| 119 | |
| 120 | // pprof server |
| 121 | if ctx.Bool(pprofFlag.Name) { |
| 122 | listenHost := ctx.String(pprofAddrFlag.Name) |
| 123 | |
| 124 | port := ctx.Int(pprofPortFlag.Name) |
| 125 | |
| 126 | address := fmt.Sprintf("%s:%d", listenHost, port) |
| 127 | // This context value ("metrics.addr") represents the utils.MetricsHTTPFlag.Name. |
| 128 | // It cannot be imported because it will cause a cyclical dependency. |
| 129 | StartPProf(address, !ctx.IsSet("metrics.addr")) |
| 130 | } |
| 131 | return nil |
| 132 | } |
| 133 | |
| 134 | func StartPProf(address string, withMetrics bool) { |
| 135 | // Hook go-metrics into expvar on any /debug/metrics request, load all vars |
no test coverage detected