1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 07:20: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"
"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)
}

View File

@ -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

View File

@ -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 {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

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
// 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)

View File

@ -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)
}