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

Add a custom log formatter #1266

Merged
merged 4 commits into from
Jul 10, 2018
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
5 changes: 3 additions & 2 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/dcos/dcos-cli/pkg/config"
"github.com/dcos/dcos-cli/pkg/httpclient"
"github.com/dcos/dcos-cli/pkg/log"
"github.com/dcos/dcos-cli/pkg/login"
"github.com/dcos/dcos-cli/pkg/open"
"github.com/dcos/dcos-cli/pkg/plugin"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (ctx *Context) Logger() *logrus.Logger {
if ctx.logger == nil {
ctx.logger = &logrus.Logger{
Out: ctx.env.ErrOut,
Formatter: new(logrus.TextFormatter),
Formatter: &log.Formatter{},
Hooks: make(logrus.LevelHooks),
}
}
Expand Down Expand Up @@ -172,8 +173,8 @@ func (ctx *Context) Login(flags *login.Flags, httpClient *httpclient.Client) (st

// Setup configures a given cluster based on its URL and setup flags.
func (ctx *Context) Setup(flags *setup.Flags, clusterURL string) (*config.Cluster, error) {
// This supports passing a cluster URL without scheme, it then defaults to HTTPS.
if !strings.HasPrefix(clusterURL, "https://") && !strings.HasPrefix(clusterURL, "http://") {
ctx.Logger().Info("Missing scheme in cluster URL, assuming HTTPS.")
clusterURL = "https://" + clusterURL
}

Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/dcos.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func NewDCOSCommand(ctx api.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "dcos",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
cmd.SilenceUsage = true
if verbose == 1 {
// -v sets the logger level to info.
ctx.Logger().SetLevel(logrus.InfoLevel)
Expand Down
24 changes: 13 additions & 11 deletions pkg/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,25 +176,27 @@ func (c *Client) NewRequest(method, path string, body io.Reader, opts ...Option)
func (c *Client) Do(req *http.Request) (*http.Response, error) {
logger := c.opts.Logger

if logger != nil {
dumpBody := (logger.Level >= logrus.DebugLevel)
reqDump, err := httputil.DumpRequestOut(req, dumpBody)
if logger != nil && logger.Level >= logrus.DebugLevel {
reqDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
logger.Warnf("Couldn't dump request: %s", err)
logger.Debug("Couldn't dump request: %s", err)
} else {
logger.Infof("Outgoing request:\n%s", reqDump)
logger.Debug(string(reqDump))
}
}

resp, err := c.baseClient.Do(req)

if err == nil && logger != nil {
dumpBody := (logger.Level >= logrus.DebugLevel)
respDump, err := httputil.DumpResponse(resp, dumpBody)
if err != nil {
logger.Warnf("Couldn't dump response: %s", err)
if logger != nil && logger.Level >= logrus.DebugLevel {
if err == nil {
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
logger.Debug("Couldn't dump response: %s", err)
} else {
logger.Debug(string(respDump))
}
} else {
logger.Infof("Incoming response:\n%s", respDump)
logger.Debug(err)
}
}
return resp, err
Expand Down
22 changes: 22 additions & 0 deletions pkg/log/formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package log

import (
"bytes"

"github.com/sirupsen/logrus"
)

// Formatter is a simple formatter for logrus.
type Formatter struct{}

// Format returns the log entry message with a trailing newline.
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}
b.WriteString(entry.Message + "\n")
return b.Bytes(), nil
}
48 changes: 48 additions & 0 deletions pkg/log/formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package log

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"

"github.com/sirupsen/logrus"
)

func TestFormat(t *testing.T) {
formatter := &Formatter{}
logger := &logrus.Logger{
Out: ioutil.Discard,
Formatter: formatter,
}

fixtures := []struct {
entry *logrus.Entry
expOut string
}{
{
// simple entry
&logrus.Entry{
Logger: logger,
Message: "DEBUGME",
},
"DEBUGME\n",
},
{
// entry with buffer
&logrus.Entry{
Logger: logger,
Message: "DEBUGMETOO",
Buffer: bytes.NewBuffer(nil),
},
"DEBUGMETOO\n",
},
}

for _, fixture := range fixtures {
out, err := formatter.Format(fixture.entry)
require.NoError(t, err)
require.Equal(t, fixture.expOut, string(out))
}
}