aboutsummaryrefslogtreecommitdiffstats
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
parentae723466f745347c931a8a8bd9ff6dbf5237ece3 (diff)
Added -logfile option.
-rw-r--r--lib/webserver/server.go18
-rw-r--r--main.go28
2 files changed, 36 insertions, 10 deletions
diff --git a/lib/webserver/server.go b/lib/webserver/server.go
index 8fd7e44..7649cce 100644
--- a/lib/webserver/server.go
+++ b/lib/webserver/server.go
@@ -2,6 +2,7 @@ package webserver
import (
"fmt"
+ "log"
"net/http"
"os"
@@ -36,20 +37,21 @@ const indexFilename = "index.html"
const cookieMaxAge = "3600"
// New creates an instance of Server
-func New(cfg *xdsconfig.Config, mfolder *model.Folder, sdks *crosssdk.SDKs, log *logrus.Logger) *Server {
+func New(cfg *xdsconfig.Config, mfolder *model.Folder, sdks *crosssdk.SDKs, logr *logrus.Logger) *Server {
// Setup logging for gin router
- if log.Level == logrus.DebugLevel {
+ if logr.Level == logrus.DebugLevel {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
- // TODO
- // - try to bind gin DefaultWriter & DefaultErrorWriter to logrus logger
- // - try to fix pb about isTerminal=false when out is in VSC Debug Console
- //gin.DefaultWriter = ??
- //gin.DefaultErrorWriter = ??
+ // Redirect gin logs into logrus logger
+ gin.DefaultWriter = logr.Out
+ gin.DefaultErrorWriter = logr.Out
+ log.SetOutput(logr.Out)
+
+ // FIXME - fix pb about isTerminal=false when out is in VSC Debug Console
// Creates gin router
r := gin.New()
@@ -63,7 +65,7 @@ func New(cfg *xdsconfig.Config, mfolder *model.Folder, sdks *crosssdk.SDKs, log
sessions: nil,
mfolder: mfolder,
sdks: sdks,
- log: log,
+ log: logr,
stop: make(chan struct{}),
}
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