diff --git a/config.go b/config.go index 39b1348..12d02f1 100644 --- a/config.go +++ b/config.go @@ -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 { diff --git a/funtion.go b/funtion.go index 1d714fd..cec3387 100644 --- a/funtion.go +++ b/funtion.go @@ -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()) }, } diff --git a/log_test.go b/log_test.go index 1e7b701..a23d1c9 100644 --- a/log_test.go +++ b/log_test.go @@ -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)) diff --git a/logs/alils/alils.go b/logs/alils/alils.go index 867ff4c..d098d4d 100644 --- a/logs/alils/alils.go +++ b/logs/alils/alils.go @@ -6,8 +6,8 @@ import ( "sync" "time" - "github.com/astaxie/beego/logs" "github.com/gogo/protobuf/proto" + "golib.gaore.com/GaoreGo/grlogs/logs" ) const ( diff --git a/logs/es/es.go b/logs/es/es.go index 863244d..b41f197 100644 --- a/logs/es/es.go +++ b/logs/es/es.go @@ -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, } diff --git a/logs/log.go b/logs/log.go index 49f3794..6a9c985 100644 --- a/logs/log.go +++ b/logs/log.go @@ -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", "")