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

ddl, util: Add highlight to log #4861

Merged
merged 4 commits into from
Oct 23, 2017
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
3 changes: 2 additions & 1 deletion ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestT(t *testing.T) {
CustomVerboseFlag = true
logLevel := os.Getenv("log_level")
logutil.InitLogger(&logutil.LogConfig{
Level: logLevel,
Level: logLevel,
Format: "highlight",
})
TestingT(t)
}
Expand Down
2 changes: 1 addition & 1 deletion ddl/index_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *testIndexChangeSuite) SetUpSuite(c *C) {
t := meta.NewMeta(txn)
return errors.Trace(t.CreateDatabase(s.dbInfo))
})
c.Check(err, IsNil)
c.Check(err, IsNil, Commentf("err %v", errors.ErrorStack(err)))
}

func (s *testIndexChangeSuite) TestIndexChange(c *C) {
Expand Down
36 changes: 36 additions & 0 deletions util/logutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,30 @@ func stringToLogLevel(level string) log.Level {
return defaultLogLevel
}

// logTypeToColor converts the Level to a color string.
func logTypeToColor(level log.Level) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about change this to func logTypeToColor(type string, level log.Level) string
If type is not "highlight", return "\033".

Copy link
Member

@coocood coocood Oct 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only highlight need to call this function and write color code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So why not call it logLevelToColor?

switch level {
case log.DebugLevel:
return "[0;37"
case log.InfoLevel:
return "[0;36"
case log.WarnLevel:
return "[0;33"
case log.ErrorLevel:
return "[0;31"
case log.FatalLevel:
return "[0;31"
case log.PanicLevel:
return "[0;31"
}

return "[0;37"
}

// textFormatter is for compatability with ngaut/log
type textFormatter struct {
DisableTimestamp bool
EnableColors bool
}

// Format implements logrus.Formatter
Expand All @@ -123,6 +144,12 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) {
} else {
b = &bytes.Buffer{}
}

if f.EnableColors {
colorStr := logTypeToColor(entry.Level)
fmt.Fprintf(b, "\033%sm ", colorStr)
}

if !f.DisableTimestamp {
fmt.Fprintf(b, "%s ", entry.Time.Format(defaultLogTimeFormat))
}
Expand All @@ -136,6 +163,10 @@ func (f *textFormatter) Format(entry *log.Entry) ([]byte, error) {
}
}
b.WriteByte('\n')

if f.EnableColors {
b.WriteString("\033[0m")
}
return b.Bytes(), nil
}

Expand All @@ -156,6 +187,11 @@ func stringToLogFormatter(format string, disableTimestamp bool) log.Formatter {
TimestampFormat: defaultLogTimeFormat,
DisableTimestamp: disableTimestamp,
}
case "highlight":
return &textFormatter{
DisableTimestamp: disableTimestamp,
EnableColors: true,
}
default:
return &textFormatter{}
}
Expand Down