github.com/lingyao2333/mo-zero@v1.4.1/core/logx/readme-cn.md (about) 1 <IMG align="right" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png"> 2 3 # logx 4 5 [English](readme.md) | 简体中文 6 7 ## logx 配置 8 9 ```go 10 type LogConf struct { 11 ServiceName string `json:",optional"` 12 Mode string `json:",default=console,options=[console,file,volume]"` 13 Encoding string `json:",default=json,options=[json,plain]"` 14 TimeFormat string `json:",optional"` 15 Path string `json:",default=logs"` 16 Level string `json:",default=info,options=[info,error,severe]"` 17 Compress bool `json:",optional"` 18 KeepDays int `json:",optional"` 19 StackCooldownMillis int `json:",default=100"` 20 MaxBackups int `json:",default=0"` 21 MaxSize int `json:",default=0"` 22 Rotation string `json:",default=daily,options=[daily,size]"` 23 } 24 ``` 25 26 - `ServiceName`:设置服务名称,可选。在 `volume` 模式下,该名称用于生成日志文件。在 `rest/zrpc` 服务中,名称将被自动设置为 `rest`或`zrpc` 的名称。 27 - `Mode`:输出日志的模式,默认是 `console` 28 - `console` 模式将日志写到 `stdout/stderr` 29 - `file` 模式将日志写到 `Path` 指定目录的文件中 30 - `volume` 模式在 docker 中使用,将日志写入挂载的卷中 31 - `Encoding`: 指示如何对日志进行编码,默认是 `json` 32 - `json`模式以 json 格式写日志 33 - `plain`模式用纯文本写日志,并带有终端颜色显示 34 - `TimeFormat`:自定义时间格式,可选。默认是 `2006-01-02T15:04:05.000Z07:00` 35 - `Path`:设置日志路径,默认为 `logs` 36 - `Level`: 用于过滤日志的日志级别。默认为 `info` 37 - `info`,所有日志都被写入 38 - `error`, `info` 的日志被丢弃 39 - `severe`, `info` 和 `error` 日志被丢弃,只有 `severe` 日志被写入 40 - `Compress`: 是否压缩日志文件,只在 `file` 模式下工作 41 - `KeepDays`:日志文件被保留多少天,在给定的天数之后,过期的文件将被自动删除。对 `console` 模式没有影响 42 - `StackCooldownMillis`:多少毫秒后再次写入堆栈跟踪。用来避免堆栈跟踪日志过多 43 - `MaxBackups`: 多少个日志文件备份将被保存。0代表所有备份都被保存。当`Rotation`被设置为`size`时才会起作用。注意:`KeepDays`选项的优先级会比`MaxBackups`高,即使`MaxBackups`被设置为0,当达到`KeepDays`上限时备份文件同样会被删除。 44 - `MaxSize`: 当前被写入的日志文件最大可占用多少空间。0代表没有上限。单位为`MB`。当`Rotation`被设置为`size`时才会起作用。 45 - `Rotation`: 日志轮转策略类型。默认为`daily`(按天轮转)。 46 - `daily` 按天轮转。 47 - `size` 按日志大小轮转。 48 49 50 ## 打印日志方法 51 52 ```go 53 type Logger interface { 54 // Error logs a message at error level. 55 Error(...interface{}) 56 // Errorf logs a message at error level. 57 Errorf(string, ...interface{}) 58 // Errorv logs a message at error level. 59 Errorv(interface{}) 60 // Errorw logs a message at error level. 61 Errorw(string, ...LogField) 62 // Info logs a message at info level. 63 Info(...interface{}) 64 // Infof logs a message at info level. 65 Infof(string, ...interface{}) 66 // Infov logs a message at info level. 67 Infov(interface{}) 68 // Infow logs a message at info level. 69 Infow(string, ...LogField) 70 // Slow logs a message at slow level. 71 Slow(...interface{}) 72 // Slowf logs a message at slow level. 73 Slowf(string, ...interface{}) 74 // Slowv logs a message at slow level. 75 Slowv(interface{}) 76 // Sloww logs a message at slow level. 77 Sloww(string, ...LogField) 78 // WithContext returns a new logger with the given context. 79 WithContext(context.Context) Logger 80 // WithDuration returns a new logger with the given duration. 81 WithDuration(time.Duration) Logger 82 } 83 ``` 84 85 - `Error`, `Info`, `Slow`: 将任何类型的信息写进日志,使用 `fmt.Sprint(...)` 来转换为 `string` 86 - `Errorf`, `Infof`, `Slowf`: 将指定格式的信息写入日志 87 - `Errorv`, `Infov`, `Slowv`: 将任何类型的信息写入日志,用 `json marshal` 编码 88 - `Errorw`, `Infow`, `Sloww`: 写日志,并带上给定的 `key:value` 字段 89 - `WithContext`:将给定的 ctx 注入日志信息,例如用于记录 `trace-id`和`span-id` 90 - `WithDuration`: 将指定的时间写入日志信息中,字段名为 `duration` 91 92 ## 与第三方日志库集成 93 94 - zap 95 - 实现:[https://github.com/zeromicro/zero-contrib/blob/main/logx/zapx/zap.go](https://github.com/zeromicro/zero-contrib/blob/main/logx/zapx/zap.go) 96 - 使用示例:[https://github.com/zeromicro/zero-examples/blob/main/logx/zaplog/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/zaplog/main.go) 97 - logrus 98 - 实现:[https://github.com/zeromicro/zero-contrib/blob/main/logx/logrusx/logrus.go](https://github.com/zeromicro/zero-contrib/blob/main/logx/logrusx/logrus.go) 99 - 使用示例:[https://github.com/zeromicro/zero-examples/blob/main/logx/logrus/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/logrus/main.go) 100 101 对于其它的日志库,请参考上面示例实现,并欢迎提交 `PR` 到 [https://github.com/zeromicro/zero-contrib](https://github.com/zeromicro/zero-contrib) 102 103 ## 将日志写到指定的存储 104 105 `logx`定义了两个接口,方便自定义 `logx`,将日志写入任何存储。 106 107 - `logx.NewWriter(w io.Writer)` 108 - `logx.SetWriter(write logx.Writer)` 109 110 例如,如果我们想把日志写进kafka,而不是控制台或文件,我们可以像下面这样做。 111 112 ```go 113 type KafkaWriter struct { 114 Pusher *kq.Pusher 115 } 116 117 func NewKafkaWriter(pusher *kq.Pusher) *KafkaWriter { 118 return &KafkaWriter{ 119 Pusher: pusher, 120 } 121 } 122 123 func (w *KafkaWriter) Write(p []byte) (n int, err error) { 124 // writing log with newlines, trim them. 125 if err := w.Pusher.Push(strings.TrimSpace(string(p))); err != nil { 126 return 0, err 127 } 128 129 return len(p), nil 130 } 131 132 func main() { 133 pusher := kq.NewPusher([]string{"localhost:9092"}, "go-zero") 134 defer pusher.Close() 135 136 writer := logx.NewWriter(NewKafkaWriter(pusher)) 137 logx.SetWriter(writer) 138 139 // more code 140 } 141 ``` 142 143 完整代码:[https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go) 144 145 ## 过滤敏感字段 146 147 如果我们需要防止 `password` 字段被记录下来,我们可以像下面这样实现。 148 149 ```go 150 type ( 151 Message struct { 152 Name string 153 Password string 154 Message string 155 } 156 157 SensitiveLogger struct { 158 logx.Writer 159 } 160 ) 161 162 func NewSensitiveLogger(writer logx.Writer) *SensitiveLogger { 163 return &SensitiveLogger{ 164 Writer: writer, 165 } 166 } 167 168 func (l *SensitiveLogger) Info(msg interface{}, fields ...logx.LogField) { 169 if m, ok := msg.(Message); ok { 170 l.Writer.Info(Message{ 171 Name: m.Name, 172 Password: "******", 173 Message: m.Message, 174 }, fields...) 175 } else { 176 l.Writer.Info(msg, fields...) 177 } 178 } 179 180 func main() { 181 // setup logx to make sure originalWriter not nil, 182 // the injected writer is only for filtering, like a middleware. 183 184 originalWriter := logx.Reset() 185 writer := NewSensitiveLogger(originalWriter) 186 logx.SetWriter(writer) 187 188 logx.Infov(Message{ 189 Name: "foo", 190 Password: "shouldNotAppear", 191 Message: "bar", 192 }) 193 194 // more code 195 } 196 ``` 197 198 完整代码:[https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go) 199 200 ## 更多示例 201 202 [https://github.com/zeromicro/zero-examples/tree/main/logx](https://github.com/zeromicro/zero-examples/tree/main/logx) 203 204 ## Give a Star! ⭐ 205 206 如果你正在使用或者觉得这个项目对你有帮助,请 **star** 支持,感谢!