12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package log
- import (
- "encoding/json"
- "log"
- "os"
- "runtime"
- )
- 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{
- NewBrush("1;36"),
- NewBrush("1;34"),
- NewBrush("1;32"),
- NewBrush("1;33"),
- NewBrush("1;31"),
- NewBrush("1;35"),
- NewBrush("1;31"),
- }
- type ConsoleWriter struct {
- lg *log.Logger
- Level int `json:"level"`
- }
- func NewConsole() LoggerInterface {
- return &ConsoleWriter{
- lg: log.New(os.Stdout, "", log.Ldate|log.Ltime),
- Level: TRACE,
- }
- }
- func (cw *ConsoleWriter) Init(config string) error {
- return json.Unmarshal([]byte(config), cw)
- }
- func (cw *ConsoleWriter) WriteMsg(msg string, skip, level int) error {
- if cw.Level > level {
- return nil
- }
- if runtime.GOOS == "windows" {
- cw.lg.Println(msg)
- } else {
- cw.lg.Println(colors[level](msg))
- }
- return nil
- }
- func (_ *ConsoleWriter) Flush() {
- }
- func (_ *ConsoleWriter) Destroy() {
- }
- func init() {
- Register("console", NewConsole)
- }
|