1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 19:00:55 +00:00

Custom formatting opts implementation

This commit is contained in:
IamCathal 2020-08-28 18:18:28 +01:00
parent 2b39ff7837
commit 8178f035a0
8 changed files with 125 additions and 87 deletions

View File

@ -5,6 +5,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/astaxie/beego/pkg/common"
"github.com/astaxie/beego/pkg/logs" "github.com/astaxie/beego/pkg/logs"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
) )
@ -37,8 +38,7 @@ type aliLSWriter struct {
withMap bool withMap bool
groupMap map[string]*LogGroup groupMap map[string]*LogGroup
lock *sync.Mutex lock *sync.Mutex
UseCustomFormatter bool customFormatter func(*logs.LogMsg) string
CustomFormatter func(*logs.LogMsg) string
Config Config
} }
@ -50,15 +50,17 @@ func NewAliLS() logs.Logger {
} }
// Init parses config and initializes struct // 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 { for _, elem := range opts {
if elem != nil { if elem.Key == "formatter" {
c.UseCustomFormatter = true formatter, err := logs.GetFormatter(elem)
c.CustomFormatter = elem if err != nil {
return err
}
c.customFormatter = formatter
} }
} }
json.Unmarshal([]byte(jsonConfig), c) json.Unmarshal([]byte(jsonConfig), c)
if c.FlushWhen > CacheSize { if c.FlushWhen > CacheSize {
@ -72,11 +74,13 @@ func (c *aliLSWriter) Init(jsonConfig string, LogFormatter ...func(*logs.LogMsg)
AccessKeySecret: c.KeySecret, AccessKeySecret: c.KeySecret,
} }
c.store, err = prj.GetLogStore(c.LogStore) store, err := prj.GetLogStore(c.LogStore)
if err != nil { if err != nil {
return err return err
} }
c.store = store
// Create default Log Group // Create default Log Group
c.group = append(c.group, &LogGroup{ c.group = append(c.group, &LogGroup{
Topic: proto.String(""), Topic: proto.String(""),
@ -141,8 +145,8 @@ func (c *aliLSWriter) WriteMsg(lm *logs.LogMsg) error {
lg = c.group[0] lg = c.group[0]
} }
if c.UseCustomFormatter { if c.customFormatter != nil {
content = c.CustomFormatter(lm) content = c.customFormatter(lm)
} else { } else {
content = c.Format(lm) content = c.Format(lm)
} }

View File

@ -27,6 +27,7 @@ import (
type connWriter struct { type connWriter struct {
lg *logWriter lg *logWriter
innerWriter io.WriteCloser innerWriter io.WriteCloser
customFormatter func(*LogMsg) string
ReconnectOnMsg bool `json:"reconnectOnMsg"` ReconnectOnMsg bool `json:"reconnectOnMsg"`
Reconnect bool `json:"reconnect"` Reconnect bool `json:"reconnect"`
Net string `json:"net"` Net string `json:"net"`
@ -48,12 +49,16 @@ func (c *connWriter) Format(lm *LogMsg) string {
// Init initializes a connection writer with json config. // Init initializes a connection writer with json config.
// json config only needs they "level" key // json config only needs they "level" key
func (c *connWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { func (c *connWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
// for _, elem := range LogFormatter {
// if elem != nil { for _, elem := range opts {
// c.UseCustomFormatter = true if elem.Key == "formatter" {
// c.CustomFormatter = elem formatter, err := GetFormatter(elem)
// } if err != nil {
// } return err
}
c.customFormatter = formatter
}
}
return json.Unmarshal([]byte(jsonConfig), c) return json.Unmarshal([]byte(jsonConfig), c)
} }
@ -75,7 +80,14 @@ func (c *connWriter) WriteMsg(lm *LogMsg) error {
defer c.innerWriter.Close() 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) _, err := c.lg.writeln(msg)
if err != nil { if err != nil {
return err return err

View File

@ -81,7 +81,6 @@ func NewConsole() Logger {
// Init initianlizes the console logger. // Init initianlizes the console logger.
// jsonConfig must be in the format '{"level":LevelTrace}' // 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 { func (c *consoleWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
for _, elem := range opts { for _, elem := range opts {

View File

@ -12,6 +12,7 @@ import (
"github.com/elastic/go-elasticsearch/v6" "github.com/elastic/go-elasticsearch/v6"
"github.com/elastic/go-elasticsearch/v6/esapi" "github.com/elastic/go-elasticsearch/v6/esapi"
"github.com/astaxie/beego/pkg/common"
"github.com/astaxie/beego/pkg/logs" "github.com/astaxie/beego/pkg/logs"
) )
@ -33,8 +34,7 @@ type esLogger struct {
*elasticsearch.Client *elasticsearch.Client
DSN string `json:"dsn"` DSN string `json:"dsn"`
Level int `json:"level"` Level int `json:"level"`
UseCustomFormatter bool customFormatter func(*logs.LogMsg) string
CustomFormatter func(*logs.LogMsg) string
} }
func (el *esLogger) Format(lm *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} // {"dsn":"http://localhost:9200/","level":1}
func (el *esLogger) Init(jsonconfig string, LogFormatter ...func(*logs.LogMsg) string) error { func (el *esLogger) Init(jsonConfig string, opts ...common.SimpleKV) error {
for _, elem := range LogFormatter {
if elem != nil { for _, elem := range opts {
el.UseCustomFormatter = true if elem.Key == "formatter" {
el.CustomFormatter = elem 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 { if err != nil {
return err return err
} }
@ -79,8 +83,8 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
} }
msg := "" msg := ""
if el.UseCustomFormatter { if el.customFormatter != nil {
msg = el.CustomFormatter(lm) msg = el.customFormatter(lm)
} else { } else {
msg = el.Format(lm) msg = el.Format(lm)
} }

View File

@ -62,8 +62,7 @@ type fileLogWriter struct {
hourlyOpenDate int hourlyOpenDate int
hourlyOpenTime time.Time hourlyOpenTime time.Time
UseCustomFormatter bool customFormatter func(*LogMsg) string
CustomFormatter func(*LogMsg) string
Rotate bool `json:"rotate"` Rotate bool `json:"rotate"`
@ -110,12 +109,16 @@ func (w *fileLogWriter) Format(lm *LogMsg) string {
// "perm":"0600" // "perm":"0600"
// } // }
func (w *fileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { func (w *fileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
// for _, elem := range LogFormatter {
// if elem != nil { for _, elem := range opts {
// w.UseCustomFormatter = true if elem.Key == "formatter" {
// w.CustomFormatter = elem formatter, err := GetFormatter(elem)
// } if err != nil {
// } return err
}
w.customFormatter = formatter
}
}
err := json.Unmarshal([]byte(jsonConfig), w) err := json.Unmarshal([]byte(jsonConfig), w)
if err != nil { if err != nil {
@ -166,8 +169,9 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error {
} }
hd, d, h := formatTimeHeader(lm.When) hd, d, h := formatTimeHeader(lm.When)
msg := "" msg := ""
if w.UseCustomFormatter {
msg = w.CustomFormatter(lm) if w.customFormatter != nil {
msg = w.customFormatter(lm)
} else { } else {
msg = w.Format(lm) msg = w.Format(lm)
} }

View File

@ -17,8 +17,7 @@ type JLWriter struct {
RedirectURL string `json:"redirecturl,omitempty"` RedirectURL string `json:"redirecturl,omitempty"`
ImageURL string `json:"imageurl,omitempty"` ImageURL string `json:"imageurl,omitempty"`
Level int `json:"level"` Level int `json:"level"`
UseCustomFormatter bool customFormatter func(*LogMsg) string
CustomFormatter func(*LogMsg) string
} }
// newJLWriter creates jiaoliao writer. // newJLWriter creates jiaoliao writer.
@ -28,12 +27,15 @@ func newJLWriter() Logger {
// Init JLWriter with json config string // Init JLWriter with json config string
func (s *JLWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { func (s *JLWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
// for _, elem := range LogFormatter { for _, elem := range opts {
// if elem != nil { if elem.Key == "formatter" {
// s.UseCustomFormatter = true formatter, err := GetFormatter(elem)
// s.CustomFormatter = elem if err != nil {
// } return err
// } }
s.customFormatter = formatter
}
}
return json.Unmarshal([]byte(jsonConfig), s) return json.Unmarshal([]byte(jsonConfig), s)
} }
@ -49,7 +51,15 @@ func (s *JLWriter) WriteMsg(lm *LogMsg) error {
return nil 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 := url.Values{}
form.Add("authorName", s.AuthorName) form.Add("authorName", s.AuthorName)
form.Add("title", s.Title) form.Add("title", s.Title)

View File

@ -29,8 +29,7 @@ type multiFileLogWriter struct {
writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter writers [LevelDebug + 1 + 1]*fileLogWriter // the last one for fullLogWriter
fullLogWriter *fileLogWriter fullLogWriter *fileLogWriter
Separate []string `json:"separate"` Separate []string `json:"separate"`
UseCustomFormatter bool customFormatter func(*LogMsg) string
CustomFormatter func(*LogMsg) string
} }
var levelNames = [...]string{"emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"} 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 { func (f *multiFileLogWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
// for _, elem := range LogFormatter { for _, elem := range opts {
// if elem != nil { if elem.Key == "formatter" {
// f.UseCustomFormatter = true formatter, err := GetFormatter(elem)
// f.CustomFormatter = elem if err != nil {
// } return err
// } }
f.customFormatter = formatter
}
}
writer := newFileWriter().(*fileLogWriter) writer := newFileWriter().(*fileLogWriter)
err := writer.Init(jsonConfig) err := writer.Init(jsonConfig)

View File

@ -34,8 +34,7 @@ type SMTPWriter struct {
FromAddress string `json:"fromAddress"` FromAddress string `json:"fromAddress"`
RecipientAddresses []string `json:"sendTos"` RecipientAddresses []string `json:"sendTos"`
Level int `json:"level"` Level int `json:"level"`
UseCustomFormatter bool customFormatter func(*LogMsg) string
CustomFormatter func(*LogMsg) string
} }
// NewSMTPWriter creates the smtp writer. // NewSMTPWriter creates the smtp writer.
@ -55,12 +54,16 @@ func newSMTPWriter() Logger {
// "level":LevelError // "level":LevelError
// } // }
func (s *SMTPWriter) Init(jsonConfig string, opts ...common.SimpleKV) error { func (s *SMTPWriter) Init(jsonConfig string, opts ...common.SimpleKV) error {
// for _, elem := range LogFormatter {
// if elem != nil { for _, elem := range opts {
// s.UseCustomFormatter = true if elem.Key == "formatter" {
// s.CustomFormatter = elem formatter, err := GetFormatter(elem)
// } if err != nil {
// } return err
}
s.customFormatter = formatter
}
}
return json.Unmarshal([]byte(jsonConfig), s) return json.Unmarshal([]byte(jsonConfig), s)
} }