Skip to content

Commit

Permalink
fix: modify the usage of the logger (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
SchwarzSail authored Oct 7, 2024
1 parent a670173 commit 63cd96c
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions cmd/api/biz/handler/api/class_room_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cmd/api/biz/handler/api/user_service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/api/biz/rpc/classroom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/biz/rpc/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/api/main.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/classroom/dal/cache/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cmd/classroom/dal/cache/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions cmd/classroom/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
10 changes: 4 additions & 6 deletions cmd/classroom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ var (

func Init() {
// config init
logger.LoggerInit()

path = flag.String("config", "./config", "config path")
flag.Parse()
config.Init(*path, serviceName)
Expand All @@ -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(
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions cmd/classroom/workqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
9 changes: 4 additions & 5 deletions cmd/user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (

func Init() {
// config init
logger.LoggerInit()
path = flag.String("config", "./config", "config path")
flag.Parse()
config.Init(*path, serviceName)
Expand All @@ -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(
Expand All @@ -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)
}
}
18 changes: 9 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -43,32 +43,32 @@ 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()
}

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

Expand Down
7 changes: 6 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 63cd96c

Please sign in to comment.