|
| 1 | +--- |
| 2 | +title: "slog" |
| 3 | +linkTitle: "slog" |
| 4 | +weight: 5 |
| 5 | +keywords: ["Logger Extension", "slog"] |
| 6 | +description: "Hertz interfaces with slog and lumberjack." |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Logger structure |
| 11 | + |
| 12 | +```go |
| 13 | +var _ hlog.FullLogger = (*Logger)(nil) |
| 14 | + |
| 15 | +type Logger struct { |
| 16 | + l *slog.Logger |
| 17 | + cfg *config |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +## NewLogger |
| 22 | + |
| 23 | +Create and initialize a Logger through `defaultConfig()`. The required configuration can be passed into the function as a parameter. If no parameters are passed in, the initial configuration will be installed to create the Logger. |
| 24 | +For related configuration, please refer to [option configuration](#option-configuration) below |
| 25 | + |
| 26 | +Currently only `slog.NewJSONHandler()` in the slog library is supported, `slog.NewTextHandler()` is not supported |
| 27 | + |
| 28 | +Function Signature: |
| 29 | + |
| 30 | +```go |
| 31 | +func NewLogger(opts ...Option) *Logger |
| 32 | +``` |
| 33 | + |
| 34 | +Sample code: |
| 35 | + |
| 36 | +```go |
| 37 | +package main |
| 38 | + |
| 39 | +import ( |
| 40 | + "github.com/cloudwego/hertz/pkg/common/hlog" |
| 41 | + hertzslog "github.com/hertz-contrib/logger/slog" |
| 42 | + "os" |
| 43 | +) |
| 44 | + |
| 45 | +func main() { |
| 46 | + logger := hertzslog.NewLogger(hertzslog.WithOutput(os.Stdout)) |
| 47 | + hlog.SetLogger(logger) |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## Logger |
| 52 | + |
| 53 | +`Logger` is used to obtain `*slog.Logger` to meet complex operations |
| 54 | + |
| 55 | +Function Signature: |
| 56 | + |
| 57 | +```go |
| 58 | +func (l *Logger) Logger() *slog.Logger |
| 59 | +``` |
| 60 | + |
| 61 | +Sample code: |
| 62 | + |
| 63 | +```go |
| 64 | +package main |
| 65 | + |
| 66 | +import ( |
| 67 | + hertzslog "github.com/hertz-contrib/logger/slog" |
| 68 | + "os" |
| 69 | +) |
| 70 | + |
| 71 | +func main() { |
| 72 | + logger := hertzslog.NewLogger(hertzslog.WithOutput(os.Stdout)) |
| 73 | + l := logger.Logger() |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +## Option configuration |
| 78 | + |
| 79 | +### WithOutput |
| 80 | + |
| 81 | +`WithOutput` is used to set the output location of the log |
| 82 | + |
| 83 | +Function Signature: |
| 84 | + |
| 85 | +```go |
| 86 | +func WithOutput(writer io.Writer) Option |
| 87 | +``` |
| 88 | + |
| 89 | +Sample code: |
| 90 | + |
| 91 | +```go |
| 92 | +package main |
| 93 | + |
| 94 | +import ( |
| 95 | + hertzslog "github.com/hertz-contrib/logger/slog" |
| 96 | + "os" |
| 97 | +) |
| 98 | + |
| 99 | +func main() { |
| 100 | + logger := hertzslog.NewLogger(hertzslog.WithOutput(os.Stdout)) |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +### WithLevel |
| 107 | + |
| 108 | +`WithLevel` judges the incoming `*slog.LevelVar`. Only log levels higher than or equal to this will be recorded |
| 109 | + |
| 110 | +>It is worth noting that if `WithLevel` is set together with `WithHandlerOptions`, the log level of WithLevel will override the log level in WithHandlerOptions |
| 111 | +
|
| 112 | +Function Signature: |
| 113 | + |
| 114 | +```go |
| 115 | +func WithLevel(lvl *slog.LevelVar) Option |
| 116 | +``` |
| 117 | + |
| 118 | +Sample code: |
| 119 | + |
| 120 | +```go |
| 121 | +package main |
| 122 | + |
| 123 | +import ( |
| 124 | + hertzslog "github.com/hertz-contrib/logger/slog" |
| 125 | +) |
| 126 | + |
| 127 | +func main() { |
| 128 | + //Empty LevelVar corresponds to LevelInfo |
| 129 | + logger := hertzslog.NewLogger(hertzslog.WithLevel(&slog.LevelVar{})) |
| 130 | + |
| 131 | + //Dynamically set the log level to Level Debug |
| 132 | + levelVar := slog.LevelVar{} |
| 133 | + levelVar.Set(slog.LevelDebug) |
| 134 | + logger := hertzslog.NewLogger(hertzslog.WithLevel(&slog.LevelVar{})) |
| 135 | +} |
| 136 | + |
| 137 | +``` |
| 138 | + |
| 139 | +### WithHandlerOptions |
| 140 | + |
| 141 | +`WithHandlerOptions` passes `*slog.HandlerOptions` into the configuration |
| 142 | + |
| 143 | +Function Signature: |
| 144 | + |
| 145 | +```go |
| 146 | +func WithHandlerOptions(opts *slog.HandlerOptions) Option |
| 147 | +``` |
| 148 | + |
| 149 | +Sample code: |
| 150 | + |
| 151 | +```go |
| 152 | +package main |
| 153 | + |
| 154 | +import ( |
| 155 | + hertzslog "github.com/hertz-contrib/logger/slog" |
| 156 | +) |
| 157 | + |
| 158 | +func main() { |
| 159 | + logger := hertzslog.NewLogger(hertzslog.WithHandlerOptions(&slog.HandlerOptions{ |
| 160 | + AddSource: false, |
| 161 | + Level: slog.LevelInfo, |
| 162 | + ReplaceAttr: nil, |
| 163 | + })) |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +## A complete slog example |
| 168 | + |
| 169 | +```go |
| 170 | +package main |
| 171 | + |
| 172 | +import ( |
| 173 | + "context" |
| 174 | + "log" |
| 175 | + "os" |
| 176 | + "path" |
| 177 | + "time" |
| 178 | + |
| 179 | + "github.com/cloudwego/hertz/pkg/app" |
| 180 | + "github.com/cloudwego/hertz/pkg/app/server" |
| 181 | + "github.com/cloudwego/hertz/pkg/common/hlog" |
| 182 | + "github.com/cloudwego/hertz/pkg/protocol/consts" |
| 183 | + hertzslog "github.com/hertz-contrib/logger/slog" |
| 184 | + "gopkg.in/natefinch/lumberjack.v2" |
| 185 | +) |
| 186 | + |
| 187 | +func main() { |
| 188 | + h := server.Default() |
| 189 | + |
| 190 | + // Customizable output directory. |
| 191 | + var logFilePath string |
| 192 | + dir := "./hlog" |
| 193 | + logFilePath = dir + "/logs/" |
| 194 | + if err := os.MkdirAll(logFilePath, 0o777); err != nil { |
| 195 | + log.Println(err.Error()) |
| 196 | + return |
| 197 | + } |
| 198 | + |
| 199 | + // 将文件名设置为日期 |
| 200 | + logFileName := time.Now().Format("2006-01-02") + ".log" |
| 201 | + fileName := path.Join(logFilePath, logFileName) |
| 202 | + if _, err := os.Stat(fileName); err != nil { |
| 203 | + if _, err := os.Create(fileName); err != nil { |
| 204 | + log.Println(err.Error()) |
| 205 | + return |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + logger := hertzslog.NewLogger() |
| 210 | + // set filename to date |
| 211 | + lumberjackLogger := &lumberjack.Logger{ |
| 212 | + Filename: fileName, |
| 213 | + MaxSize: 20, // A file can be up to 20M. |
| 214 | + MaxBackups: 5, // Save up to 5 files at the same time |
| 215 | + MaxAge: 10, // A file can be saved for up to 10 days. |
| 216 | + Compress: true, // Compress with gzip. |
| 217 | + } |
| 218 | + |
| 219 | + logger.SetOutput(lumberjackLogger) |
| 220 | + logger.SetLevel(hlog.LevelDebug) |
| 221 | + |
| 222 | + hlog.SetLogger(logger) |
| 223 | + |
| 224 | + h.GET("/hello", func(ctx context.Context, c *app.RequestContext) { |
| 225 | + hlog.Info("Hello, hertz") |
| 226 | + c.String(consts.StatusOK, "Hello hertz!") |
| 227 | + }) |
| 228 | + |
| 229 | + h.Spin() |
| 230 | +} |
| 231 | +``` |
| 232 | + |
| 233 | +For more details on how to adapt the interface of hlog, see [hertz-contrib/logger/slog](https://github.com/hertz-contrib/logger/tree/main/slog) |
0 commit comments