Merge pull request #416 from yuce/375-log-path-2

Adds log-path config
This commit is contained in:
Yuce Tekol 2017-04-05 21:57:33 +03:00 committed by GitHub
commit 5b790550d0
4 changed files with 38 additions and 5 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

@ -1,6 +1,7 @@
package cmd_test
import (
"errors"
"io/ioutil"
"strings"
"testing"
@ -23,6 +24,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
{
@ -73,7 +76,7 @@ data-dir = "` + actualDataDir + `"
},
// TEST 2
{
args: []string{"server"},
args: []string{"server", "--log-path", logFile.Name()},
env: map[string]string{"PILOSA_PROFILE.CPU_TIME": "1m"},
cfgFileContent: `
bind = "localhost:0"
@ -96,7 +99,16 @@ data-dir = "` + actualDataDir + `"
v.Check(cmd.Server.Config.AntiEntropy.Interval, pilosa.Duration(time.Minute*11))
v.Check(cmd.Server.CPUProfile, profFile.Name())
v.Check(cmd.Server.CPUTime, time.Minute)
return v.Error()
v.Check(cmd.Server.Config.LogPath, logFile.Name())
if v.Error() != nil {
return v.Error()
}
// confirm log file was written
info, err := logFile.Stat()
if err != nil || info.Size() == 0 {
return errors.New("Log file was not written!")
}
return nil
},
},
}

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)
@ -108,7 +116,17 @@ func normalizeHost(host string) (string, error) {
// Close shuts down the server.
func (m *Command) Close() error {
err := m.Server.Close()
var logErr error
serveErr := m.Server.Close()
logOutput := m.Server.LogOutput
if closer, ok := logOutput.(io.Closer); ok {
logErr = closer.Close()
}
close(m.Done)
return err
if serveErr != nil && logErr != nil {
return fmt.Errorf("closing server: '%v', closing logs: '%v'", serveErr, logErr)
} else if logErr != nil {
return logErr
}
return serveErr
}