NewLogger() creates a new Logger instance with the specified configuration and optional data directory path
(config LoggerConfig, dataDirPath ...string)
| 200 | |
| 201 | // NewLogger() creates a new Logger instance with the specified configuration and optional data directory path |
| 202 | func NewLogger(config LoggerConfig, dataDirPath ...string) LoggerI { |
| 203 | if config.Out == nil { |
| 204 | if dataDirPath == nil || dataDirPath[0] == "" { |
| 205 | dataDirPath = make([]string, 1) |
| 206 | dataDirPath[0] = DefaultDataDirPath() |
| 207 | } |
| 208 | logPath := filepath.Join(dataDirPath[0], LogDirectory, LogFileName) |
| 209 | if _, err := os.Stat(logPath); errors.Is(err, os.ErrNotExist) { |
| 210 | if err = os.MkdirAll(filepath.Join(dataDirPath[0], LogDirectory), os.ModePerm); err != nil { |
| 211 | panic(err) |
| 212 | } |
| 213 | } |
| 214 | logFile := &lumberjack.Logger{ |
| 215 | Filename: logPath, |
| 216 | MaxSize: 1, // megabyte |
| 217 | MaxBackups: 1500, |
| 218 | MaxAge: 14, // days |
| 219 | Compress: true, |
| 220 | } |
| 221 | config.Out = io.MultiWriter(os.Stdout, logFile) |
| 222 | } |
| 223 | var slogger *slog.Logger |
| 224 | if config.JSON { |
| 225 | slogger = slog.New(slog.NewJSONHandler(config.Out, &slog.HandlerOptions{ |
| 226 | Level: slog.Level(config.Level), |
| 227 | })) |
| 228 | } else { |
| 229 | slogger = slog.New(slog.NewTextHandler(config.Out, &slog.HandlerOptions{ |
| 230 | Level: slog.Level(config.Level), |
| 231 | })) |
| 232 | } |
| 233 | return &Logger{ |
| 234 | config: config, |
| 235 | slog: slogger, |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // NewDefaultLogger() creates a Logger with default settings, logging at the Debug level to stdout |
| 240 | func NewDefaultLogger() LoggerI { |