comparison pkg/config/config.go @ 5490:5f47eeea988d logging

Use own logging package.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 Sep 2021 17:45:39 +0200
parents 0919946f624b
children 1222b777f51f
comparison
equal deleted inserted replaced
5488:a726a92ea5c9 5490:5f47eeea988d
15 package config 15 package config
16 16
17 import ( 17 import (
18 "crypto/sha256" 18 "crypto/sha256"
19 "fmt" 19 "fmt"
20 "log" 20 lg "log"
21 "sync" 21 "sync"
22 "time" 22 "time"
23 23
24 homedir "github.com/mitchellh/go-homedir" 24 homedir "github.com/mitchellh/go-homedir"
25 25
26 "github.com/spf13/cobra" 26 "github.com/spf13/cobra"
27 "github.com/spf13/viper" 27 "github.com/spf13/viper"
28 28
29 "gemma.intevation.de/gemma/pkg/common" 29 "gemma.intevation.de/gemma/pkg/common"
30 "gemma.intevation.de/gemma/pkg/log"
30 ) 31 )
31 32
32 // This is not part of the persistent config. 33 // This is not part of the persistent config.
33 var configFile string 34 var configFile string
34 35
122 // SOAPTimeout is the timeout till a SOAP request is canceled. 123 // SOAPTimeout is the timeout till a SOAP request is canceled.
123 func SOAPTimeout() time.Duration { return viper.GetDuration("soap-timeout") } 124 func SOAPTimeout() time.Duration { return viper.GetDuration("soap-timeout") }
124 125
125 // ReportPath is a path where report templates are stored. 126 // ReportPath is a path where report templates are stored.
126 func ReportPath() string { return viper.GetString("report-path") } 127 func ReportPath() string { return viper.GetString("report-path") }
128
129 // LogFile is the path to the log file.
130 func LogFile() string { return viper.GetString("log-file") }
131
132 // LogLevel is the log level of the application.
133 func LogLevel() log.LogLevel { return log.ParseLogLevel(viper.GetString("log-level")) }
127 134
128 var ( 135 var (
129 proxyKeyOnce sync.Once 136 proxyKeyOnce sync.Once
130 proxyKey []byte 137 proxyKey []byte
131 proxyPrefixOnce sync.Once 138 proxyPrefixOnce sync.Once
190 func SessionTimeout() time.Duration { 197 func SessionTimeout() time.Duration {
191 fetchTimeout := func() { 198 fetchTimeout := func() {
192 if sessionTimeout == 0 { 199 if sessionTimeout == 0 {
193 sessionTimeout = viper.GetDuration("session-timeout") 200 sessionTimeout = viper.GetDuration("session-timeout")
194 if sessionTimeout <= 0 { 201 if sessionTimeout <= 0 {
195 log.Println("warn: non-positive session-timeout configured.") 202 lg.Println("warn: non-positive session-timeout configured.")
196 } 203 }
197 } 204 }
198 } 205 }
199 sessionTimeoutOnce.Do(fetchTimeout) 206 sessionTimeoutOnce.Do(fetchTimeout)
200 return sessionTimeout 207 return sessionTimeout
294 str("published-config", "", "path to a config file served to client.") 301 str("published-config", "", "path to a config file served to client.")
295 302
296 str("report-path", "", "path to a report templates.") 303 str("report-path", "", "path to a report templates.")
297 304
298 d("soap-timeout", 3*time.Minute, "Timeout till a SOAP request is canceled.") 305 d("soap-timeout", 3*time.Minute, "Timeout till a SOAP request is canceled.")
306
307 str("log-file", "", "path to a file to log to.")
308 str("log-level", log.InfoLogLevel.String(), "path to a file to log to.")
309
299 } 310 }
300 311
301 var ( 312 var (
302 configCond = sync.NewCond(new(sync.Mutex)) 313 configCond = sync.NewCond(new(sync.Mutex))
303 configReady bool 314 configReady bool
330 viper.SetConfigFile(configFile) 341 viper.SetConfigFile(configFile)
331 } else { 342 } else {
332 // Find home directory. 343 // Find home directory.
333 home, err := homedir.Dir() 344 home, err := homedir.Dir()
334 if err != nil { 345 if err != nil {
335 log.Fatalf("error: %v\n", err) 346 lg.Fatalf("error: %v\n", err)
336 } 347 }
337 348
338 // Search config in home directory with name ".cobra" (without extension). 349 // Search config in home directory with name ".cobra" (without extension).
339 viper.AddConfigPath(home) 350 viper.AddConfigPath(home)
340 viper.SetConfigName(".gemma") 351 viper.SetConfigName(".gemma")
342 if err := viper.ReadInConfig(); err != nil { 353 if err := viper.ReadInConfig(); err != nil {
343 if _, ok := err.(viper.ConfigFileNotFoundError); ok && configFile == "" { 354 if _, ok := err.(viper.ConfigFileNotFoundError); ok && configFile == "" {
344 // Don't bother if not found. 355 // Don't bother if not found.
345 return 356 return
346 } 357 }
347 log.Fatalf("Can't read config: %v\n", err) 358 lg.Fatalf("Can't read config: %v\n", err)
348 } 359 }
349 } 360 }