Tweak verbose HTTP logging

- log headers only in DEBUG=api mode
- enable color output on stderr
- hide little-useful TLS debbuging info
- ensure all request headers are logged
This commit is contained in:
Mislav Marohnić 2020-02-21 12:57:00 +01:00
parent 9c2efd6c1c
commit 9c00ac0224
2 changed files with 25 additions and 15 deletions

View file

@ -38,12 +38,15 @@ func AddHeader(name, value string) ClientOption {
}
// VerboseLog enables request/response logging within a RoundTripper
func VerboseLog(out io.Writer, logBodies bool) ClientOption {
func VerboseLog(out io.Writer, logTraffic bool, colorize bool) ClientOption {
logger := &httpretty.Logger{
RequestHeader: true,
RequestBody: logBodies,
ResponseHeader: true,
ResponseBody: logBodies,
Time: true,
TLS: false,
Colors: colorize,
RequestHeader: logTraffic,
RequestBody: logTraffic,
ResponseHeader: logTraffic,
ResponseBody: logTraffic,
Formatters: []httpretty.Formatter{&httpretty.JSONFormatter{}},
}
logger.SetOutput(out)

View file

@ -97,15 +97,14 @@ var initContext = func() context.Context {
// BasicClient returns an API client that borrows from but does not depend on
// user configuration
func BasicClient() (*api.Client, error) {
opts := []api.ClientOption{
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
opts := []api.ClientOption{}
if verbose := os.Getenv("DEBUG"); verbose != "" {
opts = append(opts, apiVerboseLog())
}
opts = append(opts, api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)))
if c, err := context.ParseDefaultConfig(); err == nil {
opts = append(opts, api.AddHeader("Authorization", fmt.Sprintf("token %s", c.Token)))
}
if verbose := os.Getenv("DEBUG"); verbose != "" {
opts = append(opts, api.VerboseLog(os.Stderr, false))
}
return api.NewClient(opts...), nil
}
@ -123,20 +122,28 @@ var apiClientForContext = func(ctx context.Context) (*api.Client, error) {
if err != nil {
return nil, err
}
opts := []api.ClientOption{
opts := []api.ClientOption{}
if verbose := os.Getenv("DEBUG"); verbose != "" {
opts = append(opts, apiVerboseLog())
}
opts = append(opts,
api.AddHeader("Authorization", fmt.Sprintf("token %s", token)),
api.AddHeader("User-Agent", fmt.Sprintf("GitHub CLI %s", Version)),
// antiope-preview: Checks
// shadow-cat-preview: Draft pull requests
api.AddHeader("Accept", "application/vnd.github.antiope-preview+json, application/vnd.github.shadow-cat-preview"),
api.AddHeader("GraphQL-Features", "pe_mobile"),
}
if verbose := os.Getenv("DEBUG"); verbose != "" {
opts = append(opts, api.VerboseLog(os.Stderr, strings.Contains(verbose, "api")))
}
)
return api.NewClient(opts...), nil
}
func apiVerboseLog() api.ClientOption {
logTraffic := strings.Contains(os.Getenv("DEBUG"), "api")
colorize := utils.IsTerminal(os.Stderr)
return api.VerboseLog(utils.NewColorable(os.Stderr), logTraffic, colorize)
}
func colorableOut(cmd *cobra.Command) io.Writer {
out := cmd.OutOrStdout()
if outFile, isFile := out.(*os.File); isFile {