|
|
@@ -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, |
|
|
|
} |
|
|
|