import (
"fmt"
+ "log"
"net/http"
"os"
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()
sessions: nil,
mfolder: mfolder,
sdks: sdks,
- log: log,
+ log: logr,
stop: make(chan struct{}),
}
"os"
"os/exec"
"os/signal"
+ "path/filepath"
"strings"
"syscall"
"time"
}
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"
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