Add DEBUG log_level.
This commit is contained in:
parent
48d0382b0e
commit
b63c1e85d3
1 changed files with 31 additions and 0 deletions
31
main.go
31
main.go
|
@ -156,6 +156,7 @@ type Config struct {
|
||||||
OpenDTUPassword string `json:"opendtu_password"`
|
OpenDTUPassword string `json:"opendtu_password"`
|
||||||
TimescaleDB bool `json:"timescaledb"`
|
TimescaleDB bool `json:"timescaledb"`
|
||||||
TZ string `json:"tz"`
|
TZ string `json:"tz"`
|
||||||
|
LogLevel string `json:"log_level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
|
@ -240,6 +241,30 @@ func loadConfig() Config {
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to map environment variable to slog.Level
|
||||||
|
func getLogLevel(defaultLevel slog.Level) slog.Level {
|
||||||
|
logLevelStr := config.LogLevel
|
||||||
|
switch logLevelStr {
|
||||||
|
case "DEBUG":
|
||||||
|
return slog.LevelDebug
|
||||||
|
case "INFO":
|
||||||
|
return slog.LevelInfo
|
||||||
|
case "WARN":
|
||||||
|
return slog.LevelWarn
|
||||||
|
case "ERROR":
|
||||||
|
return slog.LevelError
|
||||||
|
default:
|
||||||
|
return defaultLevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to create a new logger with a specified log level
|
||||||
|
func createLoggerWithLevel(level slog.Level) *slog.Logger {
|
||||||
|
return slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: level,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
// Main program
|
// Main program
|
||||||
func main() {
|
func main() {
|
||||||
// Initial logger setup
|
// Initial logger setup
|
||||||
|
@ -248,6 +273,10 @@ func main() {
|
||||||
// Load the configuration
|
// Load the configuration
|
||||||
config := loadConfig()
|
config := loadConfig()
|
||||||
|
|
||||||
|
// Set the logLevel
|
||||||
|
logLevel := getLogLevel(slog.LevelInfo) // Default to info level
|
||||||
|
logger = createLoggerWithLevel(logLevel)
|
||||||
|
|
||||||
dbConnStr := config.DB
|
dbConnStr := config.DB
|
||||||
// Connect to PostgreSQL
|
// Connect to PostgreSQL
|
||||||
db, err := sql.Open("postgres", dbConnStr)
|
db, err := sql.Open("postgres", dbConnStr)
|
||||||
|
@ -262,6 +291,8 @@ func main() {
|
||||||
// Create WebSocket URL from config variable
|
// Create WebSocket URL from config variable
|
||||||
wsURL := "ws://" + config.OpenDTUAddress + "/livedata"
|
wsURL := "ws://" + config.OpenDTUAddress + "/livedata"
|
||||||
|
|
||||||
|
logger.Debug(wsURL)
|
||||||
|
|
||||||
// Create headers with optional Basic Auth
|
// Create headers with optional Basic Auth
|
||||||
headers := http.Header{}
|
headers := http.Header{}
|
||||||
if config.OpenDTUAuth {
|
if config.OpenDTUAuth {
|
||||||
|
|
Loading…
Reference in a new issue