diff --git a/pkg/log/logging.go b/pkg/log/logging.go index 111bfd029..0b4754189 100644 --- a/pkg/log/logging.go +++ b/pkg/log/logging.go @@ -27,6 +27,11 @@ import ( // logInstance is the instance of the logger which is used under the hood to log var logInstance *slog.Logger +// IsInitialized returns true if the logger has been initialized. +func IsInitialized() bool { + return logInstance != nil +} + // logpath is the path in which log files will be written. // This value is a mere fallback for other modules that could but shouldn't be used before calling ConfigureLogger var logPath = "." diff --git a/pkg/mail/domain.go b/pkg/mail/domain.go index 2bfc58c51..439e0dd7c 100644 --- a/pkg/mail/domain.go +++ b/pkg/mail/domain.go @@ -35,9 +35,13 @@ func GetMailDomain() string { } } if hostname, err := os.Hostname(); err == nil && hostname != "" { - log.Warningf("Could not determine mail domain from public URL, falling back to hostname %q", hostname) + if log.IsInitialized() { + log.Warningf("Could not determine mail domain from public URL, falling back to hostname %q", hostname) + } return hostname } - log.Warningf("Could not determine mail domain from public URL or hostname, falling back to %q", "vikunja") + if log.IsInitialized() { + log.Warningf("Could not determine mail domain from public URL or hostname, falling back to %q", "vikunja") + } return "vikunja" } diff --git a/pkg/models/notifications_test.go b/pkg/models/notifications_test.go index 526f39951..94c2aca83 100644 --- a/pkg/models/notifications_test.go +++ b/pkg/models/notifications_test.go @@ -17,6 +17,7 @@ package models import ( + "os" "testing" "code.vikunja.io/api/pkg/config" @@ -33,7 +34,11 @@ func TestGetThreadID(t *testing.T) { t.Run("default domain when no public URL", func(t *testing.T) { config.ServicePublicURL.Set("") threadID := getThreadID(123) - assert.Equal(t, "", threadID) + expectedDomain := "vikunja" + if hostname, err := os.Hostname(); err == nil && hostname != "" { + expectedDomain = hostname + } + assert.Equal(t, "", threadID) }) t.Run("simple domain without port", func(t *testing.T) { @@ -73,7 +78,11 @@ func TestGetThreadID(t *testing.T) { t.Run("invalid URL falls back to default", func(t *testing.T) { config.ServicePublicURL.Set("not a valid url") threadID := getThreadID(333) - assert.Equal(t, "", threadID) + expectedDomain := "vikunja" + if hostname, err := os.Hostname(); err == nil && hostname != "" { + expectedDomain = hostname + } + assert.Equal(t, "", threadID) }) t.Run("URL with path", func(t *testing.T) {