add zap logger
This commit is contained in:
+35
-16
@@ -18,9 +18,12 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"it2000.com.cn/tele-recv/utils"
|
||||
@@ -55,7 +58,7 @@ Read the telegram from `,
|
||||
Mode = os.Getenv(ModeName)
|
||||
|
||||
logger *zap.Logger
|
||||
l *zap.SugaredLogger
|
||||
// l *zap.SugaredLogger
|
||||
)
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
@@ -78,14 +81,12 @@ 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()
|
||||
}
|
||||
|
||||
initLog()
|
||||
defer logger.Sync()
|
||||
l = logger.Sugar()
|
||||
utils.Log = l
|
||||
// l = logger.Sugar()
|
||||
utils.Log = logger
|
||||
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
@@ -94,13 +95,6 @@ func initConfig() {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
//home, err := os.Open(".")
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
// os.Exit(1)
|
||||
//}
|
||||
|
||||
// Search config in home directory with name ".tele-recv" (without extension).
|
||||
viper.AddConfigPath(".")
|
||||
viper.SetConfigName("telegram")
|
||||
@@ -111,7 +105,7 @@ func initConfig() {
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
// fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
l.Infof("Using config file : %s", viper.ConfigFileUsed())
|
||||
logger.Info("Using config file ", zap.String("path", viper.ConfigFileUsed()))
|
||||
device = viper.GetString("serial.device")
|
||||
baudrate = viper.GetInt("serial.baudrate")
|
||||
bytesize = byte(viper.GetInt("serial.bytesize"))
|
||||
@@ -123,3 +117,28 @@ func initConfig() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func initLog() {
|
||||
logFile := "./log/telegram-%Y-%m-%d-%H.log"
|
||||
rotator, err := rotatelogs.New(
|
||||
logFile,
|
||||
rotatelogs.WithMaxAge(60*24*time.Hour),
|
||||
rotatelogs.WithRotationTime(time.Hour))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
priority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
||||
return lvl >= zapcore.DebugLevel
|
||||
})
|
||||
|
||||
encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
|
||||
|
||||
core := zapcore.NewTee(
|
||||
zapcore.NewCore(encoder, zapcore.Lock(os.Stdout), priority),
|
||||
zapcore.NewCore(encoder, zapcore.AddSync(rotator), priority),
|
||||
)
|
||||
|
||||
logger = zap.New(core)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user