高热共公日志库
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.
余 欣怀 8b94a0d256 修改es账号密码 1 年之前
logs 修复es type错误问题 1 年之前
.gitignore v1.2.0 整个beegolog 打进去, 增加syncmap日志通道 3 年之前
README.md v1.3.2 增加 glogs.Debug 等直接方法 3 年之前
config.go 接入阿里sls 3 年之前
funtion.go 修改es账号密码 1 年之前
grlogs.go v1.3.2 增加 glogs.Debug 等直接方法 3 年之前
log.go v1.3.5 Fix 修复goroutine并发调用初始化 nil pointer的问题 3 年之前
log_test.go 修复es type错误问题 1 年之前
options.go v1.3.5 Fix 修复goroutine并发调用初始化 nil pointer的问题 3 年之前

README.md

grlogs

本库为争游内部日志公共库, 该库基于 beegoLogger 基础上完善, 目前支持的引擎有 file、console、net、smtp、es、alisls

代码示例

  1. 引入
import "golib.gaore.com/GaoreGo/grlogs"
  1. 简单用法
 grlogs.Get("test", 128).Info("hello word")
 grlogs.Get("test").Warning("hello word")

Get 方法中 lable 参数为标签,为识别分类所用,在Grlogs里一个分类使用一个管道进行日志

  1. 进阶用法
logger := grlogs.GetEs("wifi")
logger.SetAdapter(LevelAll, AdapterElasticSearch)
logger.SetAdapter(LevelInfo, AdapterFile)
logger.Critical("出错了")
logger.Info("出错了")
  1. 如果需要写入es 或 alils, 必须设置环境变量 GRLOG_APP_NAME, 不能有反斜杠, 如
export GRLOG_APP_NAME=mkt.gaore.com;

还需要额外引入es库,完成初始化动作

import _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
  1. 文件日志会写入到 ./runtime/logs/ 文件夹 请务必在项目构建阶段创建该目录

  2. 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() 方法安全关闭通道,以刷新缓冲区

  1. 完整示例
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 := 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()
}