v1.2.1 增加es管道写入日志
This commit is contained in:
		
							parent
							
								
									e662190f7d
								
							
						
					
					
						commit
						f0f0b1db40
					
				@ -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 {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										10
									
								
								funtion.go
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								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())
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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))
 | 
			
		||||
 | 
			
		||||
@ -6,8 +6,8 @@ import (
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/astaxie/beego/logs"
 | 
			
		||||
	"github.com/gogo/protobuf/proto"
 | 
			
		||||
	"golib.gaore.com/GaoreGo/grlogs/logs"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
 | 
			
		||||
@ -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,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -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", "")
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user