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 { type EsConfig struct {
Dsn string `json:"dsn"` Username string `json:"username"`
Level Level `json:"level"` Password string `json:"password"`
Dsn string `json:"dsn"`
Level Level `json:"level"`
Index string `json:"index"`
} }
func (c *EsConfig) String() string { 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/" dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
if envkey == "prod" || envkey == "" { 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{ c := EsConfig{
Dsn: dsn, Username: "elastic",
Level: level, 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())
}, },
} }

View File

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

View File

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

View File

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

View File

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