1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| package logUtil
import ( "fmt" "io" "os" "path" "runtime" "strconv" "sync" "time" ) const ( colorRed = 31 colorYellow = 33 colorBlue = 34 levelT = "[T] " levelE = "[E] " levelW = "[W] " defaultFileSize = 60 * 1024 * 1024 minFileSize = 1 * 1024 * 1024 defaultLogDir = "log" defaultLogName = "default.log" logTypeStd logType = iota + 1 logTypeFile ) type ( logType int LogOption func(log *myLog) myLog struct { sync.Once sync.Mutex outs map[logType]io.Writer file *os.File fileName string dir string size int64 } ) var ( defaultLogger = &myLog{} ) func (m *myLog) init() { if m.dir == "" { m.dir = defaultLogDir } if m.fileName == "" { m.fileName = defaultLogName } if m.size == 0 { m.size = defaultFileSize } else { if m.size < minFileSize { panic(fmt.Sprintf("invalid size: %d", m.size)) } } if m.outs == nil { m.outs = make(map[logType]io.Writer) } if !isExist(m.dir) { if err := os.Mkdir(m.dir, 0777); err != nil { panic(err) } } name := path.Join(m.dir, m.fileName) file, err := os.OpenFile(name, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755) if err != nil { panic(err) } m.file = file m.outs[logTypeStd] = os.Stdout m.outs[logTypeFile] = file } func (m *myLog) checkLogSize() { if m.file == nil { return } m.Lock() defer m.Unlock() fileInfo, err := m.file.Stat() if err != nil { panic(err) } if m.size > fileInfo.Size() { return } newName := path.Join(m.dir, time.Now().Format("2006_01_02_15:04:03")+".log") name := path.Join(m.dir, m.fileName) err = os.Rename(name, newName) if err != nil { panic(err) } file, err := os.OpenFile(name, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0755) if err != nil { panic(err) } m.file.Close() m.file = file m.outs[logTypeFile] = file return } func (m *myLog) write(level string, content string) { m.checkLogSize() var colorText int switch level { case levelT: colorText = colorBlue case levelW: colorText = colorYellow case levelE: colorText = colorRed } for k, wr := range m.outs { if k == logTypeStd { fmt.Fprintf(wr, setColor(content, colorText)) } else { fmt.Fprintf(wr, content) } } } func WithSize(size int64) LogOption { return func(log *myLog) { log.size = size } } func WithLogDir(dir string) LogOption { return func(log *myLog) { log.dir = dir } } func WithFileName(name string) LogOption { return func(log *myLog) { log.fileName = name } } func InitLogger(args ...LogOption) { defaultLogger.Do(func() { for _, af := range args { af(defaultLogger) } defaultLogger.init() }) }
func T(format string, v ...interface{}) { _, file, line, _ := runtime.Caller(1) timeStr := time.Now().Format("2006-01-02 15:04:05.0000") + " " codeLine := "[" + timeStr + shortFileName(file) + ":" + strconv.Itoa(line) + "]" content := levelT + codeLine + fmt.Sprintf(format, v...) + "\n" defaultLogger.write(levelT, content) }
func E(format string, v ...interface{}) { _, file, line, _ := runtime.Caller(1) timeStr := time.Now().Format("2006-01-02 15:04:05.0000") + " " codeLine := "[" + timeStr + shortFileName(file) + ":" + strconv.Itoa(line) + "]" content := levelE + codeLine + fmt.Sprintf(format, v...) + "\n" defaultLogger.write(levelE, content) }
func W(format string, v ...interface{}) { _, file, line, _ := runtime.Caller(1) timeStr := time.Now().Format("2006-01-02 15:04:05.0000") + " " codeLine := "[" + timeStr + shortFileName(file) + ":" + strconv.Itoa(line) + "]" content := levelW + codeLine + fmt.Sprintf(format, v...) + "\n" defaultLogger.write(levelW, content) } func isExist(path string) bool { _, err := os.Stat(path) if err != nil { if os.IsExist(err) { return true } return false } return true } func shortFileName(file string) string { short := file for i := len(file) - 1; i > 0; i-- { if file[i] == '/' { short = file[i+1:] break } } return short } func setColor(msg string, text int) string { return fmt.Sprintf("%c[%dm%s%c[0m", 0x1B, text, msg, 0x1B) }
|