github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/logs/logger.go (about) 1 // the package is exported from github.com/beego/beego/v2/core/logs 2 3 // Copyright 2023. All Rights Reserved. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package logs 18 19 import ( 20 "io" 21 "runtime" 22 "sync" 23 "time" 24 ) 25 26 type logWriter struct { 27 sync.Mutex 28 writer io.Writer 29 } 30 31 func newLogWriter(wr io.Writer) *logWriter { 32 return &logWriter{writer: wr} 33 } 34 35 func (lg *logWriter) writeln(msg string) (int, error) { 36 lg.Lock() 37 msg += "\n" 38 n, err := lg.writer.Write([]byte(msg)) 39 lg.Unlock() 40 return n, err 41 } 42 43 const ( 44 y1 = `0123456789` 45 y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789` 46 y3 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999` 47 y4 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789` 48 mo1 = `000000000111` 49 mo2 = `123456789012` 50 d1 = `0000000001111111111222222222233` 51 d2 = `1234567890123456789012345678901` 52 h1 = `000000000011111111112222` 53 h2 = `012345678901234567890123` 54 mi1 = `000000000011111111112222222222333333333344444444445555555555` 55 mi2 = `012345678901234567890123456789012345678901234567890123456789` 56 s1 = `000000000011111111112222222222333333333344444444445555555555` 57 s2 = `012345678901234567890123456789012345678901234567890123456789` 58 ns1 = `0123456789` 59 ) 60 61 func formatTimeHeader(when time.Time) ([]byte, int, int) { 62 y, mo, d := when.Date() 63 h, mi, s := when.Clock() 64 ns := when.Nanosecond() / 1000000 65 // len("2006/01/02 15:04:05.123 ")==24 66 var buf [24]byte 67 68 buf[0] = y1[y/1000%10] 69 buf[1] = y2[y/100] 70 buf[2] = y3[y-y/100*100] 71 buf[3] = y4[y-y/100*100] 72 buf[4] = '/' 73 buf[5] = mo1[mo-1] 74 buf[6] = mo2[mo-1] 75 buf[7] = '/' 76 buf[8] = d1[d-1] 77 buf[9] = d2[d-1] 78 buf[10] = ' ' 79 buf[11] = h1[h] 80 buf[12] = h2[h] 81 buf[13] = ':' 82 buf[14] = mi1[mi] 83 buf[15] = mi2[mi] 84 buf[16] = ':' 85 buf[17] = s1[s] 86 buf[18] = s2[s] 87 buf[19] = '.' 88 buf[20] = ns1[ns/100] 89 buf[21] = ns1[ns%100/10] 90 buf[22] = ns1[ns%10] 91 92 buf[23] = ' ' 93 94 return buf[0:], d, h 95 } 96 97 var ( 98 green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109}) 99 white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109}) 100 yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109}) 101 red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109}) 102 blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109}) 103 magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109}) 104 cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109}) 105 106 w32Green = string([]byte{27, 91, 52, 50, 109}) 107 w32White = string([]byte{27, 91, 52, 55, 109}) 108 w32Yellow = string([]byte{27, 91, 52, 51, 109}) 109 w32Red = string([]byte{27, 91, 52, 49, 109}) 110 w32Blue = string([]byte{27, 91, 52, 52, 109}) 111 w32Magenta = string([]byte{27, 91, 52, 53, 109}) 112 w32Cyan = string([]byte{27, 91, 52, 54, 109}) 113 114 reset = string([]byte{27, 91, 48, 109}) 115 ) 116 117 var ( 118 once sync.Once 119 colorMap map[string]string 120 ) 121 122 func initColor() { 123 if runtime.GOOS == "windows" { 124 green = w32Green 125 white = w32White 126 yellow = w32Yellow 127 red = w32Red 128 blue = w32Blue 129 magenta = w32Magenta 130 cyan = w32Cyan 131 cyan = w32White 132 } 133 colorMap = map[string]string{ 134 // by color 135 "green": green, 136 "white": white, 137 "yellow": yellow, 138 "red": red, 139 // by method 140 "GET": blue, 141 "POST": cyan, 142 "PUT": yellow, 143 "DELETE": red, 144 "PATCH": green, 145 "HEAD": magenta, 146 "OPTIONS": white, 147 } 148 } 149 150 // ColorByStatus return color by http code 151 // 2xx return Green 152 // 3xx return White 153 // 4xx return Yellow 154 // 5xx return Red 155 func ColorByStatus(code int) string { 156 once.Do(initColor) 157 switch { 158 case code >= 200 && code < 300: 159 return colorMap["green"] 160 case code >= 300 && code < 400: 161 return colorMap["white"] 162 case code >= 400 && code < 500: 163 return colorMap["yellow"] 164 default: 165 return colorMap["red"] 166 } 167 } 168 169 // ColorByMethod return color by http code 170 func ColorByMethod(method string) string { 171 once.Do(initColor) 172 if c := colorMap[method]; c != "" { 173 return c 174 } 175 return reset 176 } 177 178 // ResetColor return reset color 179 func ResetColor() string { 180 return reset 181 }