Compare commits
No commits in common. "master" and "v1.3.0" have entirely different histories.
45
README.md
45
README.md
@ -23,7 +23,7 @@ logger.Critical("出错了")
|
|||||||
logger.Info("出错了")
|
logger.Info("出错了")
|
||||||
```
|
```
|
||||||
|
|
||||||
4. 如果需要写入es 或 alils, 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
|
4. 如果需要写入es, 必须设置环境变量 `GRLOG_APP_NAME`, 不能有反斜杠, 如
|
||||||
```shell script
|
```shell script
|
||||||
export GRLOG_APP_NAME=mkt.gaore.com;
|
export GRLOG_APP_NAME=mkt.gaore.com;
|
||||||
```
|
```
|
||||||
@ -32,20 +32,10 @@ export GRLOG_APP_NAME=mkt.gaore.com;
|
|||||||
import _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
import _ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
||||||
```
|
```
|
||||||
|
|
||||||
```go
|
|
||||||
import _ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
|
|
||||||
```
|
|
||||||
|
|
||||||
5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录**
|
5. 文件日志会写入到 `./runtime/logs/` 文件夹 **请务必在项目构建阶段创建该目录**
|
||||||
|
|
||||||
|
|
||||||
6. `AliLS` 日志接入的是阿里SLS GOSDK , 下面是关于 [https://github.com/aliyun/aliyun-log-go-sdk/tree/master/producer] 描述
|
6. 完整示例
|
||||||
|
|
||||||
producer提供了两种关闭模式,分为有限关闭和安全关闭,安全关闭会等待producer中缓存的所有的数据全部发送完成以后在关闭producer,有限关闭会接收用户传递的一个参数值,时间单位为秒,当开始关闭producer的时候开始计时,超过传递的设定值还未能完全关闭producer的话会强制退出producer,此时可能会有部分数据未被成功发送而丢失。
|
|
||||||
|
|
||||||
所以用了aliLS 的 adapter 时,最好调用`grlogs.Close()` 或 `grlogs.CloseAll()` 方法安全关闭通道,以刷新缓冲区
|
|
||||||
|
|
||||||
7. 完整示例
|
|
||||||
```go
|
```go
|
||||||
package grlogs
|
package grlogs
|
||||||
|
|
||||||
@ -58,35 +48,30 @@ import (
|
|||||||
|
|
||||||
func TestGetLogger(t *testing.T) {
|
func TestGetLogger(t *testing.T) {
|
||||||
// 新建 channel 大小为128 标识为nds 日志通道 , Get 的方法 默认带 console 和 file 输出
|
// 新建 channel 大小为128 标识为nds 日志通道 , Get 的方法 默认带 console 和 file 输出
|
||||||
l := grlogs.Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch)
|
l := Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch)
|
||||||
l.Debug("我正在调试")
|
l.Debug("我正在调试")
|
||||||
l.Critical("出错了")
|
l.Critical("出错了")
|
||||||
|
|
||||||
// 复用 nds 的日志通道
|
// 复用 nds 的日志通道
|
||||||
grlogs.Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
|
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
|
||||||
grlogs.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 输出
|
// 新建 channel 大小为默认 标识为wifi 日志通道 , GetEs 的方法 默认带 console 和 file 和 elatisearch 输出
|
||||||
grlogs.GetEs("wifi")
|
GetEs("wifi")
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
grlogs.Get("wifi").Warning("Warning")
|
Get("wifi").Warning("Warning")
|
||||||
grlogs.Get("wifi").Warn("Warn")
|
Get("wifi").Warn("Warn")
|
||||||
grlogs.Get("wifi").Debug("Debug")
|
Get("wifi").Debug("Debug")
|
||||||
grlogs.Get("wifi").Error("Error")
|
Get("wifi").Error("Error")
|
||||||
grlogs.Get("wifi").Notice("Notice")
|
Get("wifi").Notice("Notice")
|
||||||
grlogs.Get("wifi").Info("Info")
|
Get("wifi").Info("Info")
|
||||||
grlogs.Get("wifi").Alert("Alert")
|
Get("wifi").Alert("Alert")
|
||||||
}
|
}
|
||||||
|
|
||||||
Get("wifi").Critical("neoweiwoewe")
|
Get("wifi").Critical("neoweiwoewe")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDropAdapter(t *testing.T) {
|
func TestGetEs(t *testing.T) {
|
||||||
grlogs.SetAdapter(LevelAll, AdapterAliLs)
|
fmt.Println("hello world")
|
||||||
grlogs.DropAdapter(AdapterAliLs)
|
|
||||||
grlogs.Informational(errors.New("he hello"))
|
|
||||||
grlogs.SetAdapter(LevelAll, AdapterAliLs)
|
|
||||||
grlogs.Debug(errors.New("he hello"))
|
|
||||||
grlogs.CloseAll()
|
|
||||||
}
|
}
|
||||||
```
|
```
|
95
funtion.go
95
funtion.go
@ -5,21 +5,9 @@ import (
|
|||||||
"github.com/astaxie/beego/logs"
|
"github.com/astaxie/beego/logs"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{
|
var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
|
||||||
|
|
||||||
AdapterSocket: func(l *Logger, level Level) error {
|
|
||||||
c := ConnLogConfig{
|
|
||||||
ReconnectOnMsg: false,
|
|
||||||
Reconnect: true,
|
|
||||||
Net: "",
|
|
||||||
Addr: "127.0.0.1:9888",
|
|
||||||
Level: level,
|
|
||||||
}
|
|
||||||
return l.SetLogger(logs.AdapterConn, c.String())
|
|
||||||
},
|
|
||||||
|
|
||||||
AdapterFile: func(l *Logger, level Level) error {
|
AdapterFile: func(l *Logger, level Level) error {
|
||||||
if wd, err := os.Getwd(); err == nil {
|
if wd, err := os.Getwd(); err == nil {
|
||||||
@ -45,17 +33,25 @@ var adatperSetMapper = map[Adapter]func(l *Logger, level Level) error{
|
|||||||
},
|
},
|
||||||
|
|
||||||
AdapterElasticSearch: func(l *Logger, level Level) error {
|
AdapterElasticSearch: func(l *Logger, level Level) error {
|
||||||
c := GenEsConfig(level)
|
|
||||||
|
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
|
||||||
|
if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" {
|
||||||
|
dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/"
|
||||||
|
}
|
||||||
|
|
||||||
|
c := EsConfig{
|
||||||
|
Username: "elastic",
|
||||||
|
Password: "Hellogaore@",
|
||||||
|
Dsn: dsn,
|
||||||
|
Level: level,
|
||||||
|
Index: os.Getenv("GRLOG_APP_NAME"),
|
||||||
|
}
|
||||||
|
|
||||||
return l.SetLogger(logs.AdapterEs, c.String())
|
return l.SetLogger(logs.AdapterEs, c.String())
|
||||||
},
|
},
|
||||||
|
|
||||||
AdapterAliLs: func(l *Logger, level Level) error {
|
AdapterAliLs: func(l *Logger, level Level) error {
|
||||||
c := GenAliConfig(level)
|
|
||||||
return l.SetLogger(logs.AdapterAliLS, c.String())
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenAliConfig(level Level) *AliLSConfig {
|
|
||||||
var project string = "gaore-app-logstore"
|
var project string = "gaore-app-logstore"
|
||||||
var endpoint string
|
var endpoint string
|
||||||
|
|
||||||
@ -65,7 +61,7 @@ func GenAliConfig(level Level) *AliLSConfig {
|
|||||||
endpoint = project + ".cn-shenzhen.log.aliyuncs.com"
|
endpoint = project + ".cn-shenzhen.log.aliyuncs.com"
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &AliLSConfig{
|
c := AliLSConfig{
|
||||||
Project: project,
|
Project: project,
|
||||||
Endpoint: endpoint,
|
Endpoint: endpoint,
|
||||||
KeyID: "LTAI4GCHwcqtrFD4DHRHxR4k",
|
KeyID: "LTAI4GCHwcqtrFD4DHRHxR4k",
|
||||||
@ -76,64 +72,7 @@ func GenAliConfig(level Level) *AliLSConfig {
|
|||||||
Level: level,
|
Level: level,
|
||||||
FlushWhen: 0,
|
FlushWhen: 0,
|
||||||
}
|
}
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
func GenEsConfig(level Level) *EsConfig {
|
return l.SetLogger(logs.AdapterAliLS, c.String())
|
||||||
dsn := "http://es-cn-tl32xlfmu00015h34.public.elasticsearch.aliyuncs.com:9200/"
|
|
||||||
if os.Getenv(envkey) == "prod" || os.Getenv(envkey) == "" || os.Getenv(envkey) == "gray" {
|
|
||||||
dsn = "http://es-cn-tl32xlfmu00015h34.elasticsearch.aliyuncs.com:9200/"
|
|
||||||
}
|
|
||||||
|
|
||||||
c := &EsConfig{
|
|
||||||
Username: "kaifa_api",
|
|
||||||
Password: "2quYX3bTeahO",
|
|
||||||
Dsn: dsn,
|
|
||||||
Level: level,
|
|
||||||
Index: os.Getenv("GRLOG_APP_NAME"),
|
|
||||||
}
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
var adatperDropMapper = map[Adapter]func(l *Logger) error{
|
|
||||||
|
|
||||||
AdapterAliLs: func(l *Logger) error {
|
|
||||||
return l.BeeLogger.DelLogger(logs.AdapterAliLS)
|
|
||||||
},
|
|
||||||
|
|
||||||
AdapterFile: func(l *Logger) error {
|
|
||||||
return l.BeeLogger.DelLogger(logs.AdapterFile)
|
|
||||||
},
|
|
||||||
|
|
||||||
AdapterConsole: func(l *Logger) error {
|
|
||||||
return l.BeeLogger.DelLogger(logs.AdapterConsole)
|
|
||||||
},
|
|
||||||
|
|
||||||
AdapterElasticSearch: func(l *Logger) error {
|
|
||||||
return l.BeeLogger.DelLogger(logs.AdapterEs)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatLog(f interface{}, v ...interface{}) string {
|
|
||||||
var msg string
|
|
||||||
switch f.(type) {
|
|
||||||
case string:
|
|
||||||
msg = f.(string)
|
|
||||||
if len(v) == 0 {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
if strings.Contains(msg, "%") && !strings.Contains(msg, "%%") {
|
|
||||||
//format string
|
|
||||||
} else {
|
|
||||||
//do not contain format char
|
|
||||||
msg += strings.Repeat(" %v", len(v))
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
msg = fmt.Sprint(f)
|
|
||||||
if len(v) == 0 {
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
msg += strings.Repeat(" %v", len(v))
|
|
||||||
}
|
|
||||||
return fmt.Sprintf(msg, v...)
|
|
||||||
}
|
|
||||||
|
59
grlogs.go
59
grlogs.go
@ -1,59 +0,0 @@
|
|||||||
package grlogs
|
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
func Info(v ...interface{}) {
|
|
||||||
Get("grlogs").Info(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Informational(v ...interface{}) {
|
|
||||||
Get("grlogs").Informational(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Warning(v ...interface{}) {
|
|
||||||
Get("grlogs").Warning(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Warn(v ...interface{}) {
|
|
||||||
Get("grlogs").Warn(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Notice(v ...interface{}) {
|
|
||||||
Get("grlogs").Notice(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Error(v ...interface{}) {
|
|
||||||
Get("grlogs").Error(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Critical(v ...interface{}) {
|
|
||||||
Get("grlogs").Critical(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Alert(v ...interface{}) {
|
|
||||||
Get("grlogs").Alert(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Emergency(v ...interface{}) {
|
|
||||||
Get("grlogs").Emergency(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Trace(v ...interface{}) {
|
|
||||||
Get("grlogs").Trace(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Debug(v ...interface{}) {
|
|
||||||
Get("grlogs").Debug(generateFmtStr(len(v)), v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateFmtStr(n int) string {
|
|
||||||
return strings.Repeat("%v ", n)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetAdapter(level Level, adapter Adapter) {
|
|
||||||
Get("grlogs").SetAdapter(level, adapter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func DropAdapter(adapter Adapter) {
|
|
||||||
Get("grlogs").DropAdapter(adapter)
|
|
||||||
}
|
|
110
log.go
110
log.go
@ -11,21 +11,13 @@ var loggers = sync.Map{}
|
|||||||
|
|
||||||
var envkey = "CENTER_RUNMODE"
|
var envkey = "CENTER_RUNMODE"
|
||||||
|
|
||||||
var defaultModeMapping = map[string][]AdapterTupple{
|
|
||||||
"es": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterElasticSearch}},
|
|
||||||
"ali": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterAliLs}},
|
|
||||||
"": []AdapterTupple{{LevelAll, AdapterConsole}, {LevelAll, AdapterFile}},
|
|
||||||
}
|
|
||||||
|
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
Lable string
|
Lable string
|
||||||
*logs.BeeLogger
|
*logs.BeeLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
type Getter func() *Logger
|
|
||||||
|
|
||||||
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
||||||
if call, ok := adatperSetMapper[adapter]; ok {
|
if call, ok := adatperMapper[adapter]; ok {
|
||||||
if err := call(self, level); err != nil {
|
if err := call(self, level); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
@ -33,70 +25,65 @@ func (self *Logger) SetAdapter(level Level, adapter Adapter) *Logger {
|
|||||||
return self
|
return self
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *Logger) DropAdapter(adapter Adapter) *Logger {
|
func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
|
||||||
if call, ok := adatperDropMapper[adapter]; ok {
|
var channellens int64
|
||||||
if err := call(self); err != nil {
|
var tmp interface{}
|
||||||
fmt.Println(err)
|
|
||||||
}
|
tmp, loaded = loggers.LoadOrStore(label, new(Logger))
|
||||||
}
|
l = tmp.(*Logger)
|
||||||
return self
|
if len(channelLens) > 0 {
|
||||||
|
channellens = channelLens[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
func newLoggerFromMap(label string, defaultmode string, channelLens ...int64) Getter {
|
if !loaded {
|
||||||
|
|
||||||
if tmp, ok := loggers.Load(label); ok {
|
|
||||||
return tmp.(Getter)
|
|
||||||
}
|
|
||||||
|
|
||||||
var l *Logger
|
|
||||||
var once sync.Once
|
|
||||||
wapperGetter := Getter(func() *Logger {
|
|
||||||
once.Do(func() {
|
|
||||||
|
|
||||||
var channelLensNum int64 = 100
|
|
||||||
if len(channelLens) > 0 && channelLens[0] > 0 {
|
|
||||||
channelLensNum = channelLens[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
l = &Logger{BeeLogger: logs.NewLogger(channelLensNum)}
|
|
||||||
l.Lable = label
|
l.Lable = label
|
||||||
l.BeeLogger = logs.NewLogger(channelLensNum)
|
l.BeeLogger = logs.NewLogger(channellens)
|
||||||
l.BeeLogger.Lable = label
|
l.BeeLogger.Lable = label
|
||||||
l.Env = os.Getenv(envkey)
|
l.Env = os.Getenv(envkey)
|
||||||
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
||||||
l.EnableFuncCallDepth(true)
|
l.EnableFuncCallDepth(true)
|
||||||
l.SetLogFuncCallDepth(2)
|
l.SetLogFuncCallDepth(2)
|
||||||
if mode, ok := defaultModeMapping[defaultmode]; ok {
|
|
||||||
for _, v := range mode {
|
|
||||||
l.SetAdapter(v.Level, v.Adapter)
|
|
||||||
}
|
}
|
||||||
}
|
return
|
||||||
})
|
|
||||||
|
|
||||||
return l
|
|
||||||
})
|
|
||||||
|
|
||||||
tmp, loaded := loggers.LoadOrStore(label, wapperGetter)
|
|
||||||
if loaded {
|
|
||||||
return tmp.(Getter)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return wapperGetter
|
func Get(label string, channelLens ...int64) (l *Logger) {
|
||||||
|
var filelevel Level = LevelInfo
|
||||||
|
var loaded bool
|
||||||
|
if os.Getenv(envkey) == "dev" {
|
||||||
|
filelevel = LevelAll
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get(label string, channelLens ...int64) *Logger {
|
if l, loaded = New(label, channelLens...); !loaded {
|
||||||
getter := newLoggerFromMap(label, "", channelLens...)
|
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole)
|
||||||
return getter()
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetEs(label string, channelLens ...int64) *Logger {
|
func GetEs(label string, channelLens ...int64) (l *Logger) {
|
||||||
getter := newLoggerFromMap(label, "es", channelLens...)
|
var filelevel Level = LevelInfo
|
||||||
return getter()
|
var loaded bool
|
||||||
|
if os.Getenv(envkey) == "dev" {
|
||||||
|
filelevel = LevelAll
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAli(label string, channelLens ...int64) *Logger {
|
if l, loaded = New(label, channelLens...); !loaded {
|
||||||
getter := newLoggerFromMap(label, "ali", channelLens...)
|
l.SetAdapter(filelevel, AdapterFile).SetAdapter(LevelAll, AdapterConsole).SetAdapter(filelevel, AdapterElasticSearch)
|
||||||
return getter()
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAli(label string, channelLens ...int64) (l *Logger) {
|
||||||
|
var filelevel Level = LevelInfo
|
||||||
|
var loaded bool
|
||||||
|
if os.Getenv(envkey) == "dev" {
|
||||||
|
filelevel = LevelAll
|
||||||
|
}
|
||||||
|
|
||||||
|
if l, loaded = New(label, channelLens...); !loaded {
|
||||||
|
l.SetAdapter(LevelAll, AdapterConsole).SetAdapter(filelevel, AdapterFile).SetAdapter(filelevel, AdapterAliLs)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Close(lables ...string) {
|
func Close(lables ...string) {
|
||||||
@ -132,14 +119,3 @@ func CloseAll() {
|
|||||||
})
|
})
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
var level Level = LevelInfo
|
|
||||||
if os.Getenv(envkey) == "dev" {
|
|
||||||
level = LevelAll
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultModeMapping["es"] = []AdapterTupple{{level, AdapterConsole}, {level, AdapterElasticSearch}}
|
|
||||||
defaultModeMapping["ali"] = []AdapterTupple{{level, AdapterConsole}, {level, AdapterAliLs}}
|
|
||||||
defaultModeMapping[""] = []AdapterTupple{{level, AdapterConsole}, {level, AdapterFile}}
|
|
||||||
}
|
|
||||||
|
49
log_test.go
49
log_test.go
@ -1,10 +1,8 @@
|
|||||||
package grlogs
|
package grlogs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
_ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
|
_ "golib.gaore.com/GaoreGo/grlogs/logs/alils"
|
||||||
_ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
_ "golib.gaore.com/GaoreGo/grlogs/logs/es"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -49,50 +47,5 @@ func TestGetAliLs(t *testing.T) {
|
|||||||
l.Warn("Warn")
|
l.Warn("Warn")
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(time.Hour * 1)
|
Close(lable, lable, lable)
|
||||||
}
|
|
||||||
|
|
||||||
func TestDropAdapter(t *testing.T) {
|
|
||||||
SetAdapter(LevelAll, AdapterAliLs)
|
|
||||||
DropAdapter(AdapterAliLs)
|
|
||||||
Informational(errors.New("he hello"))
|
|
||||||
SetAdapter(LevelAll, AdapterAliLs)
|
|
||||||
Debug(errors.New("he hello"))
|
|
||||||
CloseAll()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(i int) {
|
|
||||||
GetAli("ok").Debug("%d", i)
|
|
||||||
time.Sleep(time.Second * 10)
|
|
||||||
wg.Done()
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
GetAli("ok").Debug("aaaaaa%d", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetEs(t *testing.T) {
|
|
||||||
wg := sync.WaitGroup{}
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(i int) {
|
|
||||||
GetEs("ok").Debug("%d", i)
|
|
||||||
time.Sleep(time.Second * 10)
|
|
||||||
wg.Done()
|
|
||||||
}(i)
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
GetEs("ok").Debug("aaaaaa%d", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/aliyun/aliyun-log-go-sdk/producer"
|
"github.com/aliyun/aliyun-log-go-sdk/producer"
|
||||||
"golib.gaore.com/GaoreGo/grlogs/logs"
|
"golib.gaore.com/GaoreGo/grlogs/logs"
|
||||||
"os"
|
|
||||||
"runtime"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,7 +27,6 @@ type alilsLogger struct {
|
|||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
Level int `json:"level"`
|
Level int `json:"level"`
|
||||||
FlushWhen int `json:"flush_when"`
|
FlushWhen int `json:"flush_when"`
|
||||||
Debug bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *alilsLogger) Init(jsonconfig string) error {
|
func (a *alilsLogger) Init(jsonconfig string) error {
|
||||||
@ -43,14 +40,11 @@ func (a *alilsLogger) Init(jsonconfig string) error {
|
|||||||
producerConfig.AccessKeyID = a.KeyID
|
producerConfig.AccessKeyID = a.KeyID
|
||||||
producerConfig.AccessKeySecret = a.KeySecret
|
producerConfig.AccessKeySecret = a.KeySecret
|
||||||
producerConfig.LingerMs = 100
|
producerConfig.LingerMs = 100
|
||||||
producerConfig.NoRetryStatusCodeList = []int{-1}
|
|
||||||
producerConfig.Retries = 2
|
|
||||||
producerConfig.AllowLogLevel = "error"
|
producerConfig.AllowLogLevel = "error"
|
||||||
producerConfig.MaxIoWorkerCount = int64(runtime.NumCPU())
|
|
||||||
a.producer = producer.InitProducer(producerConfig)
|
a.producer = producer.InitProducer(producerConfig)
|
||||||
a.callback = &Callback{}
|
a.callback = &Callback{}
|
||||||
a.producer.Start()
|
a.producer.Start()
|
||||||
a.Debug = os.Getenv("GRLOG_ALILS_DEBUG") == "on"
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,19 +62,12 @@ func (a *alilsLogger) WriteMsg(when time.Time, msg string, level int, lable stri
|
|||||||
|
|
||||||
log := producer.GenerateLog(uint32(when.Unix()), vals)
|
log := producer.GenerateLog(uint32(when.Unix()), vals)
|
||||||
|
|
||||||
if a.Debug {
|
|
||||||
for _, topic := range a.Topics {
|
|
||||||
if err := a.producer.SendLogWithCallBack(a.Project, a.LogStore, topic, a.Source, log, a.callback); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for _, topic := range a.Topics {
|
for _, topic := range a.Topics {
|
||||||
if err := a.producer.SendLog(a.Project, a.LogStore, topic, a.Source, log); err != nil {
|
if err := a.producer.SendLog(a.Project, a.LogStore, topic, a.Source, log); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,15 +11,15 @@ type Callback struct {
|
|||||||
func (callback *Callback) Success(result *producer.Result) {
|
func (callback *Callback) Success(result *producer.Result) {
|
||||||
attemptList := result.GetReservedAttempts()
|
attemptList := result.GetReservedAttempts()
|
||||||
for _, attempt := range attemptList {
|
for _, attempt := range attemptList {
|
||||||
fmt.Printf("alilog %+v \n", attempt)
|
fmt.Printf("%+v \n", attempt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (callback *Callback) Fail(result *producer.Result) {
|
func (callback *Callback) Fail(result *producer.Result) {
|
||||||
fmt.Println("IsSuccessful", result.IsSuccessful())
|
fmt.Println(result.IsSuccessful())
|
||||||
fmt.Println("GetErrorCode", result.GetErrorCode())
|
fmt.Println(result.GetErrorCode())
|
||||||
fmt.Println("GetErrorMessage", result.GetErrorMessage())
|
fmt.Println(result.GetErrorMessage())
|
||||||
fmt.Println("GetReservedAttempts", result.GetReservedAttempts())
|
fmt.Println(result.GetReservedAttempts())
|
||||||
fmt.Println("GetRequestId", result.GetRequestId())
|
fmt.Println(result.GetRequestId())
|
||||||
fmt.Println("GetTimeStampMs", result.GetTimeStampMs())
|
fmt.Println(result.GetTimeStampMs())
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ func (el *esLogger) WriteMsg(when time.Time, msg string, level int, lable string
|
|||||||
|
|
||||||
d := goes.Document{
|
d := goes.Document{
|
||||||
Index: fmt.Sprintf("%s-%04d.%02d.%02d", el.IndexName, when.Year(), when.Month(), when.Day()),
|
Index: fmt.Sprintf("%s-%04d.%02d.%02d", el.IndexName, when.Year(), when.Month(), when.Day()),
|
||||||
Type: "_doc",
|
Type: "logs",
|
||||||
Fields: vals,
|
Fields: vals,
|
||||||
}
|
}
|
||||||
_, err := el.Index(d, nil)
|
_, err := el.Index(d, nil)
|
||||||
|
@ -281,7 +281,7 @@ func testFileRotate(t *testing.T, fn1, fn2 string, daily, hourly bool) {
|
|||||||
fw.hourlyOpenDate = fw.hourlyOpenTime.Day()
|
fw.hourlyOpenDate = fw.hourlyOpenTime.Day()
|
||||||
}
|
}
|
||||||
|
|
||||||
fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug, "", "dev")
|
fw.WriteMsg(time.Now(), "this is a msg for test", LevelDebug)
|
||||||
|
|
||||||
for _, file := range []string{fn1, fn2} {
|
for _, file := range []string{fn1, fn2} {
|
||||||
_, err := os.Stat(file)
|
_, err := os.Stat(file)
|
||||||
|
11
options.go
11
options.go
@ -4,11 +4,6 @@ type Level int
|
|||||||
|
|
||||||
type Adapter string
|
type Adapter string
|
||||||
|
|
||||||
type AdapterTupple struct {
|
|
||||||
Level Level
|
|
||||||
Adapter Adapter
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_ Level = iota
|
_ Level = iota
|
||||||
LevelNone Level = iota
|
LevelNone Level = iota
|
||||||
@ -28,9 +23,3 @@ const (
|
|||||||
AdapterElasticSearch Adapter = "es"
|
AdapterElasticSearch Adapter = "es"
|
||||||
AdapterAliLs Adapter = "alils"
|
AdapterAliLs Adapter = "alils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
DEV = "dev"
|
|
||||||
GRAY = "gray"
|
|
||||||
PROD = "prod"
|
|
||||||
)
|
|
||||||
|
Loading…
Reference in New Issue
Block a user