From c9fa5eb4ab6cb575408882d2fbc85903c4066ba0 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Tue, 25 Nov 2025 08:08:19 +0100 Subject: client: add Logger interface to give programs more control See https://codeberg.org/eduVPN/eduvpn-common/issues/102 --- internal/log/log.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'internal') 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() } -- cgit v1.2.3