add zap for default logger
This commit is contained in:
+20
-1
@@ -20,10 +20,14 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"it2000.com.cn/tele-recv/utils"
|
||||
)
|
||||
|
||||
const ModeName = "TELEGRAM_MODE"
|
||||
|
||||
var (
|
||||
cfgFile string
|
||||
device string
|
||||
@@ -47,6 +51,11 @@ Read the telegram from `,
|
||||
// has an action associated with it:
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
Mode = os.Getenv(ModeName)
|
||||
|
||||
logger *zap.Logger
|
||||
l *zap.SugaredLogger
|
||||
)
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
@@ -69,6 +78,14 @@ func init() {
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
if Mode == "PROD" {
|
||||
logger, _ = zap.NewProduction()
|
||||
} else {
|
||||
logger, _ = zap.NewDevelopment()
|
||||
}
|
||||
defer logger.Sync()
|
||||
l = logger.Sugar()
|
||||
utils.Log = l
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
@@ -93,7 +110,8 @@ func initConfig() {
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
// fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
l.Infof("Using config file : %s", viper.ConfigFileUsed())
|
||||
device = viper.GetString("serial.device")
|
||||
baudrate = viper.GetInt("serial.baudrate")
|
||||
bytesize = byte(viper.GetInt("serial.bytesize"))
|
||||
@@ -103,4 +121,5 @@ func initConfig() {
|
||||
dbInit = viper.GetBool("sqlite.init")
|
||||
socketAddress = viper.GetString("socket.address")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user