1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-21 21:20:54 +00:00

Merge pull request #4179 from IamCathal/custom-log-formatter-secondpr

Custom log formatter secondpr
This commit is contained in:
Ming Deng 2020-08-22 21:36:50 +08:00 committed by GitHub
commit 09afe0ae8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 77 additions and 14 deletions

View File

@ -100,6 +100,10 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) {
return nil return nil
} }
func (c *aliLSWriter) Format(lm *logs.LogMsg) string {
return lm.Msg
}
// WriteMsg writes a message in connection. // WriteMsg writes a message in connection.
// If connection is down, try to re-connect. // If connection is down, try to re-connect.
func (c *aliLSWriter) WriteMsg(lm *logs.LogMsg) error { func (c *aliLSWriter) WriteMsg(lm *logs.LogMsg) error {

View File

@ -39,6 +39,10 @@ func NewConn() Logger {
return conn return conn
} }
func (c *connWriter) Format(lm *LogMsg) string {
return lm.Msg
}
// 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) error { func (c *connWriter) Init(jsonConfig string) error {
@ -62,7 +66,8 @@ func (c *connWriter) WriteMsg(lm *LogMsg) error {
defer c.innerWriter.Close() defer c.innerWriter.Close()
} }
_, err := c.lg.writeln(lm) msg := c.Format(lm)
_, err := c.lg.writeln(msg)
if err != nil { if err != nil {
return err return err
} }

View File

@ -52,6 +52,20 @@ type consoleWriter struct {
Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color 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. // NewConsole creates ConsoleWriter returning as LoggerInterface.
func NewConsole() Logger { func NewConsole() Logger {
cw := &consoleWriter{ cw := &consoleWriter{
@ -76,10 +90,12 @@ func (c *consoleWriter) WriteMsg(lm *LogMsg) error {
if lm.Level > c.Level { if lm.Level > c.Level {
return nil return nil
} }
// fmt.Printf("Formatted: %s\n\n", c.fmtter.Format(lm))
if c.Colorful { if c.Colorful {
lm.Msg = strings.Replace(lm.Msg, levelPrefix[lm.Level], colors[lm.Level](levelPrefix[lm.Level]), 1) 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 return nil
} }

View File

@ -35,6 +35,10 @@ type esLogger struct {
Level int `json:"level"` Level int `json:"level"`
} }
func (el *esLogger) Format(lm *logs.LogMsg) string {
return lm.Msg
}
// {"dsn":"http://localhost:9200/","level":1} // {"dsn":"http://localhost:9200/","level":1}
func (el *esLogger) Init(jsonconfig string) error { func (el *esLogger) Init(jsonconfig string) error {
err := json.Unmarshal([]byte(jsonconfig), el) err := json.Unmarshal([]byte(jsonconfig), el)
@ -67,7 +71,7 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
idx := LogDocument{ idx := LogDocument{
Timestamp: lm.When.Format(time.RFC3339), Timestamp: lm.When.Format(time.RFC3339),
Msg: lm.Msg, Msg: el.Format(lm),
} }
body, err := json.Marshal(idx) body, err := json.Marshal(idx)

View File

@ -89,6 +89,10 @@ func newFileWriter() Logger {
return w return w
} }
func (w *fileLogWriter) Format(lm *LogMsg) string {
return lm.Msg
}
// Init file logger with json config. // Init file logger with json config.
// jsonConfig like: // jsonConfig like:
// { // {
@ -149,7 +153,8 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error {
return nil return nil
} }
hd, d, h := formatTimeHeader(lm.When) hd, d, h := formatTimeHeader(lm.When)
lm.Msg = string(hd) + lm.Msg + "\n" msg := w.Format(lm)
msg = fmt.Sprintf("%s %s\n", string(hd), msg)
if w.Rotate { if w.Rotate {
w.RLock() w.RLock()
if w.needRotateHourly(len(lm.Msg), h) { if w.needRotateHourly(len(lm.Msg), h) {
@ -176,10 +181,10 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error {
} }
w.Lock() w.Lock()
_, err := w.fileWriter.Write([]byte(lm.Msg)) _, err := w.fileWriter.Write([]byte(msg))
if err == nil { if err == nil {
w.maxLinesCurLines++ w.maxLinesCurLines++
w.maxSizeCurSize += len(lm.Msg) w.maxSizeCurSize += len(msg)
} }
w.Unlock() w.Unlock()
return err return err

View File

@ -27,6 +27,10 @@ func (s *JLWriter) Init(jsonconfig string) error {
return json.Unmarshal([]byte(jsonconfig), s) return json.Unmarshal([]byte(jsonconfig), s)
} }
func (s *JLWriter) Format(lm *LogMsg) string {
return lm.Msg
}
// WriteMsg writes message in smtp writer. // WriteMsg writes message in smtp writer.
// Sends an email with subject and only this message. // Sends an email with subject and only this message.
func (s *JLWriter) WriteMsg(lm *LogMsg) error { func (s *JLWriter) WriteMsg(lm *LogMsg) error {
@ -34,8 +38,7 @@ 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"), lm.Msg) 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

@ -86,6 +86,7 @@ type newLoggerFunc func() Logger
type Logger interface { type Logger interface {
Init(config string) error Init(config string) error
WriteMsg(lm *LogMsg) error WriteMsg(lm *LogMsg) error
Format(lm *LogMsg) string
Destroy() Destroy()
Flush() Flush()
} }
@ -128,6 +129,8 @@ const defaultAsyncMsgLen = 1e3
type nameLogger struct { type nameLogger struct {
Logger Logger
// Formatter func(*LogMsg) string
LogFormatter
name string name string
} }
@ -139,6 +142,10 @@ type LogMsg struct {
LineNumber int LineNumber int
} }
type LogFormatter interface {
Format(lm *LogMsg) string
}
var logMsgPool *sync.Pool var logMsgPool *sync.Pool
// NewLogger returns a new BeeLogger. // NewLogger returns a new BeeLogger.
@ -179,6 +186,10 @@ func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger {
return bl return bl
} }
func Format(lm *LogMsg) string {
return lm.Msg
}
// SetLogger provides a given logger adapter into BeeLogger with config string. // SetLogger provides a given logger adapter into BeeLogger with config string.
// config must in in JSON format like {"interval":360}} // config must in in JSON format like {"interval":360}}
func (bl *BeeLogger) setLogger(adapterName string, configs ...string) error { 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) { func (bl *BeeLogger) writeToLoggers(lm *LogMsg) {
for _, l := range bl.outputs { for _, l := range bl.outputs {
// fmt.Println("Formatted: ", l.Format(lm))
err := l.WriteMsg(lm) err := l.WriteMsg(lm)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err) fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err)

View File

@ -30,10 +30,10 @@ func newLogWriter(wr io.Writer) *logWriter {
return &logWriter{writer: wr} return &logWriter{writer: wr}
} }
func (lg *logWriter) writeln(lm *LogMsg) (int, error) { func (lg *logWriter) writeln(msg string) (int, error) {
lg.Lock() lg.Lock()
h, _, _ := formatTimeHeader(lm.When) msg += "\n"
n, err := lg.writer.Write(append(append(h, lm.Msg...), '\n')) n, err := lg.writer.Write([]byte(msg))
lg.Unlock() lg.Unlock()
return n, err return n, err
} }

View File

@ -78,6 +78,10 @@ func (f *multiFileLogWriter) Init(config string) error {
return nil return nil
} }
func (f *multiFileLogWriter) Format(lm *LogMsg) string {
return lm.Msg
}
func (f *multiFileLogWriter) Destroy() { func (f *multiFileLogWriter) Destroy() {
for i := 0; i < len(f.writers); i++ { for i := 0; i < len(f.writers); i++ {
if f.writers[i] != nil { if f.writers[i] != nil {

View File

@ -18,6 +18,10 @@ func newSLACKWriter() Logger {
return &SLACKWriter{Level: LevelTrace} return &SLACKWriter{Level: LevelTrace}
} }
func (s *SLACKWriter) Format(lm *LogMsg) string {
return lm.Msg
}
// Init SLACKWriter with json config string // Init SLACKWriter with json config string
func (s *SLACKWriter) Init(jsonconfig string) error { func (s *SLACKWriter) Init(jsonconfig string) error {
return json.Unmarshal([]byte(jsonconfig), s) return json.Unmarshal([]byte(jsonconfig), s)
@ -29,8 +33,8 @@ func (s *SLACKWriter) WriteMsg(lm *LogMsg) error {
if lm.Level > s.Level { if lm.Level > s.Level {
return nil return nil
} }
msg := s.Format(lm)
text := fmt.Sprintf("{\"text\": \"%s %s\"}", lm.When.Format("2006-01-02 15:04:05"), lm.Msg) text := fmt.Sprintf("{\"text\": \"%s %s\"}", lm.When.Format("2006-01-02 15:04:05"), msg)
form := url.Values{} form := url.Values{}
form.Add("payload", text) form.Add("payload", text)

View File

@ -114,6 +114,10 @@ func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd
return client.Quit() return client.Quit()
} }
func (s *SMTPWriter) Format(lm *LogMsg) string {
return lm.Msg
}
// WriteMsg writes message in smtp writer. // WriteMsg writes message in smtp writer.
// Sends an email with subject and only this message. // Sends an email with subject and only this message.
func (s *SMTPWriter) WriteMsg(lm *LogMsg) error { func (s *SMTPWriter) WriteMsg(lm *LogMsg) error {
@ -126,11 +130,13 @@ func (s *SMTPWriter) WriteMsg(lm *LogMsg) error {
// Set up authentication information. // Set up authentication information.
auth := s.getSMTPAuth(hp[0]) auth := s.getSMTPAuth(hp[0])
msg := s.Format(lm)
// Connect to the server, authenticate, set the sender and recipient, // Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step. // and send the email all in one step.
contentType := "Content-Type: text/plain" + "; charset=UTF-8" contentType := "Content-Type: text/plain" + "; charset=UTF-8"
mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress + mailmsg := []byte("To: " + strings.Join(s.RecipientAddresses, ";") + "\r\nFrom: " + s.FromAddress + "<" + s.FromAddress +
">\r\nSubject: " + s.Subject + "\r\n" + contentType + "\r\n\r\n" + fmt.Sprintf(".%s", lm.When.Format("2006-01-02 15:04:05")) + lm.Msg) ">\r\nSubject: " + s.Subject + "\r\n" + contentType + "\r\n\r\n" + fmt.Sprintf(".%s", lm.When.Format("2006-01-02 15:04:05")) + msg)
return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg) return s.sendMail(s.Host, auth, s.FromAddress, s.RecipientAddresses, mailmsg)
} }