# grlogs 本库为争游内部日志公共库, 该库基于 beegoLogger 基础上完善, 目前支持的引擎有 file、console、net、smtp、es、alisls ## 代码示例 1. 引入 ``` import "golib.gaore.com/GaoreGo/grlogs" ``` 2. 简单用法 ``` grlogs.Get("test", 128).Info("hello word") grlogs.Get("test").Warning("hello word") ``` `Get` 方法中 `lable` 参数为标签,为识别分类所用,在Grlogs里一个分类使用一个管道进行日志 3. 进阶用法 ``` logger := grlogs.GetEs("wifi") logger.SetAdapter(LevelAll, AdapterElasticSearch) logger.SetAdapter(LevelInfo, AdapterFile) logger.Critical("出错了") logger.Info("出错了") ``` 4. 如果需要写入es, 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如 ```shell script export GRLOG_APP_NAME=mkt.gaore.com; ``` 还需要额外引入es库,完成初始化动作 ```go import _ "golib.gaore.com/GaoreGo/grlogs/logs/es" ``` 5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录** 6. 完整示例 ```go package grlogs import ( "fmt" _ "golib.gaore.com/GaoreGo/grlogs/logs/es" "testing" "time" ) func TestGetLogger(t *testing.T) { // 新建 channel 大小为128 标识为nds 日志通道 , Get 的方法 默认带 console 和 file 输出 l := Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch) l.Debug("我正在调试") l.Critical("出错了") // 复用 nds 的日志通道 Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123)) Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123)) // 新建 channel 大小为默认 标识为wifi 日志通道 , GetEs 的方法 默认带 console 和 file 和 elatisearch 输出 GetEs("wifi") for i := 0; i < 10; i++ { Get("wifi").Warning("Warning") Get("wifi").Warn("Warn") Get("wifi").Debug("Debug") Get("wifi").Error("Error") Get("wifi").Notice("Notice") Get("wifi").Info("Info") Get("wifi").Alert("Alert") } Get("wifi").Critical("neoweiwoewe") } func TestGetEs(t *testing.T) { fmt.Println("hello world") } ```