Browse Source

feat(tool): for ekuiper kubernetes tool, add options for log pint location (#915)

Add two options in etc/cf.yaml file for log print
    * consoleLog: true/false
    * fileLog: true/false
Closes: #905

Signed-off-by: Jianxiang Ran <rxan_embedded@163.com>
SuperRxan 3 năm trước cách đây
mục cha
commit
43d60d13e1

+ 1 - 1
.github/workflows/build_packages.yaml

@@ -235,7 +235,7 @@ jobs:
             set -e -u -x
             docker run -d --name kuiper-kubernetes-tool lfedge/ekuiper-kubernetes-tool:$(git describe --tags --always)
             sleep 5
-            if [ "$(docker logs kuiper-kubernetes-tool)" != "Kuiper kubernetes tool is started successfully!"  ]; then exit 1; fi
+            if [[ "$(docker logs kuiper-kubernetes-tool)" != *"Kuiper kubernetes tool is started successfully!"*  ]]; then exit 1; fi
         - uses: docker/login-action@v1
           if: github.event_name == 'release'
           with:

+ 2 - 0
tools/kubernetes/README-CN.md

@@ -29,6 +29,8 @@ port: 9081  //eKuiper 端口
 timeout: 500  //执行一条命令超时时间(单位:毫秒)
 intervalTime: 60  //隔多久检查一次命令文件夹(单位:秒)
 ip: "127.0.0.1" //eKuiper ip地址
+consoleLog: true //是否需要打印log到console上
+fileLog: true   //是否需要打印log到log文件中,如果需要,log将会打印到logPath指定文件中
 logPath: "./log/kubernetes.log" //日志保存路径
 commandDir: "./sample/" //命令文件夹路径
 ```

+ 2 - 0
tools/kubernetes/README.md

@@ -30,6 +30,8 @@ port: 9081  //eKuiper port
 timeout: 500  //Timeout for executing a command (unit: ms)
 intervalTime: 60  //interval of Checking the command folder  (unit: seconds)
 ip: "127.0.0.1" //eKuiper ip adress
+consoleLog: true  //true|false, if it's set to true, then the log will be print to console
+fileLog: false  //true|false, if it's set to true, then the log will be print to log file "logPath"
 logPath: "./log/kubernetes.log" //Log save path
 commandDir: "./sample/" //Command folder path
 ```

+ 18 - 8
tools/kubernetes/conf/conf.go

@@ -19,6 +19,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"gopkg.in/yaml.v3"
+	"io"
 	"io/ioutil"
 	"net/http"
 	"os"
@@ -36,6 +37,8 @@ type (
 		Timeout      int    `yaml:"timeout"`
 		IntervalTime int    `yaml:"intervalTime"`
 		Ip           string `yaml:"ip"`
+		ConsoleLog   bool   `yaml:"consoleLog"`
+		FileLog      bool   `yaml:"fileLog"`
 		LogPath      string `yaml:"logPath"`
 		CommandDir   string `yaml:"commandDir"`
 	}
@@ -132,15 +135,22 @@ func (c *config) initLog() bool {
 		DisableColors: true,
 		FullTimestamp: true,
 	})
-
-	logFile, err := os.OpenFile(c.LogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
-	if err == nil {
-		Log.SetOutput(logFile)
-		return true
-	} else {
-		Log.Infof("Failed to log to file, using default stderr.")
-		return false
+	if c.FileLog {
+		logFile, err := os.OpenFile(c.LogPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
+		if err != nil {
+			fmt.Println("Failed to init log file settings..." + err.Error())
+			Log.Infof("Failed to log to file, using default stderr.")
+			return false
+		} else if c.ConsoleLog {
+			mw := io.MultiWriter(os.Stdout, logFile)
+			Log.SetOutput(mw)
+		} else if !c.ConsoleLog {
+			Log.SetOutput(logFile)
+		}
+	} else if c.ConsoleLog {
+		Log.SetOutput(os.Stdout)
 	}
+	return true
 }
 
 func (c *config) Init() bool {

+ 2 - 0
tools/kubernetes/etc/cf.yaml

@@ -2,5 +2,7 @@ port: 9081
 timeout: 500
 intervalTime: 30
 ip: "127.0.0.1"
+consoleLog: true
+fileLog: false
 logPath: "./log/kubeedge.log"
 commandDir: "./sample/"