| logs | ||
| .gitignore | ||
| config.go | ||
| funtion.go | ||
| go.mod | ||
| go.sum | ||
| grlogs.go | ||
| log_test.go | ||
| log.go | ||
| options.go | ||
| README.md | ||
grlogs
本库为争游内部日志公共库, 该库基于 beegoLogger 基础上完善, 目前支持的引擎有 file、console、net、smtp、es、alisls
代码示例
- 引入
import "golib.gaore.com/GaoreGo/haiwai-grlogs"
- 简单用法
 grlogs.Get("test", 128).Info("hello word")
 grlogs.Get("test").Warning("hello word")
Get 方法中 lable 参数为标签,为识别分类所用,在Grlogs里一个分类使用一个管道进行日志
- 进阶用法
logger := grlogs.GetEs("wifi")
logger.SetAdapter(LevelAll, AdapterElasticSearch)
logger.SetAdapter(LevelInfo, AdapterFile)
logger.Critical("出错了")
logger.Info("出错了")
- 如果需要写入es 或 alils, 必须设置环境变量 GRLOG_APP_NAME, 不能有反斜杠, 如
export GRLOG_APP_NAME=mkt.gaore.com;
还需要额外引入es库,完成初始化动作
import _ "golib.gaore.com/GaoreGo/haiwai-grlogs/v2/logs/es"
import _ "golib.gaore.com/GaoreGo/haiwai-grlogs/v2/logs/alils"
- 
文件日志会写入到 ./runtime/logs/文件夹 请务必在项目构建阶段创建该目录
- 
AliLS日志接入的是阿里SLS GOSDK , 下面是关于 [https://github.com/aliyun/aliyun-log-go-sdk/tree/master/producer] 描述
producer提供了两种关闭模式,分为有限关闭和安全关闭,安全关闭会等待producer中缓存的所有的数据全部发送完成以后在关闭producer,有限关闭会接收用户传递的一个参数值,时间单位为秒,当开始关闭producer的时候开始计时,超过传递的设定值还未能完全关闭producer的话会强制退出producer,此时可能会有部分数据未被成功发送而丢失。
所以用了aliLS 的 adapter 时,最好调用grlogs.Close() 或 grlogs.CloseAll() 方法安全关闭通道,以刷新缓冲区
- 完整示例
package grlogs
import (
	"fmt"
	_ "golib.gaore.com/GaoreGo/haiwai-grlogs/v2/logs/es"
	"testing"
	"time"
)
func TestGetLogger(t *testing.T) {
	// 新建 channel 大小为128 标识为nds 日志通道 , Get 的方法 默认带 console 和 file 输出
	l := grlogs.Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch)
	l.Debug("我正在调试")
	l.Critical("出错了")
	// 复用 nds 的日志通道
	grlogs.Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
	grlogs.Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
	// 新建 channel 大小为默认 标识为wifi 日志通道 , GetEs 的方法 默认带 console 和 file 和 elatisearch 输出
	grlogs.GetEs("wifi")
	for i := 0; i < 10; i++ {
		grlogs.Get("wifi").Warning("Warning")
		grlogs.Get("wifi").Warn("Warn")
		grlogs.Get("wifi").Debug("Debug")
		grlogs.Get("wifi").Error("Error")
		grlogs.Get("wifi").Notice("Notice")
		grlogs.Get("wifi").Info("Info")
		grlogs.Get("wifi").Alert("Alert")
	}
	Get("wifi").Critical("neoweiwoewe")
}
func TestDropAdapter(t *testing.T) {
	grlogs.SetAdapter(LevelAll, AdapterAliLs)
	grlogs.DropAdapter(AdapterAliLs)
	grlogs.Informational(errors.New("he hello"))
	grlogs.SetAdapter(LevelAll, AdapterAliLs)
	grlogs.Debug(errors.New("he hello"))
	grlogs.CloseAll()
}