From 63cd96cf59a0d0250273e91e20c68896b8a08906 Mon Sep 17 00:00:00 2001 From: SchwarzSail <119314702+SchwarzSail@users.noreply.github.com> Date: Mon, 7 Oct 2024 16:51:22 +0800 Subject: [PATCH] fix: modify the usage of the logger (#12) --- cmd/api/biz/handler/api/class_room_service.go | 4 ++-- cmd/api/biz/handler/api/user_service.go | 4 ++-- cmd/api/biz/rpc/classroom.go | 2 +- cmd/api/biz/rpc/user.go | 2 +- cmd/api/main.go | 4 +--- cmd/classroom/dal/cache/init.go | 2 +- cmd/classroom/dal/cache/schedule.go | 4 ++-- cmd/classroom/handler.go | 8 ++++---- cmd/classroom/main.go | 10 ++++------ cmd/classroom/workqueue.go | 4 ++-- cmd/user/main.go | 9 ++++----- config/config.go | 18 +++++++++--------- pkg/logger/logger.go | 7 ++++++- 13 files changed, 39 insertions(+), 39 deletions(-) diff --git a/cmd/api/biz/handler/api/class_room_service.go b/cmd/api/biz/handler/api/class_room_service.go index 9a4b8a0..ff32e54 100644 --- a/cmd/api/biz/handler/api/class_room_service.go +++ b/cmd/api/biz/handler/api/class_room_service.go @@ -21,7 +21,7 @@ func GetEmptyClassrooms(ctx context.Context, c *app.RequestContext) { var req api.EmptyClassroomRequest err = c.BindAndValidate(&req) if err != nil { - logger.LoggerObj.Errorf("api.GetEmptyClassrooms: BindAndValidate error %v", err) + logger.Errorf("api.GetEmptyClassrooms: BindAndValidate error %v", err) pack.RespError(c, errno.ParamEmpty) return } @@ -32,7 +32,7 @@ func GetEmptyClassrooms(ctx context.Context, c *app.RequestContext) { Campus: req.Campus, }) if err != nil { - logger.LoggerObj.Errorf("api.GetEmptyClassrooms: GetEmptyRoomRPC error %v", err) + logger.Errorf("api.GetEmptyClassrooms: GetEmptyRoomRPC error %v", err) pack.RespError(c, errno.InternalServiceError) return } diff --git a/cmd/api/biz/handler/api/user_service.go b/cmd/api/biz/handler/api/user_service.go index 6041d62..0c85f22 100644 --- a/cmd/api/biz/handler/api/user_service.go +++ b/cmd/api/biz/handler/api/user_service.go @@ -20,7 +20,7 @@ func GetLoginData(ctx context.Context, c *app.RequestContext) { var req api.GetLoginDataRequest err = c.BindAndValidate(&req) if err != nil { - logger.LoggerObj.Errorf("api.GetLoginData: BindAndValidate error %v", err) + logger.Errorf("api.GetLoginData: BindAndValidate error %v", err) pack.RespError(c, errno.ParamEmpty) return } @@ -30,7 +30,7 @@ func GetLoginData(ctx context.Context, c *app.RequestContext) { Password: req.Password, }) if err != nil { - logger.LoggerObj.Errorf("api.GetLoginData: GetEmptyRoomRPC error %v", err) + logger.Errorf("api.GetLoginData: GetEmptyRoomRPC error %v", err) pack.RespError(c, errno.InternalServiceError) return } diff --git a/cmd/api/biz/rpc/classroom.go b/cmd/api/biz/rpc/classroom.go index 6f1638f..40f24bd 100644 --- a/cmd/api/biz/rpc/classroom.go +++ b/cmd/api/biz/rpc/classroom.go @@ -14,7 +14,7 @@ import ( func InitClassroomRPC() { client, err := client.InitClassroomRPC() if err != nil { - logger.LoggerObj.Fatalf("api.rpc.classroom InitClassroomRPC failed, err %v", err) + logger.Fatalf("api.rpc.classroom InitClassroomRPC failed, err %v", err) } classroomClient = *client } diff --git a/cmd/api/biz/rpc/user.go b/cmd/api/biz/rpc/user.go index 5d76c58..184e217 100644 --- a/cmd/api/biz/rpc/user.go +++ b/cmd/api/biz/rpc/user.go @@ -13,7 +13,7 @@ import ( func InitUserRPC() { client, err := client.InitUserRPC() if err != nil { - logger.LoggerObj.Fatalf("api.rpc.user InitUserRPC failed, err is %v", err) + logger.Fatalf("api.rpc.user InitUserRPC failed, err is %v", err) } userClient = *client } diff --git a/cmd/api/main.go b/cmd/api/main.go index acae48a..2800275 100755 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -18,8 +18,6 @@ var ( ) func Init() { - //日志初始化 - logger.LoggerInit() // config init path = flag.String("config", "./config", "config path") flag.Parse() @@ -33,7 +31,7 @@ func main() { // get available port from config set listenAddr, err := utils.GetAvailablePort() if err != nil { - logger.LoggerObj.Fatalf("Api: get available port failed, err: %v", err) + logger.Fatalf("Api: get available port failed, err: %v", err) } h := server.New( diff --git a/cmd/classroom/dal/cache/init.go b/cmd/classroom/dal/cache/init.go index cfaa3fe..3423278 100644 --- a/cmd/classroom/dal/cache/init.go +++ b/cmd/classroom/dal/cache/init.go @@ -13,7 +13,7 @@ func Init() { redisClient, err := client.NewRedisClient(constants.RedisDBEmptyRoom) if err != nil { // 如果redis服务启动失败,直接exit - logger.LoggerObj.Fatalf("cache.Init failed, err is %v", err) + logger.Fatalf("cache.Init failed, err is %v", err) } RedisClient = redisClient } diff --git a/cmd/classroom/dal/cache/schedule.go b/cmd/classroom/dal/cache/schedule.go index e78ced5..072b4aa 100644 --- a/cmd/classroom/dal/cache/schedule.go +++ b/cmd/classroom/dal/cache/schedule.go @@ -65,10 +65,10 @@ func ScheduledGetClassrooms() error { return fmt.Errorf("ScheduledGetClassrooms: failed to set empty room cache: %w", err) } } - logger.LoggerObj.Debugf("ScheduledGetClassrooms: complete the task %v", args) + logger.Debugf("ScheduledGetClassrooms: complete the task %v", args) } } - logger.LoggerObj.Infof("classroom.cache.GetClassrooms complete all tasks of campus %v in the day %v", campus, date) + logger.Infof("classroom.cache.GetClassrooms complete all tasks of campus %v in the day %v", campus, date) } } return nil diff --git a/cmd/classroom/handler.go b/cmd/classroom/handler.go index 39f47d1..384f987 100644 --- a/cmd/classroom/handler.go +++ b/cmd/classroom/handler.go @@ -22,7 +22,7 @@ func (s *ClassroomServiceImpl) GetEmptyRoom(ctx context.Context, req *classroom. // 首先判断date的格式是否符合要求 requestDate, err := time.Parse("2006-01-02", req.Date) if err != nil { - logger.LoggerObj.Errorf("Classroom.GetEmptyRoom: date format error, err: %v", err) + logger.Errorf("Classroom.GetEmptyRoom: date format error, err: %v", err) resp.Base = pack.BuildBaseResp(err) return resp, nil } @@ -33,19 +33,19 @@ func (s *ClassroomServiceImpl) GetEmptyRoom(ctx context.Context, req *classroom. dateDiff := requestDate.Sub(now).Hours() / 24 if dateDiff < 0 || dateDiff > 30 { err = fmt.Errorf("Classroom.GetEmptyRoom: date out of range, date: %v", req.Date) - logger.LoggerObj.Errorf("Classroom.GetEmptyRoom: %v", err) + logger.Errorf("Classroom.GetEmptyRoom: %v", err) resp.Base = pack.BuildBaseResp(err) return resp, nil } l := service.NewClassroomService(ctx) res, err := l.GetEmptyRoom(req) if err != nil { - logger.LoggerObj.Errorf("Classroom.GetEmptyRoom: GetEmptyRoom failed, err: %v", err) + logger.Errorf("Classroom.GetEmptyRoom: GetEmptyRoom failed, err: %v", err) resp.Base = pack.BuildBaseResp(err) return resp, nil } resp.Base = pack.BuildBaseResp(nil) resp.Rooms = pack.BuildClassRooms(res, req.Campus) - logger.LoggerObj.Info("Classroom.GetEmptyRoom: GetEmptyRoom success") + logger.Info("Classroom.GetEmptyRoom: GetEmptyRoom success") return resp, nil } diff --git a/cmd/classroom/main.go b/cmd/classroom/main.go index 6248922..66f76c5 100644 --- a/cmd/classroom/main.go +++ b/cmd/classroom/main.go @@ -23,8 +23,6 @@ var ( func Init() { // config init - logger.LoggerInit() - path = flag.String("config", "./config", "config path") flag.Parse() config.Init(*path, serviceName) @@ -39,18 +37,18 @@ func main() { if err != nil { // 如果无法解析etcd的地址,则无法连接到其他的微服务,说明整个服务无法运行,直接panic // 因为api只做数据包装返回和转发请求 - logger.LoggerObj.Fatalf("Classroom: etcd registry failed, error: %v", err) + logger.Fatalf("Classroom: etcd registry failed, error: %v", err) } // get available port from config set listenAddr, err := utils.GetAvailablePort() if err != nil { - logger.LoggerObj.Fatalf("Classroom: get available port failed: %v", err) + logger.Fatalf("Classroom: get available port failed: %v", err) } addr, err := net.ResolveTCPAddr("tcp", listenAddr) if err != nil { - logger.LoggerObj.Fatalf("Classroom: listen addr failed %v", err) + logger.Fatalf("Classroom: listen addr failed %v", err) } svr := classroom.NewServer( @@ -71,6 +69,6 @@ func main() { WorkQueue.Add("signal") if err = svr.Run(); err != nil { - logger.LoggerObj.Fatalf("Classroom: server run failed: %v", err) + logger.Fatalf("Classroom: server run failed: %v", err) } } diff --git a/cmd/classroom/workqueue.go b/cmd/classroom/workqueue.go index 3cd3684..ca80598 100644 --- a/cmd/classroom/workqueue.go +++ b/cmd/classroom/workqueue.go @@ -33,12 +33,12 @@ func worker() { return } if err := cache.ScheduledGetClassrooms(); err != nil { - logger.LoggerObj.Errorf("Classroom.worker ScheduledGetClassrooms failed: %v", err) + logger.Errorf("Classroom.worker ScheduledGetClassrooms failed: %v", err) // 如果失败,在使用该函数,采取避退策略 WorkQueue.AddRateLimited(item) } // 将signal重新放入队列,实现自驱动 WorkQueue.AddAfter(item, constants.ScheduledTime) - logger.LoggerObj.Info("Classroom.worker ScheduledGetClassrooms success") + logger.Info("Classroom.worker ScheduledGetClassrooms success") } } diff --git a/cmd/user/main.go b/cmd/user/main.go index 6dcab7a..df7b904 100644 --- a/cmd/user/main.go +++ b/cmd/user/main.go @@ -23,7 +23,6 @@ var ( func Init() { // config init - logger.LoggerInit() path = flag.String("config", "./config", "config path") flag.Parse() config.Init(*path, serviceName) @@ -33,18 +32,18 @@ func main() { Init() r, err := etcd.NewEtcdRegistry([]string{config.Etcd.Addr}) if err != nil { - logger.LoggerObj.Fatalf("User: new etcd registry failed, err: %v", err) + logger.Fatalf("User: new etcd registry failed, err: %v", err) } // get available port from config set listenAddr, err := utils.GetAvailablePort() if err != nil { - logger.LoggerObj.Fatalf("User: get available port failed, err: %v", err) + logger.Fatalf("User: get available port failed, err: %v", err) } addr, err := net.ResolveTCPAddr("tcp", listenAddr) if err != nil { - logger.LoggerObj.Fatalf("User: resolve tcp addr failed, err: %v", err) + logger.Fatalf("User: resolve tcp addr failed, err: %v", err) } svr := user.NewServer( @@ -61,6 +60,6 @@ func main() { }), ) if err = svr.Run(); err != nil { - logger.LoggerObj.Fatalf("User: run server failed, err: %v", err) + logger.Fatalf("User: run server failed, err: %v", err) } } diff --git a/config/config.go b/config/config.go index ee2802c..9564713 100644 --- a/config/config.go +++ b/config/config.go @@ -34,7 +34,7 @@ func Init(path string, service string) { etcdAddr := os.Getenv("ETCD_ADDR") if etcdAddr == "" { - logger.LoggerObj.Fatalf("config.Init: etcd addr is empty") + logger.Fatalf("config.Init: etcd addr is empty") } Etcd = &etcd{Addr: etcdAddr} @@ -43,24 +43,24 @@ func Init(path string, service string) { err := runtime_viper.AddRemoteProvider("etcd3", Etcd.Addr, "/config/config.yaml") if err != nil { - logger.LoggerObj.Fatalf("config.Init: add remote provider error: %v", err) + logger.Fatalf("config.Init: add remote provider error: %v", err) } - logger.LoggerObj.Infof("config.Init: config path: %v", path) + logger.Infof("config.Init: config path: %v", path) if err := runtime_viper.ReadRemoteConfig(); err != nil { if _, ok := err.(viper.ConfigFileNotFoundError); ok { - logger.LoggerObj.Fatal("config.Init: could not find config files") + logger.Fatal("config.Init: could not find config files") } else { - logger.LoggerObj.Fatal("config.Init: read config error: %v", err) + logger.Fatal("config.Init: read config error: %v", err) } - logger.LoggerObj.Fatal("config.Init: read config error: %v", err) + logger.Fatal("config.Init: read config error: %v", err) } configMapping(service) - // logger.LoggerObj.Infof("all keys: %v\n", runtime_viper.AllKeys()) + // logger.Infof("all keys: %v\n", runtime_viper.AllKeys()) // 持续监听配置 runtime_viper.OnConfigChange(func(e fsnotify.Event) { - logger.LoggerObj.Infof("config: config file changed: %v\n", e.String()) + logger.Infof("config: config file changed: %v\n", e.String()) }) runtime_viper.WatchConfig() } @@ -68,7 +68,7 @@ func Init(path string, service string) { func configMapping(srv string) { c := new(config) if err := runtime_viper.Unmarshal(&c); err != nil { - logger.LoggerObj.Fatalf("config.configMapping: config: unmarshal error: %v", err) + logger.Fatalf("config.configMapping: config: unmarshal error: %v", err) } Snowflake = &c.Snowflake diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index e9d0663..03b3346 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -35,7 +35,7 @@ func initLogger() { EncodeCaller: zapcore.ShortCallerEncoder, }, } - logger, err := config.Build() // 创建基础 Logger + logger, err := config.Build(zap.AddCallerSkip(1)) // 创建基础 Logger if err != nil { panic(err) } @@ -73,6 +73,11 @@ func Debugf(template string, args ...interface{}) { LoggerObj.Debugf(template, args) } +func Fatal(args ...interface{}) { + ensureLoggerInit() + LoggerObj.Fatal(args) +} + func Info(args ...interface{}) { ensureLoggerInit() LoggerObj.Info(args)