mirror of
https://github.com/astaxie/beego.git
synced 2024-12-23 15:20:50 +00:00
55 lines
986 B
Go
55 lines
986 B
Go
package logs
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
|
|
type ConsoleWriter struct {
|
|
lg *log.Logger
|
|
Level int `json:"level"`
|
|
}
|
|
|
|
// create ConsoleWriter returning as LoggerInterface.
|
|
func NewConsole() LoggerInterface {
|
|
cw := new(ConsoleWriter)
|
|
cw.lg = log.New(os.Stdout, "", log.Ldate|log.Ltime)
|
|
cw.Level = LevelTrace
|
|
return cw
|
|
}
|
|
|
|
// init console logger.
|
|
// jsonconfig like '{"level":LevelTrace}'.
|
|
func (c *ConsoleWriter) Init(jsonconfig string) error {
|
|
err := json.Unmarshal([]byte(jsonconfig), c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// write message in console.
|
|
func (c *ConsoleWriter) WriteMsg(msg string, level int) error {
|
|
if level < c.Level {
|
|
return nil
|
|
}
|
|
c.lg.Println(msg)
|
|
return nil
|
|
}
|
|
|
|
// implementing method. empty.
|
|
func (c *ConsoleWriter) Destroy() {
|
|
|
|
}
|
|
|
|
// implementing method. empty.
|
|
func (c *ConsoleWriter) Flush() {
|
|
|
|
}
|
|
|
|
func init() {
|
|
Register("console", NewConsole)
|
|
}
|