summaryrefslogtreecommitdiff
path: root/internal/log/log.go
diff options
context:
space:
mode:
authorSimon Ruderich <simon.ruderich@fau.de>2025-11-25 08:08:19 +0100
committerSimon Ruderich <simon.ruderich@fau.de>2025-12-15 19:06:52 +0100
commitc9fa5eb4ab6cb575408882d2fbc85903c4066ba0 (patch)
treed814249c46c1b644bca485c4c3fc3fa88c0aaef9 /internal/log/log.go
parent4b2ba41dd46f4a9ae80b190fd3599653b81c4717 (diff)
client: add Logger interface to give programs more control
See https://codeberg.org/eduVPN/eduvpn-common/issues/102
Diffstat (limited to 'internal/log/log.go')
-rw-r--r--internal/log/log.go22
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()
}