11e6c282 by cloudaice

diffrent level logs display diffrent color

1 parent 91d75e89
...@@ -6,6 +6,25 @@ import ( ...@@ -6,6 +6,25 @@ import (
6 "os" 6 "os"
7 ) 7 )
8 8
9 type Brush func(string) string
10
11 func NewBrush(color string) Brush {
12 pre := "\033["
13 reset := "\033[0m"
14 return func(text string) string {
15 return pre + color + "m" + text + reset
16 }
17 }
18
19 var colors = []Brush{
20 NewBrush("1;36"), // Trace cyan
21 NewBrush("1;34"), // Debug blue
22 NewBrush("1;32"), // Info green
23 NewBrush("1;33"), // Warn yellow
24 NewBrush("1;31"), // Error red
25 NewBrush("1;35"), // Critical purple
26 }
27
9 // ConsoleWriter implements LoggerInterface and writes messages to terminal. 28 // ConsoleWriter implements LoggerInterface and writes messages to terminal.
10 type ConsoleWriter struct { 29 type ConsoleWriter struct {
11 lg *log.Logger 30 lg *log.Logger
...@@ -35,7 +54,7 @@ func (c *ConsoleWriter) WriteMsg(msg string, level int) error { ...@@ -35,7 +54,7 @@ func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
35 if level < c.Level { 54 if level < c.Level {
36 return nil 55 return nil
37 } 56 }
38 c.lg.Println(msg) 57 c.lg.Println(colors[level](msg))
39 return nil 58 return nil
40 } 59 }
41 60
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!