make log file a CLI param; default to stdout if none is provided
This commit is contained in:
parent
1c208e5946
commit
7bd7cd3aab
@ -110,6 +110,7 @@ func init() {
|
||||
viper.AutomaticEnv()
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file location")
|
||||
rootCmd.PersistentFlags().String("logfile", "", "file path for logging")
|
||||
rootCmd.PersistentFlags().String("database-name", "vulcanize_public", "database name")
|
||||
rootCmd.PersistentFlags().Int("database-port", 5432, "database port")
|
||||
rootCmd.PersistentFlags().String("database-hostname", "localhost", "database hostname")
|
||||
@ -122,6 +123,7 @@ func init() {
|
||||
rootCmd.PersistentFlags().String("exporter-name", "exporter", "name of exporter plugin")
|
||||
rootCmd.PersistentFlags().String("log-level", log.InfoLevel.String(), "Log level (trace, debug, info, warn, error, fatal, panic")
|
||||
|
||||
viper.BindPFlag("logfile", rootCmd.PersistentFlags().Lookup("logfile"))
|
||||
viper.BindPFlag("database.name", rootCmd.PersistentFlags().Lookup("database-name"))
|
||||
viper.BindPFlag("database.port", rootCmd.PersistentFlags().Lookup("database-port"))
|
||||
viper.BindPFlag("database.hostname", rootCmd.PersistentFlags().Lookup("database-hostname"))
|
||||
|
19
main.go
19
main.go
@ -3,6 +3,8 @@ package main
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/cmd"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -12,13 +14,18 @@ func main() {
|
||||
logrus.SetFormatter(&logrus.TextFormatter{
|
||||
FullTimestamp: true,
|
||||
})
|
||||
file, err := os.OpenFile("vulcanizedb.log",
|
||||
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err == nil {
|
||||
logrus.SetOutput(file)
|
||||
logfile := viper.GetString("logfile")
|
||||
if logfile != "" {
|
||||
file, err := os.OpenFile(logfile,
|
||||
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
||||
if err == nil {
|
||||
logrus.SetOutput(file)
|
||||
} else {
|
||||
logrus.SetOutput(os.Stdout)
|
||||
logrus.Info("Failed to log to file, using default stdout")
|
||||
}
|
||||
} else {
|
||||
logrus.Info("Failed to log to file, using default stderr")
|
||||
logrus.SetOutput(os.Stdout)
|
||||
}
|
||||
|
||||
cmd.Execute()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user