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 {