v1.2.1 增加es管道写入日志
This commit is contained in:
parent
f0f0b1db40
commit
48f1a7b6ca
@ -35,7 +35,7 @@ var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
|
||||
AdapterElasticSearch: func(l *Logger, level Level) error {
|
||||
|
||||
dsn := "http://es-cn-0pp1mm3hq000dnbh4.public.elasticsearch.aliyuncs.com:9200/"
|
||||
if envkey == "prod" || envkey == "" {
|
||||
if envkey == "prod" || envkey == "" || envkey == "gray" {
|
||||
dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/"
|
||||
}
|
||||
|
||||
|
4
log.go
4
log.go
@ -38,7 +38,11 @@ func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
|
||||
if !loaded {
|
||||
l.Lable = label
|
||||
l.BeeLogger = logs.NewLogger(channellens)
|
||||
l.BeeLogger.Lable = label
|
||||
l.Env = os.Getenv(envkey)
|
||||
l.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
|
||||
l.EnableFuncCallDepth(true)
|
||||
l.SetLogFuncCallDepth(2)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -11,5 +11,9 @@ func TestGetLogger(t *testing.T) {
|
||||
l.Debug("我正在调试")
|
||||
l.Critical("出错了")
|
||||
Get("nds").Warning("hadoee %s", time.Now().Format(time.RFC1123))
|
||||
l.Flush()
|
||||
|
||||
Get("wifi").SetAdapter(LevelAll, AdapterElasticSearch)
|
||||
for i := 0; i < 10; i++ {
|
||||
Get("wifi").Error("neoweiwoewe")
|
||||
}
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) {
|
||||
|
||||
// WriteMsg write message in connection.
|
||||
// if connection is down, try to re-connect.
|
||||
func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int) (err error) {
|
||||
func (c *aliLSWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) (err error) {
|
||||
|
||||
if level > c.Level {
|
||||
return nil
|
||||
|
@ -48,7 +48,7 @@ func (c *connWriter) Init(jsonConfig string) error {
|
||||
|
||||
// WriteMsg write message in connection.
|
||||
// if connection is down, try to re-connect.
|
||||
func (c *connWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (c *connWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > c.Level {
|
||||
return nil
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ func (c *consoleWriter) Init(jsonConfig string) error {
|
||||
}
|
||||
|
||||
// WriteMsg write message in console.
|
||||
func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > c.Level {
|
||||
return nil
|
||||
}
|
||||
|
@ -23,6 +23,8 @@ func NewES() logs.Logger {
|
||||
|
||||
type esLogger struct {
|
||||
*goes.Client
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
DSN string `json:"dsn"`
|
||||
Level int `json:"level"`
|
||||
IndexName string `json:"index"`
|
||||
@ -53,6 +55,8 @@ func (el *esLogger) Init(jsonconfig string) error {
|
||||
}
|
||||
|
||||
conn := goes.NewClient(host, port)
|
||||
conn.AuthUsername = el.Username
|
||||
conn.AuthPassword = el.Password
|
||||
conn.WithHTTPClient(cl)
|
||||
el.Client = conn
|
||||
}
|
||||
@ -60,7 +64,7 @@ func (el *esLogger) Init(jsonconfig string) error {
|
||||
}
|
||||
|
||||
// WriteMsg will write the msg and level into es
|
||||
func (el *esLogger) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (el *esLogger) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > el.Level {
|
||||
return nil
|
||||
}
|
||||
@ -69,6 +73,14 @@ func (el *esLogger) WriteMsg(when time.Time, msg string, level int) error {
|
||||
vals["@timestamp"] = when.Format(time.RFC3339)
|
||||
vals["@msg"] = msg
|
||||
vals["@level"] = level
|
||||
vals["level_string"] = logs.GetLevelString(level)
|
||||
vals["env"] = env
|
||||
vals["lable"] = lable
|
||||
|
||||
if el.IndexName == "" {
|
||||
return errors.New("index name is empty")
|
||||
}
|
||||
|
||||
d := goes.Document{
|
||||
Index: fmt.Sprintf("%s-%04d.%02d.%02d", el.IndexName, when.Year(), when.Month(), when.Day()),
|
||||
Type: "logs",
|
||||
|
@ -144,7 +144,7 @@ func (w *fileLogWriter) needRotateHourly(size int, hour int) bool {
|
||||
}
|
||||
|
||||
// WriteMsg write logger message into file.
|
||||
func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > w.Level {
|
||||
return nil
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ func (s *JLWriter) Init(jsonconfig string) error {
|
||||
|
||||
// WriteMsg write message in smtp writer.
|
||||
// it will send an email with subject and only this message.
|
||||
func (s *JLWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (s *JLWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > s.Level {
|
||||
return nil
|
||||
}
|
||||
|
10
logs/log.go
10
logs/log.go
@ -86,7 +86,7 @@ type newLoggerFunc func() Logger
|
||||
// Logger defines the behavior of a log provider.
|
||||
type Logger interface {
|
||||
Init(config string) error
|
||||
WriteMsg(when time.Time, msg string, level int) error
|
||||
WriteMsg(when time.Time, msg string, level int, lable string, env string) error
|
||||
Destroy()
|
||||
Flush()
|
||||
}
|
||||
@ -122,6 +122,8 @@ type BeeLogger struct {
|
||||
signalChan chan string
|
||||
wg sync.WaitGroup
|
||||
outputs []*nameLogger
|
||||
Lable string
|
||||
Env string
|
||||
}
|
||||
|
||||
const defaultAsyncMsgLen = 1e3
|
||||
@ -155,6 +157,10 @@ func NewLogger(channelLens ...int64) *BeeLogger {
|
||||
return bl
|
||||
}
|
||||
|
||||
func GetLevelString(level int) string {
|
||||
return levelPrefix[level]
|
||||
}
|
||||
|
||||
// Async set the log to asynchronous and start the goroutine
|
||||
func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger {
|
||||
bl.lock.Lock()
|
||||
@ -235,7 +241,7 @@ func (bl *BeeLogger) DelLogger(adapterName string) error {
|
||||
|
||||
func (bl *BeeLogger) writeToLoggers(when time.Time, msg string, level int) {
|
||||
for _, l := range bl.outputs {
|
||||
err := l.WriteMsg(when, msg, level)
|
||||
err := l.WriteMsg(when, msg, level, bl.Lable, bl.Env)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err)
|
||||
}
|
||||
|
@ -87,14 +87,14 @@ func (f *multiFileLogWriter) Destroy() {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *multiFileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (f *multiFileLogWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if f.fullLogWriter != nil {
|
||||
f.fullLogWriter.WriteMsg(when, msg, level)
|
||||
f.fullLogWriter.WriteMsg(when, msg, level, lable, env)
|
||||
}
|
||||
for i := 0; i < len(f.writers)-1; i++ {
|
||||
if f.writers[i] != nil {
|
||||
if level == f.writers[i].Level {
|
||||
f.writers[i].WriteMsg(when, msg, level)
|
||||
f.writers[i].WriteMsg(when, msg, level, lable, env)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ func (s *SLACKWriter) Init(jsonconfig string) error {
|
||||
|
||||
// WriteMsg write message in smtp writer.
|
||||
// it will send an email with subject and only this message.
|
||||
func (s *SLACKWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (s *SLACKWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > s.Level {
|
||||
return nil
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd
|
||||
|
||||
// WriteMsg write message in smtp writer.
|
||||
// it will send an email with subject and only this message.
|
||||
func (s *SMTPWriter) WriteMsg(when time.Time, msg string, level int) error {
|
||||
func (s *SMTPWriter) WriteMsg(when time.Time, msg string, level int, lable string, env string) error {
|
||||
if level > s.Level {
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user