您的位置:首页 > 编程语言 > Go语言

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语言 游戏 log
相关文章推荐