v1.2.1 增加es管道写入日志

This commit is contained in:
liangzy 2020-04-28 14:32:56 +08:00
parent e662190f7d
commit f0f0b1db40
6 changed files with 33 additions and 12 deletions

View File

@ -43,8 +43,11 @@ func (c *ConnLogConfig) String() string {
}
type EsConfig struct {
Dsn string `json:"dsn"`
Level Level `json:"level"`
Username string `json:"username"`
Password string `json:"password"`
Dsn string `json:"dsn"`
Level Level `json:"level"`
Index string `json:"index"`
}
func (c *EsConfig) String() string {

View File

@ -36,13 +36,17 @@ var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
if envkey == "prod" || envkey == "" {
dsn = "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/"
}
c := EsConfig{
Dsn: dsn,
Level: level,
Username: "elastic",
Password: "Hellogaore@",
Dsn: dsn,
Level: level,
Index: os.Getenv("GRLOG_APP_NAME"),
}
return l.SetLogger(logs.AdapterEs, c.String())
},
}

View File

@ -1,12 +1,13 @@
package grlogs
import (
_ "golib.gaore.com/GaoreGo/grlogs/logs/es"
"testing"
"time"
)
func TestGetLogger(t *testing.T) {
l := Get("nds", 128)
l := Get("nds", 128).SetAdapter(LevelAll, AdapterElasticSearch)
l.Debug("我正在调试")
l.Critical("出错了")
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))

View File

@ -6,8 +6,8 @@ import (
"sync"
"time"
"github.com/astaxie/beego/logs"
"github.com/gogo/protobuf/proto"
"golib.gaore.com/GaoreGo/grlogs/logs"
)
const (

View File

@ -5,10 +5,11 @@ import (
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
"github.com/OwnLocal/goes"
"golib.gaore.com/GaoreGo/goes"
"golib.gaore.com/GaoreGo/grlogs/logs"
)
@ -22,8 +23,9 @@ func NewES() logs.Logger {
type esLogger struct {
*goes.Client
DSN string `json:"dsn"`
Level int `json:"level"`
DSN string `json:"dsn"`
Level int `json:"level"`
IndexName string `json:"index"`
}
// {"dsn":"http://localhost:9200/","level":1}
@ -41,7 +43,17 @@ func (el *esLogger) Init(jsonconfig string) error {
} else if host, port, err := net.SplitHostPort(u.Host); err != nil {
return err
} else {
tr := &http.Transport{
ResponseHeaderTimeout: 3 * time.Second,
}
cl := &http.Client{
Transport: tr,
}
conn := goes.NewClient(host, port)
conn.WithHTTPClient(cl)
el.Client = conn
}
return nil
@ -56,8 +68,9 @@ func (el *esLogger) WriteMsg(when time.Time, msg string, level int) error {
vals := make(map[string]interface{})
vals["@timestamp"] = when.Format(time.RFC3339)
vals["@msg"] = msg
vals["@level"] = level
d := goes.Document{
Index: fmt.Sprintf("%04d.%02d.%02d", when.Year(), when.Month(), when.Day()),
Index: fmt.Sprintf("%s-%04d.%02d.%02d", el.IndexName, when.Year(), when.Month(), when.Day()),
Type: "logs",
Fields: vals,
}

View File

@ -15,7 +15,7 @@
// Package logs provide a general log interface
// Usage:
//
// import "github.com/astaxie/beego/logs"
// import "golib.gaore.com/GaoreGo/grlogs/logs"
//
// log := NewLogger(10000)
// log.SetLogger("console", "")