diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 8331d35735d71..8aab11bc92af1 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -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) } diff --git a/ddl/index_change_test.go b/ddl/index_change_test.go index e976535937c43..dd19d85997643 100644 --- a/ddl/index_change_test.go +++ b/ddl/index_change_test.go @@ -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) { diff --git a/util/logutil/log.go b/util/logutil/log.go index 010120efb2bd8..301b3a9be2087 100644 --- a/util/logutil/log.go +++ b/util/logutil/log.go @@ -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 { + 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 @@ -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)) } @@ -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 } @@ -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{} }