add zap for default logger

This commit is contained in:
zhiqiang feng
2020-12-28 11:06:36 +08:00
parent f43412b235
commit 2cbfbdbbca
9 changed files with 81 additions and 45 deletions
+20 -1
View File
@@ -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")
}
}