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")
}
}
+7 -10
View File
@@ -16,8 +16,6 @@ limitations under the License.
package cmd
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
@@ -70,8 +68,7 @@ func start() {
// and then notify the program that it can finish.
go func() {
sig := <-sigs
fmt.Println()
fmt.Println(sig)
l.Info(sig)
utils.ServerRunning = false
utils.StopSocketServer()
done <- true
@@ -80,27 +77,27 @@ func start() {
// The program will wait here until it gets the
// expected signal (as indicated by the goroutine
// above sending a value on `done`) and then exit.
fmt.Println("awaiting signal")
l.Info("awaiting signal")
_ = utils.InitDb(dbFile, dbInit)
go utils.Listen(socketAddress)
for utils.ServerRunning {
if !utils.IsPortOpen() {
log.Println("try to open port")
l.Info("try to open port")
err := utils.OpenPort(device, baudrate, bytesize, stopbits)
if err != nil {
log.Fatal("error in open serial port ", err)
l.Fatal("error in open serial port ", err)
}
log.Println("starting read")
l.Info("starting read")
}
buffer, count := utils.ReadPort()
if count > 0 {
if lograw {
log.Print(string(buffer))
l.Error(string(buffer))
}
utils.Append(buffer)
}
}
<-done
fmt.Println("exiting")
l.Info("exiting")
}
+1 -3
View File
@@ -16,8 +16,6 @@ limitations under the License.
package cmd
import (
"log"
"github.com/spf13/cobra"
"it2000.com.cn/tele-recv/utils"
)
@@ -35,7 +33,7 @@ Test load sqlite database and init table`,
}
func test() {
log.Println("Testing environment")
l.Info("Testing environment")
_ = utils.OpenPort(device, baudrate, bytesize, stopbits)
err := utils.InitDb(dbFile, dbInit)