Adds log-path config

This commit is contained in:
Yuce Tekol 2017-04-05 16:09:43 +03:00
parent 261e327014
commit 5353448a92
4 changed files with 38 additions and 1 deletions

View file

@ -79,6 +79,7 @@ on the configured port.`,
flags.StringSliceVarP(&Server.Config.Cluster.Nodes, "cluster.hosts", "", []string{}, "Comma separated list of hosts in cluster.")
flags.DurationVarP((*time.Duration)(&Server.Config.Cluster.PollingInterval), "cluster.poll-interval", "", time.Minute, "Polling interval for cluster.") // TODO what actually is this?
flags.StringVarP(&Server.Config.Plugins.Path, "plugins.path", "", "", "Path to plugin directory.")
flags.StringVar(&Server.Config.LogPath, "log-path", "", "Log path")
flags.DurationVarP((*time.Duration)(&Server.Config.AntiEntropy.Interval), "anti-entropy.interval", "", time.Minute*10, "Interval at which to run anti-entropy routine.")
flags.StringVarP(&Server.CPUProfile, "profile.cpu", "", "", "Where to store CPU profile.")
flags.DurationVarP(&Server.CPUTime, "profile.cpu-time", "", 30*time.Second, "CPU profile duration.")

View file

@ -23,6 +23,8 @@ func TestServerConfig(t *testing.T) {
failErr(t, err, "making data dir")
profFile, err := ioutil.TempFile("", "")
failErr(t, err, "making temp file")
logFile, err := ioutil.TempFile("", "")
failErr(t, err, "making log file")
tests := []commandTest{
// TEST 0
{
@ -99,6 +101,24 @@ data-dir = "` + actualDataDir + `"
return v.Error()
},
},
// TEST 3 - test log path can be read from command line and it's priority is highest
{
args: []string{"server", "--log-path", logFile.Name()},
env: map[string]string{"PILOSA_LOG_PATH": "/tmp/mylog_env"},
cfgFileContent: `
log-path = "/tmp/mylog_cfg"
`,
validation: func() error {
v := validator{}
v.Check(cmd.Server.Config.LogPath, logFile.Name())
if err := v.Error(); err != nil {
return v.Error()
}
// check that the log file was created
_, err = logFile.Stat()
return err
},
},
}
// run server tests

View file

@ -26,6 +26,8 @@ type Config struct {
AntiEntropy struct {
Interval Duration `toml:"interval"`
} `toml:"anti-entropy"`
LogPath string `toml:"log-path"`
}
// NewConfig returns an instance of Config with default options.

View file

@ -68,7 +68,15 @@ func (m *Command) Run(args ...string) (err error) {
}
// Setup logging output.
m.Server.LogOutput = m.Stderr
if m.Config.LogPath == "" {
m.Server.LogOutput = m.Stderr
} else {
logFile, err := os.OpenFile(m.Config.LogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
m.Server.LogOutput = logFile
}
// Configure index.
fmt.Fprintf(m.Stderr, "Using data from: %s\n", m.Config.DataDir)
@ -109,6 +117,12 @@ func normalizeHost(host string) (string, error) {
// Close shuts down the server.
func (m *Command) Close() error {
err := m.Server.Close()
logOutput := m.Server.LogOutput
if logOutput != m.Stderr {
if file, ok := logOutput.(*os.File); ok {
file.Close()
}
}
close(m.Done)
return err
}