1
0
mirror of https://github.com/gluster/glusterd2.git synced 2026-02-05 12:45:38 +01:00

Initialize config and logging before main() starts to execute

initialize config followed by initializing logging before execution
of main() function will prevent redirecting of logs in init() func
of other packages to console.

Signed-off-by: Oshank Kumar <okumar@redhat.com>
This commit is contained in:
Oshank Kumar
2019-01-14 18:32:45 +05:30
committed by Aravinda VK
parent 9c6ee47f50
commit a37193deab
4 changed files with 67 additions and 73 deletions

View File

@@ -1,7 +1,6 @@
package main
package conf
import (
"encoding/json"
"errors"
"expvar"
"net"
@@ -73,6 +72,7 @@ func initFlags() {
// and flags which don't have default values
func setDefaults() error {
config.SetDefault("loglevel", "info")
config.SetDefault("hooksdir", config.GetString("localstatedir")+"/hooks")
if config.GetString("pidfile") == "" {
@@ -97,30 +97,18 @@ func setDefaults() error {
return nil
}
type valueType struct {
v interface{}
}
func (v valueType) String() string {
vb, _ := json.Marshal(v.v)
return string(vb)
}
func dumpConfigToLog() {
// DumpConfigToLog will dump all configuration in use
func DumpConfigToLog() {
if config.ConfigFileUsed() != "" {
log.WithField("file", config.ConfigFileUsed()).Info("loaded configuration from file")
}
l := log.NewEntry(log.StandardLogger())
for k, v := range config.AllSettings() {
expConfig.Set(k, valueType{v})
l = l.WithField(k, v)
}
l.Debug("running with configuration")
configs := config.AllSettings()
expConfig.Set("conf", expvar.Func(func() interface{} { return configs }))
log.WithFields(log.Fields(configs)).Debug("running with configuration")
}
// initConfig intializes GD2 configuration from various sources.
// Init intializes GD2 configuration from various sources.
// The order of preference is,
// - explicitly set configs using config.Set
// - flags, if set
@@ -128,9 +116,12 @@ func dumpConfigToLog() {
// - config file
// - defaults set using config.SetDefault
// - flag defaults
func initConfig() error {
func Init() error {
// Use config given by flags
config.BindPFlags(flag.CommandLine)
initFlags()
if err := config.BindPFlags(flag.CommandLine); err != nil {
return err
}
// Allow config values from environment environment variables.
// All options settable from the command line are available to be set this way.
@@ -145,32 +136,28 @@ func initConfig() error {
// If the file is not present panic.
// Limit config to toml only to avoid confusion with multiple config types
config.AddConfigPath(path.Join(defaultPathPrefix, "/etc/glusterd2"))
config.SetConfigName("glusterd2")
config.SetConfigType("toml")
confFile := config.GetString("config")
// If custom configuration is passed use it, if not try to use defaults
if confFile != "" {
// SetConfigFile explicitly defines the path, name and extension of the config file.
// Viper will use this and not check any of the config paths.
if confFile := config.GetString("config"); confFile != "" {
config.SetConfigFile(confFile)
} else {
config.AddConfigPath(path.Join(defaultPathPrefix, "/etc/glusterd2"))
config.SetConfigName("glusterd2")
}
if err := config.ReadInConfig(); err != nil {
// Ignore error if config file is not found, error out otherwise
if _, ok := err.(config.ConfigFileNotFoundError); ok {
log.WithError(err).
WithField("file", config.ConfigFileUsed()).
Warn("failed to load config from file")
} else {
log.WithError(err).
WithField("file", config.ConfigFileUsed()).
Error("failed to load config from file")
return err
}
}
// Finally initialize missing config with defaults
err := setDefaults()
// If custom configuration is passed use it, if not try to use defaults
err := config.ReadInConfig()
if _, ok := err.(config.ConfigFileNotFoundError); err != nil && !ok {
log.WithError(err).WithField("file", config.ConfigFileUsed()).Error("failed to load config from file")
return err
}
return err
return setDefaults()
}
func init() {
if err := Init(); err != nil {
log.WithError(err).Fatal("Failed to initialize config")
}
}

7
glusterd2/init.go Normal file
View File

@@ -0,0 +1,7 @@
package main
// initialise conf
import _ "github.com/gluster/glusterd2/glusterd2/conf"
// initialise logging
import _ "github.com/gluster/glusterd2/glusterd2/log"

20
glusterd2/log/log.go Normal file
View File

@@ -0,0 +1,20 @@
package log
import (
"github.com/gluster/glusterd2/pkg/logging"
log "github.com/sirupsen/logrus"
config "github.com/spf13/viper"
)
func init() {
var (
logLvl = config.GetString("loglevel")
logdir = config.GetString("logdir")
logFileName = config.GetString("logfile")
)
if err := logging.Init(logdir, logFileName, logLvl, true); err != nil {
log.WithError(err).Fatal("failed in initialise logging")
}
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/gluster/glusterd2/glusterd2/brickmux"
"github.com/gluster/glusterd2/glusterd2/commands/volumes"
"github.com/gluster/glusterd2/glusterd2/conf"
"github.com/gluster/glusterd2/glusterd2/daemon"
"github.com/gluster/glusterd2/glusterd2/events"
"github.com/gluster/glusterd2/glusterd2/gdctx"
@@ -36,47 +37,26 @@ import (
)
func main() {
var (
logLevel = config.GetString("loglevel")
logdir = config.GetString("logdir")
logFileName = config.GetString("logfile")
)
if err := gdctx.SetHostnameAndIP(); err != nil {
log.WithError(err).Fatal("Failed to get and set hostname or IP")
}
// Initialize and parse CLI flags
initFlags()
if showvers, _ := flag.CommandLine.GetBool("version"); showvers {
version.DumpVersionInfo()
return
}
logLevel, _ := flag.CommandLine.GetString("loglevel")
logdir, _ := flag.CommandLine.GetString("logdir")
logFileName, _ := flag.CommandLine.GetString("logfile")
if err := logging.Init(logdir, logFileName, logLevel, true); err != nil {
log.WithError(err).Fatal("Failed to initialize logging")
}
// Initialize GD2 config
if err := initConfig(); err != nil {
log.WithError(err).Fatal("Failed to initialize config")
}
logLevel2 := config.GetString("loglevel")
logdir2 := config.GetString("logdir")
logFileName2 := config.GetString("logfile")
if logLevel != logLevel2 || logdir != logdir2 || logFileName != logFileName2 {
if err := logging.Init(logdir2, logFileName2, logLevel2, true); err != nil {
log.WithError(err).Fatal("Failed to re-initialize logging")
}
}
log.WithFields(log.Fields{
"pid": os.Getpid(),
"version": version.GlusterdVersion,
}).Debug("Starting GlusterD")
dumpConfigToLog()
conf.DumpConfigToLog()
workdir := config.GetString("localstatedir")
if err := os.Chdir(workdir); err != nil {
@@ -183,9 +163,9 @@ func main() {
case unix.SIGHUP:
// Logrotate case, when Log rotated, Reopen the log file and
// re-initiate the logger instance.
if strings.ToLower(logFileName2) != "stderr" && strings.ToLower(logFileName2) != "stdout" && logFileName2 != "-" {
if strings.ToLower(logFileName) != "stderr" && strings.ToLower(logFileName) != "stdout" && logFileName != "-" {
log.Info("Received SIGHUP, Reloading log file")
if err := logging.Init(logdir2, logFileName2, logLevel2, true); err != nil {
if err := logging.Init(logdir, logFileName, logLevel, true); err != nil {
log.WithError(err).Fatal("Could not re-initialize logging")
}
}