github.com/xwi88/log4go@v0.0.6/console_writer.go (about) 1 package log4go 2 3 import ( 4 "fmt" 5 "os" 6 ) 7 8 type colorRecord Record 9 10 // brush is a color join function 11 type brush func(string) string 12 13 // newBrush return a fix color Brush 14 func newBrush(color string) brush { 15 pre := "\033[" 16 reset := "\033[0m" 17 return func(text string) string { 18 return fmt.Sprintf("%s%s%s%s%s", pre, color, "m", text, reset) 19 } 20 } 21 22 // effect: 0~8 23 // 0:no, 1: Highlight (deepen) display, 2: Low light (dimmed) display, 24 // 4: underline, 5: blink, 7: Reverse display (replace background color and font color) 25 // 8: blank 26 27 // font color: 30~39 28 // 30: black, 31: red, 32: green, 33: yellow, 34: blue, 35: purple, 36: dark green, 37: grey 29 // 38: Sets the underline on the default foreground color, 39: Turn off underlining on the default foreground color 30 31 // background color: 40~49 32 // 40: black, 41: red, 42: green, 43: yellow, 44: blue, 45: purple, 46: dark green, 47: grey 33 34 // (background;font;effect) 35 var colors = []brush{ 36 newBrush("1;31"), // Emergency red 37 newBrush("1;36"), // Alert dark green 38 newBrush("1;35"), // Critical purple 39 newBrush("1;31"), // Error red 40 newBrush("1;33"), // Warning yellow 41 newBrush("1;32"), // Notice green 42 newBrush("1;34"), // Informational blue 43 newBrush("2;37"), // Debug grey 44 } 45 46 func (r *colorRecord) ColorString() string { 47 inf := fmt.Sprintf("%s %s %s %s\n", r.time, LevelFlags[r.level], r.file, r.msg) 48 return colors[r.level](inf) 49 } 50 51 func (r *colorRecord) String() string { 52 inf := "" 53 switch r.level { 54 case EMERGENCY: 55 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[31m%s\033[0m] \033[47;30m%s\033[0m %s\n", 56 r.time, LevelFlags[r.level], r.file, r.msg) 57 case ALERT: 58 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[36m%s\033[0m] \033[47;30m%s\033[0m %s\n", 59 r.time, LevelFlags[r.level], r.file, r.msg) 60 case CRITICAL: 61 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[35m%s\033[0m] \033[47;30m%s\033[0m %s\n", 62 r.time, LevelFlags[r.level], r.file, r.msg) 63 case ERROR: 64 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[31m%s\033[0m] \033[47;30m%s\033[0m %s\n", 65 r.time, LevelFlags[r.level], r.file, r.msg) 66 case WARNING: 67 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[33m%s\033[0m] \033[47;30m%s\033[0m %s\n", 68 r.time, LevelFlags[r.level], r.file, r.msg) 69 case NOTICE: 70 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[32m%s\033[0m] \033[47;30m%s\033[0m %s\n", 71 r.time, LevelFlags[r.level], r.file, r.msg) 72 case INFO: 73 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[34m%s\033[0m] \033[47;30m%s\033[0m %s\n", 74 r.time, LevelFlags[r.level], r.file, r.msg) 75 case DEBUG: 76 inf = fmt.Sprintf("\033[36m%s\033[0m [\033[44m%s\033[0m] \033[47;30m%s\033[0m %s\n", 77 r.time, LevelFlags[r.level], r.file, r.msg) 78 } 79 80 return inf 81 } 82 83 // ConsoleWriter console writer define 84 type ConsoleWriter struct { 85 level int 86 color bool 87 fullColor bool // line all with color 88 } 89 90 // ConsoleWriterOptions color field options 91 type ConsoleWriterOptions struct { 92 Enable bool `json:"enable" mapstructure:"enable"` 93 Color bool `json:"color" mapstructure:"color"` 94 FullColor bool `json:"full_color" mapstructure:"full_color"` 95 Level string `json:"level" mapstructure:"level"` 96 } 97 98 // NewConsoleWriter create new console writer 99 func NewConsoleWriter() *ConsoleWriter { 100 return &ConsoleWriter{} 101 } 102 103 // NewConsoleWriterWithOptions create new console writer with level 104 func NewConsoleWriterWithOptions(options ConsoleWriterOptions) *ConsoleWriter { 105 defaultLevel := DEBUG 106 107 if len(options.Level) > 0 { 108 defaultLevel = getLevelDefault(options.Level, defaultLevel, "") 109 } 110 111 return &ConsoleWriter{ 112 level: defaultLevel, 113 color: options.Color, 114 fullColor: options.FullColor, 115 } 116 } 117 118 // Write console write 119 func (w *ConsoleWriter) Write(r *Record) error { 120 if r.level > w.level { 121 return nil 122 } 123 if w.color { 124 if w.fullColor { 125 _, _ = fmt.Fprint(os.Stdout, ((*colorRecord)(r)).ColorString()) 126 } else { 127 _, _ = fmt.Fprint(os.Stdout, ((*colorRecord)(r)).String()) 128 } 129 } else { 130 _, _ = fmt.Fprint(os.Stdout, r.String()) 131 } 132 return nil 133 } 134 135 // Init console init without implement 136 func (w *ConsoleWriter) Init() error { 137 return nil 138 } 139 140 // SetColor console output color control 141 func (w *ConsoleWriter) SetColor(c bool) { 142 w.color = c 143 } 144 145 // SetFullColor console output full line color control 146 func (w *ConsoleWriter) SetFullColor(c bool) { 147 w.fullColor = c 148 }