diff --git a/cmd/server.go b/cmd/server.go index 5d5035ba7..20de85a3f 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -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.") diff --git a/cmd/server_test.go b/cmd/server_test.go index d677d67a8..927ce8b66 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -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 }, }, } diff --git a/config.go b/config.go index e664c0771..8250c7f91 100644 --- a/config.go +++ b/config.go @@ -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. diff --git a/server/server.go b/server/server.go index cd9aa3cb0..469b5bc3b 100644 --- a/server/server.go +++ b/server/server.go @@ -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 }