github.com/gogf/gf/v2@v2.7.4/os/glog/glog_logger_chaining.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package glog 8 9 import ( 10 "io" 11 12 "github.com/gogf/gf/v2/os/gfile" 13 ) 14 15 // To is a chaining function, 16 // which redirects current logging content output to the specified `writer`. 17 func (l *Logger) To(writer io.Writer) *Logger { 18 logger := (*Logger)(nil) 19 if l.parent == nil { 20 logger = l.Clone() 21 } else { 22 logger = l 23 } 24 logger.SetWriter(writer) 25 return logger 26 } 27 28 // Path is a chaining function, 29 // which sets the directory path to `path` for current logging content output. 30 // 31 // Note that the parameter `path` is a directory path, not a file path. 32 func (l *Logger) Path(path string) *Logger { 33 logger := (*Logger)(nil) 34 if l.parent == nil { 35 logger = l.Clone() 36 } else { 37 logger = l 38 } 39 if path != "" { 40 if err := logger.SetPath(path); err != nil { 41 panic(err) 42 } 43 } 44 return logger 45 } 46 47 // Cat is a chaining function, 48 // which sets the category to `category` for current logging content output. 49 // Param `category` can be hierarchical, eg: module/user. 50 func (l *Logger) Cat(category string) *Logger { 51 logger := (*Logger)(nil) 52 if l.parent == nil { 53 logger = l.Clone() 54 } else { 55 logger = l 56 } 57 if logger.config.Path != "" { 58 if err := logger.SetPath(gfile.Join(logger.config.Path, category)); err != nil { 59 panic(err) 60 } 61 } 62 return logger 63 } 64 65 // File is a chaining function, 66 // which sets file name `pattern` for the current logging content output. 67 func (l *Logger) File(file string) *Logger { 68 if file == "" { 69 return l 70 } 71 logger := (*Logger)(nil) 72 if l.parent == nil { 73 logger = l.Clone() 74 } else { 75 logger = l 76 } 77 logger.SetFile(file) 78 return logger 79 } 80 81 // Level is a chaining function, 82 // which sets logging level for the current logging content output. 83 func (l *Logger) Level(level int) *Logger { 84 logger := (*Logger)(nil) 85 if l.parent == nil { 86 logger = l.Clone() 87 } else { 88 logger = l 89 } 90 logger.SetLevel(level) 91 return logger 92 } 93 94 // LevelStr is a chaining function, 95 // which sets logging level for the current logging content output using level string. 96 func (l *Logger) LevelStr(levelStr string) *Logger { 97 logger := (*Logger)(nil) 98 if l.parent == nil { 99 logger = l.Clone() 100 } else { 101 logger = l 102 } 103 if err := logger.SetLevelStr(levelStr); err != nil { 104 panic(err) 105 } 106 return logger 107 } 108 109 // Skip is a chaining function, 110 // which sets stack skip for the current logging content output. 111 // It also affects the caller file path checks when line number printing enabled. 112 func (l *Logger) Skip(skip int) *Logger { 113 logger := (*Logger)(nil) 114 if l.parent == nil { 115 logger = l.Clone() 116 } else { 117 logger = l 118 } 119 logger.SetStackSkip(skip) 120 return logger 121 } 122 123 // Stack is a chaining function, 124 // which sets stack options for the current logging content output . 125 func (l *Logger) Stack(enabled bool, skip ...int) *Logger { 126 logger := (*Logger)(nil) 127 if l.parent == nil { 128 logger = l.Clone() 129 } else { 130 logger = l 131 } 132 logger.SetStack(enabled) 133 if len(skip) > 0 { 134 logger.SetStackSkip(skip[0]) 135 } 136 return logger 137 } 138 139 // StackWithFilter is a chaining function, 140 // which sets stack filter for the current logging content output . 141 func (l *Logger) StackWithFilter(filter string) *Logger { 142 logger := (*Logger)(nil) 143 if l.parent == nil { 144 logger = l.Clone() 145 } else { 146 logger = l 147 } 148 logger.SetStack(true) 149 logger.SetStackFilter(filter) 150 return logger 151 } 152 153 // Stdout is a chaining function, 154 // which enables/disables stdout for the current logging content output. 155 // It's enabled in default. 156 func (l *Logger) Stdout(enabled ...bool) *Logger { 157 logger := (*Logger)(nil) 158 if l.parent == nil { 159 logger = l.Clone() 160 } else { 161 logger = l 162 } 163 // stdout printing is enabled if `enabled` is not passed. 164 if len(enabled) > 0 && !enabled[0] { 165 logger.config.StdoutPrint = false 166 } else { 167 logger.config.StdoutPrint = true 168 } 169 return logger 170 } 171 172 // Header is a chaining function, 173 // which enables/disables log header for the current logging content output. 174 // It's enabled in default. 175 func (l *Logger) Header(enabled ...bool) *Logger { 176 logger := (*Logger)(nil) 177 if l.parent == nil { 178 logger = l.Clone() 179 } else { 180 logger = l 181 } 182 // header is enabled if `enabled` is not passed. 183 if len(enabled) > 0 && !enabled[0] { 184 logger.SetHeaderPrint(false) 185 } else { 186 logger.SetHeaderPrint(true) 187 } 188 return logger 189 } 190 191 // Line is a chaining function, 192 // which enables/disables printing its caller file path along with its line number. 193 // The parameter `long` specified whether print the long absolute file path, eg: /a/b/c/d.go:23, 194 // or else short one: d.go:23. 195 func (l *Logger) Line(long ...bool) *Logger { 196 logger := (*Logger)(nil) 197 if l.parent == nil { 198 logger = l.Clone() 199 } else { 200 logger = l 201 } 202 if len(long) > 0 && long[0] { 203 logger.config.Flags |= F_FILE_LONG 204 } else { 205 logger.config.Flags |= F_FILE_SHORT 206 } 207 return logger 208 } 209 210 // Async is a chaining function, 211 // which enables/disables async logging output feature. 212 func (l *Logger) Async(enabled ...bool) *Logger { 213 logger := (*Logger)(nil) 214 if l.parent == nil { 215 logger = l.Clone() 216 } else { 217 logger = l 218 } 219 // async feature is enabled if `enabled` is not passed. 220 if len(enabled) > 0 && !enabled[0] { 221 logger.SetAsync(false) 222 } else { 223 logger.SetAsync(true) 224 } 225 return logger 226 }