From 347bd1674bbf67ccb6209951a4bf8f2971715532 Mon Sep 17 00:00:00 2001 From: Sebastien Douheret Date: Thu, 24 Aug 2017 21:23:40 +0200 Subject: [PATCH] Redirect HTTP and Gin server logs into a file (xds-server-verbose.log). Signed-off-by: Sebastien Douheret --- .vscode/launch.json | 19 +------------------ glide.yaml | 2 +- lib/syncthing/st.go | 5 ++++- lib/webserver/server.go | 8 ++++---- lib/xdsconfig/config.go | 8 +++++--- main.go | 36 ++++++++++++++++++++++++------------ 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 3637b39..5583251 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ "args": ["-log", "debug", "-c", "config.json.in"], "showLog": false }, -{ + { "name": "XDS-Server local dev", "type": "go", "request": "launch", @@ -31,23 +31,6 @@ }, "args": ["-log", "debug", "-c", "__config_local_dev.json"], "showLog": false - }, - { - "name": "XDS-Server IN DOCKER", - "type": "go", - "request": "launch", - "mode": "debug", - "port": 22000, - "host": "172.17.0.2", - "remotePath": "/xds/src/github.com/iotbzh/xds-server/bin/xds-server", - "program": "${workspaceRoot}", - "env": { - "GOPATH": "${workspaceRoot}/../../../..:${env:GOPATH}", - "ROOT_DIR": "${workspaceRoot}/../../../.." - }, - "args": [], - "showLog": true } - ] } diff --git a/glide.yaml b/glide.yaml index 5d813f3..e017281 100644 --- a/glide.yaml +++ b/glide.yaml @@ -25,7 +25,7 @@ import: - package: github.com/satori/go.uuid version: ^1.1.0 - package: github.com/iotbzh/xds-common - version: 363bac39b844 + version: 4b8e35b6786b subpackages: - golib/common - golib/eows diff --git a/lib/syncthing/st.go b/lib/syncthing/st.go index 10210a4..b622970 100644 --- a/lib/syncthing/st.go +++ b/lib/syncthing/st.go @@ -317,7 +317,10 @@ func (s *SyncThing) Connect() error { return fmt.Errorf("ERROR: cannot connect to Syncthing (null client)") } - s.client.SetLogger(s.log) + // Redirect HTTP log into a file + s.client.SetLogLevel(s.conf.Log.Level.String()) + s.client.LoggerPrefix = "SYNCTHING: " + s.client.LoggerOut = s.conf.LogVerboseOut s.MyID, err = s.IDGet() if err != nil { diff --git a/lib/webserver/server.go b/lib/webserver/server.go index 5183208..8639b66 100644 --- a/lib/webserver/server.go +++ b/lib/webserver/server.go @@ -46,10 +46,10 @@ func New(cfg *xdsconfig.Config, mfolders *model.Folders, sdks *crosssdk.SDKs, lo gin.SetMode(gin.ReleaseMode) } - // Redirect gin logs into logrus logger - gin.DefaultWriter = logr.Out - gin.DefaultErrorWriter = logr.Out - log.SetOutput(logr.Out) + // Redirect gin logs into another logger (LogVerboseOut may be stderr or a file) + gin.DefaultWriter = cfg.LogVerboseOut + gin.DefaultErrorWriter = cfg.LogVerboseOut + log.SetOutput(cfg.LogVerboseOut) // FIXME - fix pb about isTerminal=false when out is in VSC Debug Console diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index a3e5a7e..82ca97f 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -2,6 +2,7 @@ package xdsconfig import ( "fmt" + "io" "os" "github.com/Sirupsen/logrus" @@ -17,9 +18,10 @@ type Config struct { Builder BuilderConfig `json:"builder"` // Private (un-exported fields in REST GET /config route) - Options Options `json:"-"` - FileConf FileConfig `json:"-"` - Log *logrus.Logger `json:"-"` + Options Options `json:"-"` + FileConf FileConfig `json:"-"` + Log *logrus.Logger `json:"-"` + LogVerboseOut io.Writer `json:"-"` } // Options set at the command line diff --git a/main.go b/main.go index 65ab7a0..4fd49e9 100644 --- a/main.go +++ b/main.go @@ -117,22 +117,34 @@ func xdsApp(cliCtx *cli.Context) error { // 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) + ctx.Config.LogVerboseOut = os.Stderr + if ctx.Config.FileConf.LogsDir != "" { + if 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 } - fmt.Printf("Logging file: %s\n", logFile) - fdL, err := os.OpenFile(logFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + + logFileHTTPReq := filepath.Join(ctx.Config.FileConf.LogsDir, "xds-server-verbose.log") + fmt.Printf("Logging file for HTTP requests: %s\n", logFileHTTPReq) + fdLH, err := os.OpenFile(logFileHTTPReq, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) if err != nil { - msgErr := fmt.Sprintf("Cannot create log file %s", logFile) + msgErr := fmt.Sprintf("Cannot create log file %s", logFileHTTPReq) return cli.NewExitError(msgErr, int(syscall.EPERM)) } - ctx.Log.Out = fdL + ctx.Config.LogVerboseOut = fdLH } // Create syncthing instance when section "syncthing" is present in config.json -- 2.16.6