Skip to content

Commit

Permalink
upd(logger): handle cases that generate log files during testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonish committed Dec 23, 2024
1 parent b01ed79 commit 41bcc5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ var instance *slog.Logger
var programLevel = new(slog.LevelVar)

func init() {
//? Do not initialize logs during testing.
if os.Getenv("LOGGING_TEST") == "true" {
testH := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: programLevel})
instance = slog.New(testH)
slog.SetDefault(instance)
return
}

logFile, err := goLogger.NewLogger().SetRollingDaily(model.LOGS_DIR, model.LOG_FILENAME)
if err != nil {
slog.New(slog.NewTextHandler(os.Stdout, nil)).Error("unable to create log file", "error", err)
Expand Down
3 changes: 3 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

export LOGGING_TEST=true && go test ./... -count=1

0 comments on commit 41bcc5b

Please sign in to comment.