From 8178f035a08231ec7a04ab7a825ef1cacffac4d4 Mon Sep 17 00:00:00 2001 From: IamCathal Date: Fri, 28 Aug 2020 18:18:28 +0100 Subject: [PATCH] Custom formatting opts implementation --- pkg/logs/alils/alils.go | 36 ++++++++++++++++++++---------------- pkg/logs/conn.go | 40 ++++++++++++++++++++++++++-------------- pkg/logs/console.go | 1 - pkg/logs/es/es.go | 28 ++++++++++++++++------------ pkg/logs/file.go | 24 ++++++++++++++---------- pkg/logs/jianliao.go | 40 +++++++++++++++++++++++++--------------- pkg/logs/multifile.go | 24 +++++++++++++----------- pkg/logs/smtp.go | 19 +++++++++++-------- 8 files changed, 125 insertions(+), 87 deletions(-) diff --git a/pkg/logs/alils/alils.go b/pkg/logs/alils/alils.go index 183d9b24..425071f8 100644 --- a/pkg/logs/alils/alils.go +++ b/pkg/logs/alils/alils.go @@ -5,6 +5,7 @@ import ( "strings" "sync" + "github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/logs" "github.com/gogo/protobuf/proto" ) @@ -32,13 +33,12 @@ type Config struct { // aliLSWriter implements LoggerInterface. // Writes messages in keep-live tcp connection. type aliLSWriter struct { - store *LogStore - group []*LogGroup - withMap bool - groupMap map[string]*LogGroup - lock *sync.Mutex - UseCustomFormatter bool - CustomFormatter func(*logs.LogMsg) string + store *LogStore + group []*LogGroup + withMap bool + groupMap map[string]*LogGroup + lock *sync.Mutex + customFormatter func(*logs.LogMsg) string Config } @@ -50,15 +50,17 @@ func NewAliLS() logs.Logger { } // Init parses config and initializes struct -func (c *aliLSWriter) Init(jsonConfig string, LogFormatter ...func(*logs.LogMsg) string) (err error) { +func (c *aliLSWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { - for _, elem := range LogFormatter { - if elem != nil { - c.UseCustomFormatter = true - c.CustomFormatter = elem + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := logs.GetFormatter(elem) + if err != nil { + return err + } + c.customFormatter = formatter } } - json.Unmarshal([]byte(jsonConfig), c) if c.FlushWhen > CacheSize { @@ -72,11 +74,13 @@ func (c *aliLSWriter) Init(jsonConfig string, LogFormatter ...func(*logs.LogMsg) AccessKeySecret: c.KeySecret, } - c.store, err = prj.GetLogStore(c.LogStore) + store, err := prj.GetLogStore(c.LogStore) if err != nil { return err } + c.store = store + // Create default Log Group c.group = append(c.group, &LogGroup{ Topic: proto.String(""), @@ -141,8 +145,8 @@ func (c *aliLSWriter) WriteMsg(lm *logs.LogMsg) error { lg = c.group[0] } - if c.UseCustomFormatter { - content = c.CustomFormatter(lm) + if c.customFormatter != nil { + content = c.customFormatter(lm) } else { content = c.Format(lm) } diff --git a/pkg/logs/conn.go b/pkg/logs/conn.go index 55cbecdd..9a520bda 100644 --- a/pkg/logs/conn.go +++ b/pkg/logs/conn.go @@ -25,13 +25,14 @@ import ( // connWriter implements LoggerInterface. // Writes messages in keep-live tcp connection. type connWriter struct { - lg *logWriter - innerWriter io.WriteCloser - ReconnectOnMsg bool `json:"reconnectOnMsg"` - Reconnect bool `json:"reconnect"` - Net string `json:"net"` - Addr string `json:"addr"` - Level int `json:"level"` + lg *logWriter + innerWriter io.WriteCloser + customFormatter func(*LogMsg) string + ReconnectOnMsg bool `json:"reconnectOnMsg"` + Reconnect bool `json:"reconnect"` + Net string `json:"net"` + Addr string `json:"addr"` + Level int `json:"level"` } // NewConn creates new ConnWrite returning as LoggerInterface. @@ -48,12 +49,16 @@ func (c *connWriter) Format(lm *LogMsg) string { // Init initializes a connection writer with json config. // json config only needs they "level" key func (c *connWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { - // for _, elem := range LogFormatter { - // if elem != nil { - // c.UseCustomFormatter = true - // c.CustomFormatter = elem - // } - // } + + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := GetFormatter(elem) + if err != nil { + return err + } + c.customFormatter = formatter + } + } return json.Unmarshal([]byte(jsonConfig), c) } @@ -75,7 +80,14 @@ func (c *connWriter) WriteMsg(lm *LogMsg) error { defer c.innerWriter.Close() } - msg := c.Format(lm) + msg := "" + if c.customFormatter != nil { + msg = c.customFormatter(lm) + } else { + 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 55958008..34114e4a 100644 --- a/pkg/logs/console.go +++ b/pkg/logs/console.go @@ -81,7 +81,6 @@ func NewConsole() Logger { // Init initianlizes the console logger. // jsonConfig must be in the format '{"level":LevelTrace}' -// func (c *consoleWriter) Init(jsonConfig string, LogFormatter ...func(*LogMsg) string) error { func (c *consoleWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { for _, elem := range opts { diff --git a/pkg/logs/es/es.go b/pkg/logs/es/es.go index 4dfc4160..dc9304c8 100644 --- a/pkg/logs/es/es.go +++ b/pkg/logs/es/es.go @@ -12,6 +12,7 @@ import ( "github.com/elastic/go-elasticsearch/v6" "github.com/elastic/go-elasticsearch/v6/esapi" + "github.com/astaxie/beego/pkg/common" "github.com/astaxie/beego/pkg/logs" ) @@ -31,10 +32,9 @@ func NewES() logs.Logger { // import _ "github.com/astaxie/beego/logs/es" type esLogger struct { *elasticsearch.Client - DSN string `json:"dsn"` - Level int `json:"level"` - UseCustomFormatter bool - CustomFormatter func(*logs.LogMsg) string + DSN string `json:"dsn"` + Level int `json:"level"` + customFormatter func(*logs.LogMsg) string } func (el *esLogger) Format(lm *logs.LogMsg) string { @@ -42,15 +42,19 @@ func (el *esLogger) Format(lm *logs.LogMsg) string { } // {"dsn":"http://localhost:9200/","level":1} -func (el *esLogger) Init(jsonconfig string, LogFormatter ...func(*logs.LogMsg) string) error { - for _, elem := range LogFormatter { - if elem != nil { - el.UseCustomFormatter = true - el.CustomFormatter = elem +func (el *esLogger) Init(jsonConfig string, opts ...common.SimpleKV) error { + + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := logs.GetFormatter(elem) + if err != nil { + return err + } + el.customFormatter = formatter } } - err := json.Unmarshal([]byte(jsonconfig), el) + err := json.Unmarshal([]byte(jsonConfig), el) if err != nil { return err } @@ -79,8 +83,8 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error { } msg := "" - if el.UseCustomFormatter { - msg = el.CustomFormatter(lm) + if el.customFormatter != nil { + msg = el.customFormatter(lm) } else { msg = el.Format(lm) } diff --git a/pkg/logs/file.go b/pkg/logs/file.go index 0324486e..42148c3a 100644 --- a/pkg/logs/file.go +++ b/pkg/logs/file.go @@ -62,8 +62,7 @@ type fileLogWriter struct { hourlyOpenDate int hourlyOpenTime time.Time - UseCustomFormatter bool - CustomFormatter func(*LogMsg) string + customFormatter func(*LogMsg) string Rotate bool `json:"rotate"` @@ -110,12 +109,16 @@ func (w *fileLogWriter) Format(lm *LogMsg) string { // "perm":"0600" // } func (w *fileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { - // for _, elem := range LogFormatter { - // if elem != nil { - // w.UseCustomFormatter = true - // w.CustomFormatter = elem - // } - // } + + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := GetFormatter(elem) + if err != nil { + return err + } + w.customFormatter = formatter + } + } err := json.Unmarshal([]byte(jsonConfig), w) if err != nil { @@ -166,8 +169,9 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error { } hd, d, h := formatTimeHeader(lm.When) msg := "" - if w.UseCustomFormatter { - msg = w.CustomFormatter(lm) + + if w.customFormatter != nil { + msg = w.customFormatter(lm) } else { msg = w.Format(lm) } diff --git a/pkg/logs/jianliao.go b/pkg/logs/jianliao.go index 8daa8015..81d0195b 100644 --- a/pkg/logs/jianliao.go +++ b/pkg/logs/jianliao.go @@ -11,14 +11,13 @@ import ( // JLWriter implements beego LoggerInterface and is used to send jiaoliao webhook type JLWriter struct { - AuthorName string `json:"authorname"` - Title string `json:"title"` - WebhookURL string `json:"webhookurl"` - RedirectURL string `json:"redirecturl,omitempty"` - ImageURL string `json:"imageurl,omitempty"` - Level int `json:"level"` - UseCustomFormatter bool - CustomFormatter func(*LogMsg) string + AuthorName string `json:"authorname"` + Title string `json:"title"` + WebhookURL string `json:"webhookurl"` + RedirectURL string `json:"redirecturl,omitempty"` + ImageURL string `json:"imageurl,omitempty"` + Level int `json:"level"` + customFormatter func(*LogMsg) string } // newJLWriter creates jiaoliao writer. @@ -28,12 +27,15 @@ func newJLWriter() Logger { // Init JLWriter with json config string func (s *JLWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { - // for _, elem := range LogFormatter { - // if elem != nil { - // s.UseCustomFormatter = true - // s.CustomFormatter = elem - // } - // } + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := GetFormatter(elem) + if err != nil { + return err + } + s.customFormatter = formatter + } + } return json.Unmarshal([]byte(jsonConfig), s) } @@ -49,7 +51,15 @@ func (s *JLWriter) WriteMsg(lm *LogMsg) error { return nil } - text := fmt.Sprintf("%s %s", lm.When.Format("2006-01-02 15:04:05"), s.Format(lm)) + text := "" + + if s.customFormatter != nil { + text = fmt.Sprintf("%s %s", lm.When.Format("2006-01-02 15:04:05"), s.customFormatter(lm)) + } else { + 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/multifile.go b/pkg/logs/multifile.go index 720f5125..c1b7cfdd 100644 --- a/pkg/logs/multifile.go +++ b/pkg/logs/multifile.go @@ -26,11 +26,10 @@ import ( // and write the error-level logs to project.error.log and write the debug-level logs to project.debug.log // the rotate attribute also acts like fileLogWriter type multiFileLogWriter struct { - writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter - fullLogWriter *fileLogWriter - Separate []string `json:"separate"` - UseCustomFormatter bool - CustomFormatter func(*LogMsg) string + writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter + fullLogWriter *fileLogWriter + Separate []string `json:"separate"` + customFormatter func(*LogMsg) string } var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"} @@ -49,12 +48,15 @@ var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning // } func (f *multiFileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { - // for _, elem := range LogFormatter { - // if elem != nil { - // f.UseCustomFormatter = true - // f.CustomFormatter = elem - // } - // } + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := GetFormatter(elem) + if err != nil { + return err + } + f.customFormatter = formatter + } + } writer := newFileWriter().(*fileLogWriter) err := writer.Init(jsonConfig) diff --git a/pkg/logs/smtp.go b/pkg/logs/smtp.go index 17148812..9b67e343 100644 --- a/pkg/logs/smtp.go +++ b/pkg/logs/smtp.go @@ -34,8 +34,7 @@ type SMTPWriter struct { FromAddress string `json:"fromAddress"` RecipientAddresses []string `json:"sendTos"` Level int `json:"level"` - UseCustomFormatter bool - CustomFormatter func(*LogMsg) string + customFormatter func(*LogMsg) string } // NewSMTPWriter creates the smtp writer. @@ -55,12 +54,16 @@ func newSMTPWriter() Logger { // "level":LevelError // } func (s *SMTPWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { - // for _, elem := range LogFormatter { - // if elem != nil { - // s.UseCustomFormatter = true - // s.CustomFormatter = elem - // } - // } + + for _, elem := range opts { + if elem.Key == "formatter" { + formatter, err := GetFormatter(elem) + if err != nil { + return err + } + s.customFormatter = formatter + } + } return json.Unmarshal([]byte(jsonConfig), s) }