1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 07: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"
) )
@ -32,13 +33,12 @@ type Config struct {
// aliLSWriter implements LoggerInterface. // aliLSWriter implements LoggerInterface.
// Writes messages in keep-live tcp connection. // Writes messages in keep-live tcp connection.
type aliLSWriter struct { type aliLSWriter struct {
store *LogStore store *LogStore
group []*LogGroup group []*LogGroup
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

@ -25,13 +25,14 @@ import (
// connWriter implements LoggerInterface. // connWriter implements LoggerInterface.
// Writes messages in keep-live tcp connection. // Writes messages in keep-live tcp connection.
type connWriter struct { type connWriter struct {
lg *logWriter lg *logWriter
innerWriter io.WriteCloser innerWriter io.WriteCloser
ReconnectOnMsg bool `json:"reconnectOnMsg"` customFormatter func(*LogMsg) string
Reconnect bool `json:"reconnect"` ReconnectOnMsg bool `json:"reconnectOnMsg"`
Net string `json:"net"` Reconnect bool `json:"reconnect"`
Addr string `json:"addr"` Net string `json:"net"`
Level int `json:"level"` Addr string `json:"addr"`
Level int `json:"level"`
} }
// NewConn creates new ConnWrite returning as LoggerInterface. // 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. // 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"
) )
@ -31,10 +32,9 @@ func NewES() logs.Logger {
// import _ "github.com/astaxie/beego/logs/es" // import _ "github.com/astaxie/beego/logs/es"
type esLogger struct { 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

@ -11,14 +11,13 @@ import (
// JLWriter implements beego LoggerInterface and is used to send jiaoliao webhook // JLWriter implements beego LoggerInterface and is used to send jiaoliao webhook
type JLWriter struct { type JLWriter struct {
AuthorName string `json:"authorname"` AuthorName string `json:"authorname"`
Title string `json:"title"` Title string `json:"title"`
WebhookURL string `json:"webhookurl"` WebhookURL string `json:"webhookurl"`
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

@ -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 // 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 // the rotate attribute also acts like fileLogWriter
type multiFileLogWriter struct { 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)
} }