feat: introduce logger to config
All checks were successful
CI / test (pull_request) Successful in 37s
All checks were successful
CI / test (pull_request) Successful in 37s
This commit is contained in:
15
main.go
15
main.go
@@ -2,6 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"realdnydns/pkg/config"
|
"realdnydns/pkg/config"
|
||||||
"realdnydns/pkg/dnsProvider"
|
"realdnydns/pkg/dnsProvider"
|
||||||
@@ -15,12 +18,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: slog.LevelInfo,
|
||||||
|
}))
|
||||||
|
|
||||||
configClient := config.Config{}
|
configClient := config.Config{}
|
||||||
err := configClient.Load("config.yaml")
|
err := configClient.Load("config.yaml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logger.Error("Failed to load config file",
|
||||||
|
slog.String("error", err.Error()))
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if configClient.LogLevel != "" {
|
||||||
|
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
|
Level: slog.Level(config.LogLevelMap[strings.ToLower(configClient.LogLevel)]),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
var externalIpProvider externalIpProvider.ExternalIpProvider
|
var externalIpProvider externalIpProvider.ExternalIpProvider
|
||||||
switch configClient.ExternalIPProvider.Type {
|
switch configClient.ExternalIPProvider.Type {
|
||||||
case "plain":
|
case "plain":
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ package config
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
@@ -15,6 +17,7 @@ type Config struct {
|
|||||||
NotificationProvider NotificationProviderConfig `yaml:"notification_provider,omitempty"`
|
NotificationProvider NotificationProviderConfig `yaml:"notification_provider,omitempty"`
|
||||||
Domains []DomainConfig `yaml:"domains"`
|
Domains []DomainConfig `yaml:"domains"`
|
||||||
CheckInterval string `yaml:"check_interval"`
|
CheckInterval string `yaml:"check_interval"`
|
||||||
|
LogLevel string `yaml:"log_level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -22,6 +25,18 @@ const (
|
|||||||
ScheduledMode = "Scheduled"
|
ScheduledMode = "Scheduled"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var LogLevelMap = map[string]slog.Level{
|
||||||
|
"debug": slog.LevelDebug,
|
||||||
|
"info": slog.LevelInfo,
|
||||||
|
"warn": slog.LevelWarn,
|
||||||
|
"error": slog.LevelError,
|
||||||
|
}
|
||||||
|
|
||||||
|
func isValidLogLevel(level string) bool {
|
||||||
|
_, ok := LogLevelMap[strings.ToLower(level)]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
type DomainConfig struct {
|
type DomainConfig struct {
|
||||||
TLD string `yaml:"tld"`
|
TLD string `yaml:"tld"`
|
||||||
Subdomains []string `yaml:"subdomains"`
|
Subdomains []string `yaml:"subdomains"`
|
||||||
@@ -68,5 +83,9 @@ func (c *Config) validate() error {
|
|||||||
return errors.New("check interval must be set when mode is 'Scheduled'")
|
return errors.New("check interval must be set when mode is 'Scheduled'")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if c.LogLevel != "" && !isValidLogLevel(c.LogLevel) {
|
||||||
|
return fmt.Errorf("log level must be one of 'debug', 'info', 'warn', 'error', but got %s", c.LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user