Redirect HTTP and Gin server logs into a file (xds-server-verbose.log).
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Thu, 24 Aug 2017 19:23:40 +0000 (21:23 +0200)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Thu, 24 Aug 2017 19:28:28 +0000 (21:28 +0200)
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
.vscode/launch.json
glide.yaml
lib/syncthing/st.go
lib/webserver/server.go
lib/xdsconfig/config.go
main.go

index 3637b39..5583251 100644 (file)
@@ -16,7 +16,7 @@
             "args": ["-log", "debug", "-c", "config.json.in"],
             "showLog": false
         },
-{
+        {
             "name": "XDS-Server local dev",
             "type": "go",
             "request": "launch",
             },
             "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
         }
-
     ]
 }
index 5d813f3..e017281 100644 (file)
@@ -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
index 10210a4..b622970 100644 (file)
@@ -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 {
index 5183208..8639b66 100644 (file)
@@ -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
 
index a3e5a7e..82ca97f 100644 (file)
@@ -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 (file)
--- 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