mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 21:30:54 +00:00
Merge pull request #4179 from IamCathal/custom-log-formatter-secondpr
Custom log formatter secondpr
This commit is contained in:
commit
09afe0ae8e
@ -100,6 +100,10 @@ func (c *aliLSWriter) Init(jsonConfig string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *aliLSWriter) Format(lm *logs.LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// WriteMsg writes a message in connection.
|
||||
// If connection is down, try to re-connect.
|
||||
func (c *aliLSWriter) WriteMsg(lm *logs.LogMsg) error {
|
||||
|
@ -39,6 +39,10 @@ func NewConn() Logger {
|
||||
return conn
|
||||
}
|
||||
|
||||
func (c *connWriter) Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// Init initializes a connection writer with json config.
|
||||
// json config only needs they "level" key
|
||||
func (c *connWriter) Init(jsonConfig string) error {
|
||||
@ -62,7 +66,8 @@ func (c *connWriter) WriteMsg(lm *LogMsg) error {
|
||||
defer c.innerWriter.Close()
|
||||
}
|
||||
|
||||
_, err := c.lg.writeln(lm)
|
||||
msg := c.Format(lm)
|
||||
_, err := c.lg.writeln(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -52,6 +52,20 @@ type consoleWriter struct {
|
||||
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.
|
||||
func NewConsole() Logger {
|
||||
cw := &consoleWriter{
|
||||
@ -76,10 +90,12 @@ func (c *consoleWriter) WriteMsg(lm *LogMsg) error {
|
||||
if lm.Level > c.Level {
|
||||
return nil
|
||||
}
|
||||
// fmt.Printf("Formatted: %s\n\n", c.fmtter.Format(lm))
|
||||
if c.Colorful {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -35,6 +35,10 @@ type esLogger struct {
|
||||
Level int `json:"level"`
|
||||
}
|
||||
|
||||
func (el *esLogger) Format(lm *logs.LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// {"dsn":"http://localhost:9200/","level":1}
|
||||
func (el *esLogger) Init(jsonconfig string) error {
|
||||
err := json.Unmarshal([]byte(jsonconfig), el)
|
||||
@ -67,7 +71,7 @@ func (el *esLogger) WriteMsg(lm *logs.LogMsg) error {
|
||||
|
||||
idx := LogDocument{
|
||||
Timestamp: lm.When.Format(time.RFC3339),
|
||||
Msg: lm.Msg,
|
||||
Msg: el.Format(lm),
|
||||
}
|
||||
|
||||
body, err := json.Marshal(idx)
|
||||
|
@ -89,6 +89,10 @@ func newFileWriter() Logger {
|
||||
return w
|
||||
}
|
||||
|
||||
func (w *fileLogWriter) Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// Init file logger with json config.
|
||||
// jsonConfig like:
|
||||
// {
|
||||
@ -149,7 +153,8 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error {
|
||||
return nil
|
||||
}
|
||||
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 {
|
||||
w.RLock()
|
||||
if w.needRotateHourly(len(lm.Msg), h) {
|
||||
@ -176,10 +181,10 @@ func (w *fileLogWriter) WriteMsg(lm *LogMsg) error {
|
||||
}
|
||||
|
||||
w.Lock()
|
||||
_, err := w.fileWriter.Write([]byte(lm.Msg))
|
||||
_, err := w.fileWriter.Write([]byte(msg))
|
||||
if err == nil {
|
||||
w.maxLinesCurLines++
|
||||
w.maxSizeCurSize += len(lm.Msg)
|
||||
w.maxSizeCurSize += len(msg)
|
||||
}
|
||||
w.Unlock()
|
||||
return err
|
||||
|
@ -27,6 +27,10 @@ func (s *JLWriter) Init(jsonconfig string) error {
|
||||
return json.Unmarshal([]byte(jsonconfig), s)
|
||||
}
|
||||
|
||||
func (s *JLWriter) Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// WriteMsg writes message in smtp writer.
|
||||
// Sends an email with subject and only this message.
|
||||
func (s *JLWriter) WriteMsg(lm *LogMsg) error {
|
||||
@ -34,8 +38,7 @@ func (s *JLWriter) WriteMsg(lm *LogMsg) error {
|
||||
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.Add("authorName", s.AuthorName)
|
||||
form.Add("title", s.Title)
|
||||
|
@ -86,6 +86,7 @@ type newLoggerFunc func() Logger
|
||||
type Logger interface {
|
||||
Init(config string) error
|
||||
WriteMsg(lm *LogMsg) error
|
||||
Format(lm *LogMsg) string
|
||||
Destroy()
|
||||
Flush()
|
||||
}
|
||||
@ -128,6 +129,8 @@ const defaultAsyncMsgLen = 1e3
|
||||
|
||||
type nameLogger struct {
|
||||
Logger
|
||||
// Formatter func(*LogMsg) string
|
||||
LogFormatter
|
||||
name string
|
||||
}
|
||||
|
||||
@ -139,6 +142,10 @@ type LogMsg struct {
|
||||
LineNumber int
|
||||
}
|
||||
|
||||
type LogFormatter interface {
|
||||
Format(lm *LogMsg) string
|
||||
}
|
||||
|
||||
var logMsgPool *sync.Pool
|
||||
|
||||
// NewLogger returns a new BeeLogger.
|
||||
@ -179,6 +186,10 @@ func (bl *BeeLogger) Async(msgLen ...int64) *BeeLogger {
|
||||
return bl
|
||||
}
|
||||
|
||||
func Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// SetLogger provides a given logger adapter into BeeLogger with config string.
|
||||
// config must in in JSON format like {"interval":360}}
|
||||
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) {
|
||||
for _, l := range bl.outputs {
|
||||
// fmt.Println("Formatted: ", l.Format(lm))
|
||||
err := l.WriteMsg(lm)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err)
|
||||
|
@ -30,10 +30,10 @@ func newLogWriter(wr io.Writer) *logWriter {
|
||||
return &logWriter{writer: wr}
|
||||
}
|
||||
|
||||
func (lg *logWriter) writeln(lm *LogMsg) (int, error) {
|
||||
func (lg *logWriter) writeln(msg string) (int, error) {
|
||||
lg.Lock()
|
||||
h, _, _ := formatTimeHeader(lm.When)
|
||||
n, err := lg.writer.Write(append(append(h, lm.Msg...), '\n'))
|
||||
msg += "\n"
|
||||
n, err := lg.writer.Write([]byte(msg))
|
||||
lg.Unlock()
|
||||
return n, err
|
||||
}
|
||||
|
@ -78,6 +78,10 @@ func (f *multiFileLogWriter) Init(config string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *multiFileLogWriter) Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
func (f *multiFileLogWriter) Destroy() {
|
||||
for i := 0; i < len(f.writers); i++ {
|
||||
if f.writers[i] != nil {
|
||||
|
@ -18,6 +18,10 @@ func newSLACKWriter() Logger {
|
||||
return &SLACKWriter{Level: LevelTrace}
|
||||
}
|
||||
|
||||
func (s *SLACKWriter) Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// Init SLACKWriter with json config string
|
||||
func (s *SLACKWriter) Init(jsonconfig string) error {
|
||||
return json.Unmarshal([]byte(jsonconfig), s)
|
||||
@ -29,8 +33,8 @@ func (s *SLACKWriter) WriteMsg(lm *LogMsg) error {
|
||||
if lm.Level > s.Level {
|
||||
return nil
|
||||
}
|
||||
|
||||
text := fmt.Sprintf("{\"text\": \"%s %s\"}", lm.When.Format("2006-01-02 15:04:05"), lm.Msg)
|
||||
msg := s.Format(lm)
|
||||
text := fmt.Sprintf("{\"text\": \"%s %s\"}", lm.When.Format("2006-01-02 15:04:05"), msg)
|
||||
|
||||
form := url.Values{}
|
||||
form.Add("payload", text)
|
||||
|
@ -114,6 +114,10 @@ func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd
|
||||
return client.Quit()
|
||||
}
|
||||
|
||||
func (s *SMTPWriter) Format(lm *LogMsg) string {
|
||||
return lm.Msg
|
||||
}
|
||||
|
||||
// WriteMsg writes message in smtp writer.
|
||||
// Sends an email with subject and only this message.
|
||||
func (s *SMTPWriter) WriteMsg(lm *LogMsg) error {
|
||||
@ -126,11 +130,13 @@ func (s *SMTPWriter) WriteMsg(lm *LogMsg) error {
|
||||
// Set up authentication information.
|
||||
auth := s.getSMTPAuth(hp[0])
|
||||
|
||||
msg := s.Format(lm)
|
||||
|
||||
// Connect to the server, authenticate, set the sender and recipient,
|
||||
// and send the email all in one step.
|
||||
contentType := "Content-Type: text/plain" + "; charset=UTF-8"
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user