Go游戏服务器开发的一些思考(二十三):Go语言Log库封装技巧
2017-11-06 23:07
1021 查看
问题由来
这里又涉及到Go语言的一处脑残设定。在C++中,#include 语句,会递归include .h文件中的.h文件。
这样做的好处是:很多.h文件只需要写include语句一次,基本上整个项目都可以用
这样做的坏处是:编译时间会变长
于是,Go语言做了“优化”。import语句是不会递归import文件中的文件。
然后就是一个项目中,几乎每个文件中,都要敲一遍类似下面的语句:
import ( "github.com/golang/glog" )
相当于用敲import语句的时间(或是翻阅查找import语句;然后复制黏贴的时间)来换编译时间。
解决方法
定义如下的类:package common import ( gloglog "github.com/golang/glog" ) type ILogger interface { Info(args ...interface{}) Infoln(args ...interface{}) Infof(format string, args ...interface{}) Warning(args ...interface{}) Warningln(args ...interface{}) Warningf(format string, args ...interface{}) Error(args ...interface{}) Errorln(args ...interface{}) Errorf(format string, args ...interface{}) Fatal(args ...interface{}) Fatalln(args ...interface{}) Fatalf(format string, args ...interface{}) Flush() } type DefaultLogger struct { } func NewDefaultLogger() *DefaultLogger { return &DefaultLogger{} } func (this *DefaultLogger) Info(args ...interface{}) { } func (this *DefaultLogger) Infof(format string, args ...interface{}) { } func (this *DefaultLogger) Infoln(args ...interface{}) { } func (this *DefaultLogger) Warning(args ...interface{}) { } func (this *DefaultLogger) Warningln(args ...interface{}) { } func (this *DefaultLogger) Warningf(format string, args ...interface{}) { } func (this *DefaultLogger) Error(args ...interface{}) { } func (this *DefaultLogger) Errorf(format string, args ...interface{}) { } func (this *DefaultLogger) Errorln(args ...interface{}) { } func (this *DefaultLogger) Fatal(args ...interface{}) { } func (this *DefaultLogger) Fatalln(args ...interface{}) { } func (this *DefaultLogger) Fatalf(format string, args ...interface{}) { } func (this *DefaultLogger) Flush() { } type GLog struct { } func NewGLog() *GLog { return &GLog{} } func (this *GLog) Info(args ...interface{}) { gloglog.Info(args...) } func (this *GLog) Infof(format string, args ...interface{}) { gloglog.Infof(format, args...) } func (this *GLog) Infoln(args ...interface{}) { gloglog.Infoln(args...) } func (this *GLog) Warning(args ...interface{}) { gloglog.Warning(args...) } func (this *GLog) Warningln(args ...interface{}) { gloglog.Warningln(args...) } func (this *GLog) Warningf(format string, args ...interface{}) { gloglog.Warningf(format, args...) } func (this *GLog) Error(args ...interface{}) { gloglog.Error(args...) } func (this *GLog) Errorf(format string, args ...interface{}) { gloglog.Errorf(format, args...) } func (this *GLog) Errorln(args ...interface{}) { gloglog.Errorln(args...) } func (this *GLog) Fatal(args ...interface{}) { gloglog.Fatal(args...) } func (this *GLog) Fatalln(args ...interface{}) { gloglog.Fatalln(args...) } func (this *GLog) Fatalf(format string, args ...interface{}) { gloglog.Fatalf(format, args...) } func (this *GLog) Flush() { gloglog.Flush() } var ( glog ILogger = NewDefaultLogger() ) func SetLogger(log ILogger) { glog = log }
然后在每个目录或者子目录,拷贝一份如下代码文件,类似这样:
package main import ( "github.com/fananchong/go-x/common" ) var ( glog common.ILogger = common.NewGLog() )
以上。你就可以在任意文件中,直接使用glog.xxx(…)。再也不需要import对应的log库了。变相的实现类似#include语句的效果。
代码示例
可以参考go-x中的实现:https://github.com/fananchong/go-x/blob/master/common/log.go
https://github.com/fananchong/go-x/blob/master/test/test_packet_loss/server/log.go
相关文章推荐
- Go游戏服务器开发的一些思考(二十四):命令行库封装技巧
- Go游戏服务器开发的一些思考(一):语言层面
- Go游戏服务器开发的一些思考(二十一):Go语言的两处脑残设定
- Go游戏服务器开发的一些思考(三十二):关于无缝世界的一些思考
- Go游戏服务器开发的一些思考(九):Docker桥接网络及固定IP (二)
- Go游戏服务器开发的一些思考(二十五):Redis的Docker Swarm部署
- Go游戏服务器开发的一些思考(三十):排行榜服务器设计思路
- Go游戏服务器开发的一些思考(十五):gochart图表制作
- Go游戏服务器开发的一些思考(十三):behavior3go的一些坑(备忘)
- Go游戏服务器开发的一些思考(六):Docker Swarm Mode
- Go游戏服务器开发的一些思考(十一):IO游戏同步
- Go游戏服务器开发的一些思考(八):Docker桥接网络及固定IP
- Go游戏服务器开发的一些思考(三十九):go reflect的正确使用方法
- Go游戏服务器开发的一些思考(二十二):Godep包管理介绍
- Go游戏服务器开发的一些思考(九):Docker桥接网络及固定IP (二)
- Go游戏服务器开发的一些思考(三十四):无缝世界场景构建与同步(一)
- Go游戏服务器开发的一些思考(三十六):无缝世界场景构建与同步(三)
- Go游戏服务器开发的一些思考(十二):行为树behavior3go介绍
- Go游戏服务器开发的一些思考(二十七):Go Redis ORM库(二)
- Go游戏服务器开发的一些思考(十八):Docker内网环境搭建(备忘)