高热共公日志库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # grlogs
  2. 本库为争游内部日志公共库, 该库基于 beegoLogger 基础上完善, 目前支持的引擎有 file、console、net、smtp、es、alisls
  3. ## 代码示例
  4. 1. 引入
  5. ```
  6. import "golib.gaore.com/GaoreGo/grlogs"
  7. ```
  8. 2. 简单用法
  9. ```
  10. grlogs.Get("test", 128).Info("hello word")
  11. grlogs.Get("test").Warning("hello word")
  12. ```
  13. `Get` 方法中 `lable` 参数为标签,为识别分类所用,在Grlogs里一个分类使用一个管道进行日志
  14. 3. 进阶用法
  15. ```
  16. logger := grlogs.GetEs("wifi")
  17. logger.SetAdapter(LevelAll, AdapterElasticSearch)
  18. logger.SetAdapter(LevelInfo, AdapterFile)
  19. logger.Critical("出错了")
  20. logger.Info("出错了")
  21. ```
  22. 4. 如果需要写入es 或 alils, 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
  23. ```shell script
  24. export GRLOG_APP_NAME=mkt.gaore.com;
  25. ```
  26. 还需要额外引入es库,完成初始化动作
  27. ```go
  28. import _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
  29. ```
  30. ```go
  31. import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
  32. ```
  33. 5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录**
  34. 6. `AliLS` 日志接入的是阿里SLS GOSDK , 下面是关于 [https://github.com/aliyun/aliyun-log-go-sdk/tree/master/producer] 描述
  35. producer提供了两种关闭模式,分为有限关闭和安全关闭,安全关闭会等待producer中缓存的所有的数据全部发送完成以后在关闭producer,有限关闭会接收用户传递的一个参数值,时间单位为秒,当开始关闭producer的时候开始计时,超过传递的设定值还未能完全关闭producer的话会强制退出producer,此时可能会有部分数据未被成功发送而丢失。
  36. 所以用了aliLS 的 adapter 时,最好调用`grlogs.Close()` 或 `grlogs.CloseAll()` 方法安全关闭通道,以刷新缓冲区
  37. 7. 完整示例
  38. ```go
  39. package grlogs
  40. import (
  41. "fmt"
  42. _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
  43. "testing"
  44. "time"
  45. )
  46. func TestGetLogger(t *testing.T) {
  47. // 新建 channel 大小为128 标识为nds 日志通道 , Get 的方法 默认带 console 和 file 输出
  48. l := grlogs.Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch)
  49. l.Debug("我正在调试")
  50. l.Critical("出错了")
  51. // 复用 nds 的日志通道
  52. grlogs.Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
  53. grlogs.Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
  54. // 新建 channel 大小为默认 标识为wifi 日志通道 , GetEs 的方法 默认带 console 和 file 和 elatisearch 输出
  55. grlogs.GetEs("wifi")
  56. for i := 0; i < 10; i++ {
  57. grlogs.Get("wifi").Warning("Warning")
  58. grlogs.Get("wifi").Warn("Warn")
  59. grlogs.Get("wifi").Debug("Debug")
  60. grlogs.Get("wifi").Error("Error")
  61. grlogs.Get("wifi").Notice("Notice")
  62. grlogs.Get("wifi").Info("Info")
  63. grlogs.Get("wifi").Alert("Alert")
  64. }
  65. Get("wifi").Critical("neoweiwoewe")
  66. }
  67. func TestDropAdapter(t *testing.T) {
  68. grlogs.SetAdapter(LevelAll, AdapterAliLs)
  69. grlogs.DropAdapter(AdapterAliLs)
  70. grlogs.Informational(errors.New("he hello"))
  71. grlogs.SetAdapter(LevelAll, AdapterAliLs)
  72. grlogs.Debug(errors.New("he hello"))
  73. grlogs.CloseAll()
  74. }
  75. ```