diff --git a/funtion.go b/funtion.go index cec3387..15f2fd9 100644 --- a/funtion.go +++ b/funtion.go @@ -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/" } diff --git a/log.go b/log.go index 26941bc..1f12c07 100644 --- a/log.go +++ b/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 } diff --git a/log_test.go b/log_test.go index a23d1c9..88c0391 100644 --- a/log_test.go +++ b/log_test.go @@ -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") + } } diff --git a/logs/alils/alils.go b/logs/alils/alils.go index d098d4d..24910a5 100644 --- a/logs/alils/alils.go +++ b/logs/alils/alils.go @@ -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 diff --git a/logs/conn.go b/logs/conn.go index afe0cbb..4d69136 100644 --- a/logs/conn.go +++ b/logs/conn.go @@ -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 } diff --git a/logs/console.go b/logs/console.go index 3dcaee1..0608f7e 100644 --- a/logs/console.go +++ b/logs/console.go @@ -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 } diff --git a/logs/es/es.go b/logs/es/es.go index b41f197..fb6c75c 100644 --- a/logs/es/es.go +++ b/logs/es/es.go @@ -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", diff --git a/logs/file.go b/logs/file.go index 936afe5..0020023 100644 --- a/logs/file.go +++ b/logs/file.go @@ -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 } diff --git a/logs/jianliao.go b/logs/jianliao.go index 88ba0f9..08c7b80 100644 --- a/logs/jianliao.go +++ b/logs/jianliao.go @@ -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 } diff --git a/logs/log.go b/logs/log.go index 6a9c985..d94c8d5 100644 --- a/logs/log.go +++ b/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) } diff --git a/logs/multifile.go b/logs/multifile.go index 9016827..6ac326e 100644 --- a/logs/multifile.go +++ b/logs/multifile.go @@ -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) } } } diff --git a/logs/slack.go b/logs/slack.go index 1cd2e5a..ada21be 100644 --- a/logs/slack.go +++ b/logs/slack.go @@ -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 } diff --git a/logs/smtp.go b/logs/smtp.go index 6208d7b..96e60e6 100644 --- a/logs/smtp.go +++ b/logs/smtp.go @@ -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 }