github.com/zooyer/miskit@v1.0.71/log/format.go (about) 1 package log 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "strconv" 8 "strings" 9 ) 10 11 type Formatter func(record *Record) string 12 13 const timeLayout = "2006-01-02 15:04:05.000" 14 15 // {"time":"2020-10-25T16:36:04.4143659+08:00","level":"DEBUG","tag":[{"key":"module","value":"apollo"},{"key":"trace","value":"10001241235"},{"key":"type","value":"rpc"},{"key":"rpc","value":"http"},{"key":"latency","value":"10"},{"key":"other","value":{"json":"value"," ":" "}}],"message":"hello world"} 16 func JSONFormatter(indent bool) Formatter { 17 return func(record *Record) string { 18 data, _ := json.Marshal(record) 19 if indent { 20 var buf bytes.Buffer 21 if err := json.Indent(&buf, data, "", " "); err == nil { 22 data = buf.Bytes() 23 } 24 } 25 return string(data) 26 } 27 } 28 29 // date time level tag message 30 // 2019-10-27 00:04:36.028 DEBUG "trace"="10001241235" "type"="rpc" "rpc"="http" "hello world" 31 func TextFormatter(color bool) Formatter { 32 return func(record *Record) string { 33 var builder = pool.Get().(*strings.Builder) 34 defer pool.Put(builder) 35 defer builder.Reset() 36 37 // time 38 var str = record.Time.Format(timeLayout) 39 if color { 40 str = yellow(str) 41 } 42 builder.WriteString(str) 43 builder.WriteString(" ") 44 45 // level 46 if str = record.Level; color { 47 switch record.Level { 48 case "DEBUG": 49 str = green(str) 50 case "INFO": 51 str = blue(str) 52 case "WARNING": 53 str = orange(str) 54 case "ERROR": 55 str = red(str) 56 case "FATAL": 57 str = red(str) 58 default: 59 str = red(str) 60 } 61 } 62 builder.WriteString(str) 63 builder.WriteString(" ") 64 65 // tag 66 if tag := record.Tag; len(tag) > 0 { 67 var write bool 68 for i := range tag { 69 if tag[i].Value == nil { 70 continue 71 } 72 73 key := tag[i].Key 74 var val string 75 switch v := tag[i].Value.(type) { 76 case string: 77 val = v 78 case json.RawMessage: 79 if data, err := json.Marshal(v); err == nil { 80 val = string(data) 81 } else { 82 val = string(v) 83 } 84 default: 85 val = fmt.Sprint(tag[i].Value) 86 } 87 if key == "" || val == "" { 88 continue 89 } 90 if color { 91 key = cyan(key) 92 val = cyan(val) 93 } 94 if i != 0 { 95 builder.WriteString(" ") 96 } 97 builder.WriteString(key) 98 builder.WriteString("=") 99 builder.WriteString(val) 100 if !write { 101 write = true 102 } 103 } 104 if write { 105 builder.WriteString(" ") 106 } 107 } 108 109 // message 110 if str = strconv.Quote(record.Message); len(str)-2 == len(record.Message) { 111 str = record.Message 112 } 113 builder.WriteString(str) 114 115 return builder.String() 116 } 117 } 118 119 // colors render 120 func green(s string) string { 121 return "\033[32m" + s + "\033[0m" 122 } 123 124 func red(s string) string { 125 return "\033[31m" + s + "\033[0m" 126 } 127 128 func blue(s string) string { 129 return "\033[34m" + s + "\033[0m" 130 } 131 132 func orange(s string) string { 133 return "\033[35m" + s + "\033[0m" 134 } 135 136 func yellow(s string) string { 137 return "\033[33m" + s + "\033[0m" 138 } 139 140 func cyan(s string) string { 141 return "\033[36m" + s + "\033[0m" 142 }