diff options
Diffstat (limited to 'internal/log')
| -rw-r--r-- | internal/log/log.go | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/internal/log/log.go b/internal/log/log.go index 6a48ad7..91eaed8 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -9,12 +9,11 @@ import ( "path" ) -// Init initializes the logger by setting a max level 'level' and a directory 'directory' where the log should be stored -// internally, it uses slog, so any package just imports slog -// This can be done as this function sets the logger as the default logger in slog -// It returns the log file and the error -// This log file should be closed at the end -func Init(lvl slog.Level, dir string) (*FileRotater, error) { +type Logger struct { + fr *FileRotater +} + +func (l *Logger) Init(dir string) (*slog.Logger, error) { err := os.MkdirAll(dir, 0o700) if err != nil { return nil, err @@ -25,11 +24,14 @@ func Init(lvl slog.Level, dir string) (*FileRotater, error) { if err != nil { return nil, fmt.Errorf("failed creating log rotater: %w", err) } + l.fr = fr multi := io.MultiWriter(os.Stdout, fr) handler := slog.NewTextHandler(multi, &slog.HandlerOptions{ - Level: lvl, + Level: slog.LevelDebug, }) - logger := slog.New(handler) - slog.SetDefault(logger) - return fr, nil + return slog.New(handler), nil +} + +func (l *Logger) Close() error { + return l.fr.Close() } |
