v1.2.1 增加es管道写入日志

This commit is contained in:
liangzy 2020-04-28 15:27:39 +08:00
parent f0f0b1db40
commit 48f1a7b6ca
13 changed files with 41 additions and 15 deletions

View File

@ -35,7 +35,7 @@ var adatperMapper = map[Adapter]func(l *Logger, level Level) error{
AdapterElasticSearch: func(l *Logger, level Level) error { AdapterElasticSearch: 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 == "" || envkey == "gray" {
dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/" dsn = "http://es-cn-0pp1mm3hq000dnbh4.elasticsearch.aliyuncs.com:9200/"
} }

4
log.go
View File

@ -38,7 +38,11 @@ func New(label string, channelLens ...int64) (l *Logger, loaded bool) {
if !loaded { if !loaded {
l.Lable = label l.Lable = label
l.BeeLogger = logs.NewLogger(channellens) 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.SetPrefix(fmt.Sprintf("[env:%s logger:%s]", os.Getenv(envkey), label))
l.EnableFuncCallDepth(true)
l.SetLogFuncCallDepth(2)
} }
return return
} }

View File

@ -11,5 +11,9 @@ func TestGetLogger(t *testing.T) {
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))
l.Flush()
Get("wifi").SetAdapter(LevelAll, AdapterElasticSearch)
for i := 0; i < 10; i++ {
Get("wifi").Error("neoweiwoewe")
}
} }

View File

@ -103,7 +103,7 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) {
// WriteMsg write message in connection. // WriteMsg write message in connection.
// if connection is down, try to re-connect. // 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 { if level > c.Level {
return nil return nil

View File

@ -48,7 +48,7 @@ func (c *connWriter) Init(jsonConfig string) error {
// WriteMsg write message in connection. // WriteMsg write message in connection.
// if connection is down, try to re-connect. // 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 { if level > c.Level {
return nil return nil
} }

View File

@ -73,7 +73,7 @@ func (c *consoleWriter) Init(jsonConfig string) error {
} }
// WriteMsg write message in console. // 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 { if level > c.Level {
return nil return nil
} }

View File

@ -23,6 +23,8 @@ func NewES() logs.Logger {
type esLogger struct { type esLogger struct {
*goes.Client *goes.Client
Username string `json:"username"`
Password string `json:"password"`
DSN string `json:"dsn"` DSN string `json:"dsn"`
Level int `json:"level"` Level int `json:"level"`
IndexName string `json:"index"` IndexName string `json:"index"`
@ -53,6 +55,8 @@ func (el *esLogger) Init(jsonconfig string) error {
} }
conn := goes.NewClient(host, port) conn := goes.NewClient(host, port)
conn.AuthUsername = el.Username
conn.AuthPassword = el.Password
conn.WithHTTPClient(cl) conn.WithHTTPClient(cl)
el.Client = conn el.Client = conn
} }
@ -60,7 +64,7 @@ func (el *esLogger) Init(jsonconfig string) error {
} }
// WriteMsg will write the msg and level into es // 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 { if level > el.Level {
return nil 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["@timestamp"] = when.Format(time.RFC3339)
vals["@msg"] = msg vals["@msg"] = msg
vals["@level"] = level 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{ d := goes.Document{
Index: fmt.Sprintf("%s-%04d.%02d.%02d", el.IndexName, 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",

View File

@ -144,7 +144,7 @@ func (w *fileLogWriter) needRotateHourly(size int, hour int) bool {
} }
// WriteMsg write logger message into file. // 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 { if level > w.Level {
return nil return nil
} }

View File

@ -30,7 +30,7 @@ func (s *JLWriter) Init(jsonconfig string) error {
// WriteMsg write message in smtp writer. // WriteMsg write message in smtp writer.
// it will send an email with subject and only this message. // 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 { if level > s.Level {
return nil return nil
} }

View File

@ -86,7 +86,7 @@ type newLoggerFunc func() Logger
// Logger defines the behavior of a log provider. // Logger defines the behavior of a log provider.
type Logger interface { type Logger interface {
Init(config string) error 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() Destroy()
Flush() Flush()
} }
@ -122,6 +122,8 @@ type BeeLogger struct {
signalChan chan string signalChan chan string
wg sync.WaitGroup wg sync.WaitGroup
outputs []*nameLogger outputs []*nameLogger
Lable string
Env string
} }
const defaultAsyncMsgLen = 1e3 const defaultAsyncMsgLen = 1e3
@ -155,6 +157,10 @@ func NewLogger(channelLens ...int64) *BeeLogger {
return bl return bl
} }
func GetLevelString(level int) string {
return levelPrefix[level]
}
// Async set the log to asynchronous and start the goroutine // Async set the log to asynchronous and start the goroutine
func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger { func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger {
bl.lock.Lock() 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) { func (bl *BeeLogger) writeToLoggers(when time.Time, msg string, level int) {
for _, l := range bl.outputs { for _, l := range bl.outputs {
err := l.WriteMsg(when, msg, level) err := l.WriteMsg(when, msg, level, bl.Lable, bl.Env)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err) fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err)
} }

View File

@ -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 { 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++ { for i := 0; i < len(f.writers)-1; i++ {
if f.writers[i] != nil { if f.writers[i] != nil {
if level == f.writers[i].Level { if level == f.writers[i].Level {
f.writers[i].WriteMsg(when, msg, level) f.writers[i].WriteMsg(when, msg, level, lable, env)
} }
} }
} }

View File

@ -26,7 +26,7 @@ func (s *SLACKWriter) Init(jsonconfig string) error {
// WriteMsg write message in smtp writer. // WriteMsg write message in smtp writer.
// it will send an email with subject and only this message. // 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 { if level > s.Level {
return nil return nil
} }

View File

@ -117,7 +117,7 @@ func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd
// WriteMsg write message in smtp writer. // WriteMsg write message in smtp writer.
// it will send an email with subject and only this message. // 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 { if level > s.Level {
return nil return nil
} }