mirror of
https://github.com/astaxie/beego.git
synced 2024-11-21 22:50:56 +00:00
LogFormatter Implementation
This commit is contained in:
parent
9003ca3eef
commit
6bdedff457
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -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:
|
||||||
// {
|
// {
|
||||||
|
@ -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 {
|
||||||
|
@ -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)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -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)
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user