@@ -3,13 +3,26 @@ package config
33import (
44 "flag"
55 "fmt"
6- "log"
6+ "log/slog"
7+ "os"
78 "sync"
89
910 "github.com/sebastianrakel/openvoxview/model"
1011 "github.com/spf13/viper"
1112)
1213
14+ type LogLevel string
15+ type LogFormat string
16+
17+ const (
18+ LOG_LEVEL_INFO LogLevel = "info"
19+ LOG_LEVEL_WARN LogLevel = "warn"
20+ LOG_LEVEL_ERROR LogLevel = "error"
21+ LOG_LEVEL_DEBUG LogLevel = "debug"
22+ LOG_FORMAT_JSON LogFormat = "json"
23+ LOG_FOMRAT_TEXT LogFormat = "text"
24+ )
25+
1326var configPath = flag .String ("config" , "" , "path to the config file" )
1427var printVersion = flag .Bool ("version" , false , "prints version" )
1528
@@ -51,6 +64,8 @@ type Config struct {
5164 ReadOnly bool `mapstructure:"readonly"`
5265 DeactivateNodes bool `mapstructure:"deactivate_nodes"`
5366 } `mapstructure:"puppetca"`
67+ LogLevel LogLevel `mapstructure:"log_level"`
68+ LogFormat LogFormat `mapstructure:"log_format"`
5469}
5570
5671func PrintVersion (version string ) bool {
@@ -74,7 +89,7 @@ func GetConfig() (*Config, error) {
7489 viper .AddConfigPath ("." )
7590
7691 if * configPath != "" {
77- log . Printf ("Using config: %s " , * configPath )
92+ slog . Info ("Using config" , "path " , * configPath )
7893 viper .SetConfigFile (* configPath )
7994 }
8095
@@ -90,6 +105,8 @@ func GetConfig() (*Config, error) {
90105 viper .SetDefault ("puppetca.readonly" , true )
91106 viper .SetDefault ("puppetca.deactivate_nodes" , false )
92107 viper .SetDefault ("ui_default_refresh_interval_in_seconds" , 300 )
108+ viper .SetDefault ("log_level" , "info" )
109+ viper .SetDefault ("log_format" , "text" )
93110
94111 viper .AutomaticEnv ()
95112
@@ -115,6 +132,8 @@ func GetConfig() (*Config, error) {
115132 viper .BindEnv ("puppetca.readonly" , "PUPPETCA_READONLY" )
116133 viper .BindEnv ("puppetca.deactivate_nodes" , "PUPPETCA_DEACTIVATE_NODES" )
117134 viper .BindEnv ("ui_default_refresh_interval_in_seconds" , "UI_DEFAULT_REFRESH_INTERVAL_IN_SECONDS" )
135+ viper .BindEnv ("log_level" , "LOG_LEVEL" )
136+ viper .BindEnv ("log_level" , "LOG_FORMAT" )
118137
119138 viper .ReadInConfig ()
120139
@@ -144,3 +163,31 @@ func (c *Config) GetPuppetCAAddress() string {
144163
145164 return fmt .Sprintf ("%s://%s:%d" , scheme , c .PuppetCA .Host , c .PuppetCA .Port )
146165}
166+
167+ func (c * Config ) GetLogLevel () slog.Level {
168+ switch c .LogLevel {
169+ case LOG_LEVEL_DEBUG :
170+ return slog .LevelDebug
171+ case LOG_LEVEL_WARN :
172+ return slog .LevelWarn
173+ case LOG_LEVEL_ERROR :
174+ return slog .LevelError
175+ default :
176+ return slog .LevelInfo
177+ }
178+ }
179+
180+ func (c * Config ) GetLogger () * slog.Logger {
181+ level := c .GetLogLevel ()
182+ switch c .LogFormat {
183+ case LOG_FORMAT_JSON :
184+ return slog .New (slog .NewJSONHandler (os .Stdout , & slog.HandlerOptions {
185+ Level : level ,
186+ }))
187+ default :
188+ return slog .New (slog .NewTextHandler (os .Stdout , & slog.HandlerOptions {
189+ Level : level ,
190+ }))
191+ }
192+
193+ }
0 commit comments