From 705e091593a49a09904c75896aec1f85aa3c8862 Mon Sep 17 00:00:00 2001 From: IamCathal Date: Thu, 20 Aug 2020 19:06:51 +0100 Subject: [PATCH] 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) }