1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 19:10:50 +00:00
Beego/logs/console.go

55 lines
986 B
Go
Raw Normal View History

2013-08-27 15:48:58 +00:00
package logs
import (
"encoding/json"
"log"
"os"
)
2013-12-30 15:32:57 +00:00
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
2013-08-27 15:48:58 +00:00
type ConsoleWriter struct {
lg *log.Logger
Level int `json:"level"`
2013-08-27 15:48:58 +00:00
}
2013-12-30 15:32:57 +00:00
// create ConsoleWriter returning as LoggerInterface.
2013-08-27 15:48:58 +00:00
func NewConsole() LoggerInterface {
cw := new(ConsoleWriter)
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime)
cw.Level = LevelTrace
2013-08-27 15:48:58 +00:00
return cw
}
2013-12-30 15:32:57 +00:00
// init console logger.
// jsonconfig like '{"level":LevelTrace}'.
2013-08-27 15:48:58 +00:00
func (c *ConsoleWriter) Init(jsonconfig string) error {
err := json.Unmarshal([]byte(jsonconfig), c)
2013-08-27 15:48:58 +00:00
if err != nil {
return err
}
return nil
}
2013-12-30 15:32:57 +00:00
// write message in console.
2013-08-27 15:48:58 +00:00
func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
if level < c.Level {
2013-08-27 15:48:58 +00:00
return nil
}
c.lg.Println(msg)
return nil
}
2013-12-30 15:32:57 +00:00
// implementing method. empty.
2013-08-27 15:48:58 +00:00
func (c *ConsoleWriter) Destroy() {
}
2013-12-30 15:32:57 +00:00
// implementing method. empty.
2013-11-27 09:50:10 +00:00
func (c *ConsoleWriter) Flush() {
}
2013-08-27 15:48:58 +00:00
func init() {
Register("console", NewConsole)
}