1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package initialize
- import (
- "dhcp/global"
- "fmt"
- "github.com/go-logr/zapr"
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- "os"
- "time"
- )
- // defaultLogger is zap logr implementation.
- func InitLogger(level string) {
- config := zap.NewProductionConfig()
- file := initLogFile()
- config.OutputPaths = []string{"stdout", file}
- switch level {
- case "debug":
- config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
- default:
- config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
- }
- zapLogger, err := config.Build()
- if err != nil {
- panic(fmt.Sprintf("who watches the watchmen (%v)?", err))
- }
- global.Log = zapr.NewLogger(zapLogger)
- }
- func initLogFile() string {
- // 获取当前日期
- currentTime := time.Now()
- err := os.MkdirAll("log", os.ModePerm)
- if err != nil {
- panic(err)
- }
- // 格式化日期作为文件名
- filename := "log/app_" + currentTime.Format("2006-01-02") + ".log"
- _, err2 := os.Create(filename)
- if err2 != nil {
- global.Log.Error(err2, "create log file failed")
- }
- return filename
- }
|