@@ -43,8 +43,11 @@ func (c *ConnLogConfig) String() string { | |||||
} | } | ||||
type EsConfig struct { | 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 { | func (c *EsConfig) String() string { | ||||
@@ -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, | |||||
Level: level, | |||||
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()) | ||||
}, | }, | ||||
} | } |
@@ -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)) | ||||
@@ -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 ( | ||||
@@ -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"` | |||||
Level int `json:"level"` | |||||
DSN string `json:"dsn"` | |||||
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, | ||||
} | } | ||||
@@ -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", "") | ||||