2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @authors astaxie
|
2013-08-27 15:48:58 +00:00
|
|
|
package logs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-01-28 03:26:43 +00:00
|
|
|
"runtime"
|
2013-08-27 15:48:58 +00:00
|
|
|
)
|
|
|
|
|
2014-01-21 09:57:37 +00:00
|
|
|
type Brush func(string) string
|
|
|
|
|
|
|
|
func NewBrush(color string) Brush {
|
|
|
|
pre := "\033["
|
|
|
|
reset := "\033[0m"
|
|
|
|
return func(text string) string {
|
|
|
|
return pre + color + "m" + text + reset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var colors = []Brush{
|
2014-07-11 09:15:34 +00:00
|
|
|
NewBrush("1;37"), // Emergency white
|
|
|
|
NewBrush("1;36"), // Alert cyan
|
|
|
|
NewBrush("1;35"), // Critical magenta
|
2014-01-21 09:57:37 +00:00
|
|
|
NewBrush("1;31"), // Error red
|
2014-07-11 09:15:34 +00:00
|
|
|
NewBrush("1;33"), // Warning yellow
|
|
|
|
NewBrush("1;32"), // Notice green
|
|
|
|
NewBrush("1;34"), // Informational green
|
|
|
|
NewBrush("1;30"), // Debug black
|
2014-01-21 09:57:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
2013-12-19 10:15:00 +00:00
|
|
|
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)
|
2014-07-11 09:15:34 +00:00
|
|
|
cw.Level = LevelDebug
|
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 {
|
2014-04-03 23:33:13 +00:00
|
|
|
if len(jsonconfig) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2013-12-19 10:15:00 +00:00
|
|
|
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 {
|
2014-07-11 09:15:34 +00:00
|
|
|
if level > c.Level {
|
2013-08-27 15:48:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-01-28 03:26:43 +00:00
|
|
|
if goos := runtime.GOOS; goos == "windows" {
|
|
|
|
c.lg.Println(msg)
|
|
|
|
} else {
|
|
|
|
c.lg.Println(colors[level](msg))
|
|
|
|
}
|
2013-08-27 15:48:58 +00:00
|
|
|
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)
|
|
|
|
}
|