From 6bdedff45714b42f5bca6e4959b4771ad031fa9b Mon Sep 17 00:00:00 2001 From: IamCathal Date: Thu, 20 Aug 2020 19:00:35 +0100 Subject: [PATCH 1/4] LogFormatter Implementation --- pkg/logs/conn.go | 7 ++++++- pkg/logs/console.go | 18 +++++++++++++++++- pkg/logs/es/es.go | 4 ++++ pkg/logs/file.go | 4 ++++ pkg/logs/jianliao.go | 4 ++++ pkg/logs/log.go | 12 ++++++++++++ pkg/logs/logger.go | 6 +++--- pkg/logs/multifile.go | 4 ++++ pkg/logs/slack.go | 4 ++++ pkg/logs/smtp.go | 4 ++++ 10 files changed, 62 insertions(+), 5 deletions(-) diff --git a/pkg/logs/conn.go b/pkg/logs/conn.go index e0560fd9..79ab410c 100644 --- a/pkg/logs/conn.go +++ b/pkg/logs/conn.go @@ -39,6 +39,10 @@ func NewConn() Logger { return conn } +func (c *connWriter) Format(lm *LogMsg) string { + return lm.Msg +} + // Init initializes a connection writer with json config. // json config only needs they "level" key func (c *connWriter) Init(jsonConfig string) error { @@ -62,7 +66,8 @@ func (c *connWriter) WriteMsg(lm *LogMsg) error { defer c.innerWriter.Close() } - _, err := c.lg.writeln(lm) + msg := c.Format(lm) + _, err := c.lg.writeln(msg) if err != nil { return err } diff --git a/pkg/logs/console.go b/pkg/logs/console.go index 024152aa..86db6178 100644 --- a/pkg/logs/console.go +++ b/pkg/logs/console.go @@ -52,6 +52,20 @@ type consoleWriter struct { Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color } +func (c *consoleWriter) Format(lm *LogMsg) string { + msg := lm.Msg + + if c.Colorful { + msg = strings.Replace(lm.Msg, levelPrefix[lm.Level], colors[lm.Level](levelPrefix[lm.Level]), 1) + } + + h, _, _ := formatTimeHeader(lm.When) + bytes := append(append(h, msg...), '\n') + + return "eee" + string(bytes) + +} + // NewConsole creates ConsoleWriter returning as LoggerInterface. func NewConsole() Logger { cw := &consoleWriter{ @@ -76,10 +90,12 @@ func (c *consoleWriter) WriteMsg(lm *LogMsg) error { if lm.Level > c.Level { return nil } + // fmt.Printf("Formatted: %s\n\n", c.fmtter.Format(lm)) if c.Colorful { lm.Msg = strings.Replace(lm.Msg, levelPrefix[lm.Level], colors[lm.Level](levelPrefix[lm.Level]), 1) } - c.lg.writeln(lm) + msg := c.Format(lm) + c.lg.writeln(msg) return nil } diff --git a/pkg/logs/es/es.go b/pkg/logs/es/es.go index 5c91b2ed..b70e5cf3 100644 --- a/pkg/logs/es/es.go +++ b/pkg/logs/es/es.go @@ -35,6 +35,10 @@ type esLogger struct { Level int `json:"level"` } +func (el *esLogger) Format(lm *logs.LogMsg) string { + return lm.Msg +} + // {"dsn":"http://localhost:9200/","level":1} func (el *esLogger) Init(jsonconfig string) error { err := json.Unmarshal([]byte(jsonconfig), el) diff --git a/pkg/logs/file.go b/pkg/logs/file.go index 23ea4b09..6b33ebb1 100644 --- a/pkg/logs/file.go +++ b/pkg/logs/file.go @@ -89,6 +89,10 @@ func newFileWriter() Logger { return w } +func (w *fileLogWriter) Format(lm *LogMsg) string { + return lm.Msg +} + // Init file logger with json config. // jsonConfig like: // { diff --git a/pkg/logs/jianliao.go b/pkg/logs/jianliao.go index 0e7cfab4..a108342c 100644 --- a/pkg/logs/jianliao.go +++ b/pkg/logs/jianliao.go @@ -27,6 +27,10 @@ func (s *JLWriter) Init(jsonconfig string) error { return json.Unmarshal([]byte(jsonconfig), s) } +func (s *JLWriter) Format(lm *LogMsg) string { + return lm.Msg +} + // WriteMsg writes message in smtp writer. // Sends an email with subject and only this message. func (s *JLWriter) WriteMsg(lm *LogMsg) error { diff --git a/pkg/logs/log.go b/pkg/logs/log.go index 37421625..d47173e5 100644 --- a/pkg/logs/log.go +++ b/pkg/logs/log.go @@ -86,6 +86,7 @@ type newLoggerFunc func() Logger type Logger interface { Init(config string) error WriteMsg(lm *LogMsg) error + Format(lm *LogMsg) string Destroy() Flush() } @@ -128,6 +129,8 @@ const defaultAsyncMsgLen = 1e3 type nameLogger struct { Logger + // Formatter func(*LogMsg) string + LogFormatter name string } @@ -139,6 +142,10 @@ type LogMsg struct { LineNumber int } +type LogFormatter interface { + Format(lm *LogMsg) string +} + var logMsgPool *sync.Pool // NewLogger returns a new BeeLogger. @@ -179,6 +186,10 @@ func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger { return bl } +func Format(lm *LogMsg) string { + return lm.Msg +} + // SetLogger provides a given logger adapter into BeeLogger with config string. // config must in in JSON format like {"interval":360}} func (bl *BeeLogger) setLogger(adapterName string, configs ...string) error { @@ -237,6 +248,7 @@ func (bl *BeeLogger) DelLogger(adapterName string) error { func (bl *BeeLogger) writeToLoggers(lm *LogMsg) { for _, l := range bl.outputs { + // fmt.Println("Formatted: ", l.Format(lm)) err := l.WriteMsg(lm) if err != nil { fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err) diff --git a/pkg/logs/logger.go b/pkg/logs/logger.go index 721c8dc1..d8b334d4 100644 --- a/pkg/logs/logger.go +++ b/pkg/logs/logger.go @@ -30,10 +30,10 @@ func newLogWriter(wr io.Writer) *logWriter { return &logWriter{writer: wr} } -func (lg *logWriter) writeln(lm *LogMsg) (int, error) { +func (lg *logWriter) writeln(msg string) (int, error) { lg.Lock() - h, _, _ := formatTimeHeader(lm.When) - n, err := lg.writer.Write(append(append(h, lm.Msg...), '\n')) + msg += "\n" + n, err := lg.writer.Write([]byte(msg)) lg.Unlock() return n, err } diff --git a/pkg/logs/multifile.go b/pkg/logs/multifile.go index 1cd9e9f8..0650c99d 100644 --- a/pkg/logs/multifile.go +++ b/pkg/logs/multifile.go @@ -78,6 +78,10 @@ func (f *multiFileLogWriter) Init(config string) error { return nil } +func (f *multiFileLogWriter) Format(lm *LogMsg) string { + return lm.Msg +} + func (f *multiFileLogWriter) Destroy() { for i := 0; i < len(f.writers); i++ { if f.writers[i] != nil { diff --git a/pkg/logs/slack.go b/pkg/logs/slack.go index dad4f4ea..c31f9330 100644 --- a/pkg/logs/slack.go +++ b/pkg/logs/slack.go @@ -18,6 +18,10 @@ func newSLACKWriter() Logger { return &SLACKWriter{Level: LevelTrace} } +func (s *SLACKWriter) Format(lm *LogMsg) string { + return lm.Msg +} + // Init SLACKWriter with json config string func (s *SLACKWriter) Init(jsonconfig string) error { return json.Unmarshal([]byte(jsonconfig), s) diff --git a/pkg/logs/smtp.go b/pkg/logs/smtp.go index 0d2b3c29..beadb0d7 100644 --- a/pkg/logs/smtp.go +++ b/pkg/logs/smtp.go @@ -114,6 +114,10 @@ func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd return client.Quit() } +func (s *SMTPWriter) Format(lm *LogMsg) string { + return lm.Msg +} + // WriteMsg writes message in smtp writer. // Sends an email with subject and only this message. func (s *SMTPWriter) WriteMsg(lm *LogMsg) error { From 705e091593a49a09904c75896aec1f85aa3c8862 Mon Sep 17 00:00:00 2001 From: IamCathal Date: Thu, 20 Aug 2020 19:06:51 +0100 Subject: [PATCH 2/4] Add format call before logging --- pkg/logs/es/es.go | 2 +- pkg/logs/file.go | 7 ++++--- pkg/logs/jianliao.go | 3 +-- pkg/logs/slack.go | 4 ++-- pkg/logs/smtp.go | 4 +++- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/logs/es/es.go b/pkg/logs/es/es.go index b70e5cf3..06dfece1 100644 --- a/pkg/logs/es/es.go +++ b/pkg/logs/es/es.go @@ -71,7 +71,7 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error { idx := LogDocument{ Timestamp: lm.When.Format(time.RFC3339), - Msg: lm.Msg, + Msg: el.Format(lm), } body, err := json.Marshal(idx) diff --git a/pkg/logs/file.go b/pkg/logs/file.go index 6b33ebb1..366fbcf2 100644 --- a/pkg/logs/file.go +++ b/pkg/logs/file.go @@ -153,7 +153,8 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error { return nil } hd, d, h := formatTimeHeader(lm.When) - lm.Msg = string(hd) + lm.Msg + "\n" + msg := w.Format(lm) + msg = fmt.Sprintf("%s %s\n", string(hd), msg) if w.Rotate { w.RLock() if w.needRotateHourly(len(lm.Msg), h) { @@ -180,10 +181,10 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error { } w.Lock() - _, err := w.fileWriter.Write([]byte(lm.Msg)) + _, err := w.fileWriter.Write([]byte(msg)) if err == nil { w.maxLinesCurLines++ - w.maxSizeCurSize += len(lm.Msg) + w.maxSizeCurSize += len(msg) } w.Unlock() return err diff --git a/pkg/logs/jianliao.go b/pkg/logs/jianliao.go index a108342c..6830bade 100644 --- a/pkg/logs/jianliao.go +++ b/pkg/logs/jianliao.go @@ -38,8 +38,7 @@ func (s *JLWriter) WriteMsg(lm *LogMsg) error { return nil } - text := fmt.Sprintf("%s %s", lm.When.Format("2006-01-02 15:04:05"), lm.Msg) - + text := fmt.Sprintf("%s %s", lm.When.Format("2006-01-02 15:04:05"), s.Format(lm)) form := url.Values{} form.Add("authorName", s.AuthorName) form.Add("title", s.Title) diff --git a/pkg/logs/slack.go b/pkg/logs/slack.go index c31f9330..c0584f72 100644 --- a/pkg/logs/slack.go +++ b/pkg/logs/slack.go @@ -33,8 +33,8 @@ func (s *SLACKWriter) WriteMsg(lm *LogMsg) error { if lm.Level > s.Level { return nil } - - text := fmt.Sprintf("{\"text\": \"%s %s\"}", lm.When.Format("2006-01-02 15:04:05"), lm.Msg) + msg := s.Format(lm) + text := fmt.Sprintf("{\"text\": \"%s %s\"}", lm.When.Format("2006-01-02 15:04:05"), msg) form := url.Values{} form.Add("payload", text) diff --git a/pkg/logs/smtp.go b/pkg/logs/smtp.go index beadb0d7..d992b279 100644 --- a/pkg/logs/smtp.go +++ b/pkg/logs/smtp.go @@ -130,11 +130,13 @@ func (s *SMTPWriter) WriteMsg(lm *LogMsg) error { // Set up authentication information. auth := s.getSMTPAuth(hp[0]) + msg := s.Format(lm) + // Connect to the server, authenticate, set the sender and recipient, // and send the email all in one step. contentType := "Content-Type: text/plain" + "; charset=UTF-8" mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress + - ">\r\nSubject: " + s.Subject + "\r\n" + contentType + "\r\n\r\n" + fmt.Sprintf(".%s", lm.When.Format("2006-01-02 15:04:05")) + lm.Msg) + ">\r\nSubject: " + s.Subject + "\r\n" + contentType + "\r\n\r\n" + fmt.Sprintf(".%s", lm.When.Format("2006-01-02 15:04:05")) + msg) return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg) } From e1da804b2ba54572dc44c76b963d63068a92bad8 Mon Sep 17 00:00:00 2001 From: IamCathal Date: Thu, 20 Aug 2020 19:15:27 +0100 Subject: [PATCH 3/4] Add format func to alils --- pkg/logs/alils/alils.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/logs/alils/alils.go b/pkg/logs/alils/alils.go index 6c1464f2..2c83e4ee 100644 --- a/pkg/logs/alils/alils.go +++ b/pkg/logs/alils/alils.go @@ -100,6 +100,10 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) { return nil } +func (c *aliLSWriter) Format(lm *logs.LogMsg) string { + return lm.Msg +} + // WriteMsg writes a message in connection. // If connection is down, try to re-connect. func (c *aliLSWriter) WriteMsg(lm *logs.LogMsg) error { From 08e49ca3233350f56cce2b57e7bc761c5a9ea277 Mon Sep 17 00:00:00 2001 From: IamCathal Date: Thu, 20 Aug 2020 19:32:42 +0100 Subject: [PATCH 4/4] Test empty commit