Slight code rearrangement. Capitalise log levels.
All checks were successful
Build Docker image / build (push) Successful in 57s
All checks were successful
Build Docker image / build (push) Successful in 57s
This commit is contained in:
parent
3986ae1c1e
commit
396b7d67b4
1 changed files with 25 additions and 26 deletions
51
main.go
51
main.go
|
@ -75,30 +75,6 @@ type Payload struct {
|
||||||
var db *sql.DB
|
var db *sql.DB
|
||||||
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
var logger = slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
|
|
||||||
// Helper function to map environment variable to slog.Level
|
|
||||||
func getLogLevelFromEnv(defaultLevel slog.Level) slog.Level {
|
|
||||||
logLevelStr := os.Getenv("LOG_LEVEL")
|
|
||||||
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,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Initial logger setup
|
// Initial logger setup
|
||||||
slog.SetDefault(logger)
|
slog.SetDefault(logger)
|
||||||
|
@ -111,7 +87,6 @@ func main() {
|
||||||
// Update the logger based on the LOG_LEVEL environment variable
|
// Update the logger based on the LOG_LEVEL environment variable
|
||||||
logLevel := getLogLevelFromEnv(slog.LevelInfo) // Default to info level
|
logLevel := getLogLevelFromEnv(slog.LevelInfo) // Default to info level
|
||||||
logger = createLoggerWithLevel(logLevel)
|
logger = createLoggerWithLevel(logLevel)
|
||||||
slog.SetDefault(logger) // Update the default logger to the new one with the specified log level
|
|
||||||
|
|
||||||
// Example usage of the logger with the new log level
|
// Example usage of the logger with the new log level
|
||||||
logger.Info("Logger initialized with dynamic log level")
|
logger.Info("Logger initialized with dynamic log level")
|
||||||
|
@ -149,7 +124,29 @@ func main() {
|
||||||
select {}
|
select {}
|
||||||
}
|
}
|
||||||
|
|
||||||
var prevDt1, prevDt2, prevRt1, prevRt2, prevG int
|
// Helper function to map environment variable to slog.Level
|
||||||
|
func getLogLevelFromEnv(defaultLevel slog.Level) slog.Level {
|
||||||
|
logLevelStr := os.Getenv("LOG_LEVEL")
|
||||||
|
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,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
func updateFieldIfChanged(currentValue *int, previousValue *int) (*int, bool) {
|
func updateFieldIfChanged(currentValue *int, previousValue *int) (*int, bool) {
|
||||||
if currentValue != nil && *currentValue == *previousValue {
|
if currentValue != nil && *currentValue == *previousValue {
|
||||||
|
@ -171,6 +168,8 @@ func safeDerefInt(ptr *int) string {
|
||||||
return "nil" // Return a string indicating the value is nil
|
return "nil" // Return a string indicating the value is nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var prevDt1, prevDt2, prevRt1, prevRt2, prevG int
|
||||||
|
|
||||||
func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
func mqttMessageHandler(client mqtt.Client, msg mqtt.Message) {
|
||||||
// Parse JSON payload
|
// Parse JSON payload
|
||||||
var payload Payload
|
var payload Payload
|
||||||
|
|
Loading…
Reference in a new issue