Some checks failed
Linting and tests / Linting (push) Failing after 37s
46 lines
1000 B
Go
46 lines
1000 B
Go
package application
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
defaultLogLevel slog.Level = slog.LevelInfo
|
|
|
|
logLevelEnvVar = "BUNKERD_LOG_LEVEL"
|
|
)
|
|
|
|
func (a *Application) initializeLogger() {
|
|
logLevel := defaultLogLevel
|
|
|
|
logLevelAsString, found := os.LookupEnv(logLevelEnvVar)
|
|
if found {
|
|
switch strings.ToLower(logLevelAsString) {
|
|
case "debug":
|
|
logLevel = slog.LevelDebug
|
|
case "info":
|
|
logLevel = slog.LevelInfo
|
|
case "warn":
|
|
logLevel = slog.LevelWarn
|
|
case "error":
|
|
logLevel = slog.LevelError
|
|
}
|
|
}
|
|
|
|
slog.Warn("Setting log level.", "level", logLevel.String())
|
|
|
|
a.baseLogger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
AddSource: true,
|
|
Level: logLevel,
|
|
}))
|
|
|
|
a.appLogger = a.baseLogger.With("module", "application")
|
|
}
|
|
|
|
// NewLogger creates new sub-instance of base logger and adds some additional data to it for persistent output.
|
|
func (a *Application) NewLogger(withs ...interface{}) *slog.Logger {
|
|
return a.baseLogger.With(withs...)
|
|
}
|