add config log-level (#14)

* add config log-level

Signed-off-by: xiang <xiang13225080@163.com>

* add value

Signed-off-by: xiang <xiang13225080@163.com>
This commit is contained in:
WangXiang 2021-01-18 00:08:16 -06:00 committed by GitHub
parent fdd2220aa3
commit fa13339e97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -14,11 +14,15 @@
package ctl
import (
"github.com/pingcap/log"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/chaos-mesh/chaosd/cmd/chaosd/ctl/command"
)
var logLevel string
// CommandFlags are flags that used in all Commands
var rootCmd = &cobra.Command{
Use: "chaosd",
@ -26,6 +30,9 @@ var rootCmd = &cobra.Command{
}
func init() {
cobra.OnInitialize(setLogLevel)
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "", "", "the log level of chaosd, the value can be 'debug', 'info', 'warn' and 'error'")
rootCmd.AddCommand(
command.NewServerCommand(),
command.NewAttackCommand(),
@ -41,3 +48,13 @@ func Execute() {
command.ExitWithError(command.ExitError, err)
}
}
func setLogLevel() {
conf := &log.Config{Level: logLevel}
lg, r, err := log.InitLogger(conf)
if err != nil {
log.Error("fail to init log", zap.Error(err))
return
}
log.ReplaceGlobals(lg, r)
}