summaryrefslogtreecommitdiffstats
path: root/main.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-08-08 16:59:56 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-08-08 16:59:56 +0200
commit0262f5bef6ff67e77b844a04733c57740fba9f00 (patch)
treef361629308a116094f4b4c1901c501e82d454cb8 /main.go
parentae723466f745347c931a8a8bd9ff6dbf5237ece3 (diff)
Added -logfile option.
Diffstat (limited to 'main.go')
-rw-r--r--main.go28
1 files changed, 26 insertions, 2 deletions
diff --git a/main.go b/main.go
index fd1480e..060a927 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"os/signal"
+ "path/filepath"
"strings"
"syscall"
"time"
@@ -115,8 +116,25 @@ func xdsApp(cliCtx *cli.Context) error {
}
ctx.Config = cfg
- // TODO allow to redirect stdout/sterr into logs file
- //logFilename := filepath.Join(ctx.Config.FileConf.LogsDir + "xds-server.log")
+ // Logs redirected into a file when logsDir is set
+ logfilename := cliCtx.GlobalString("logfile")
+ if ctx.Config.FileConf.LogsDir != "" && logfilename != "stdout" {
+ if logfilename == "" {
+ logfilename = "xds-server.log"
+ }
+ // is it an absolute path ?
+ logFile := logfilename
+ if logfilename[0] == '.' || logfilename[0] != '/' {
+ logFile = filepath.Join(ctx.Config.FileConf.LogsDir, logfilename)
+ }
+ fmt.Printf("Logging file: %s\n", logFile)
+ fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
+ if err != nil {
+ msgErr := fmt.Sprintf("Cannot create log file %s", logFile)
+ return cli.NewExitError(msgErr, int(syscall.EPERM))
+ }
+ ctx.Log.Out = fdL
+ }
// FIXME - add a builder interface and support other builder type (eg. native)
builderType := "syncthing"
@@ -247,6 +265,12 @@ func main() {
Usage: "logging level (supported levels: panic, fatal, error, warn, info, debug)\n\t",
EnvVar: "LOG_LEVEL",
},
+ cli.StringFlag{
+ Name: "logfile",
+ Value: "stdout",
+ Usage: "filename where logs will be redirected (default stdout)\n\t",
+ EnvVar: "LOG_FILENAME",
+ },
}
// only one action: Web Server