123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package logs
- import (
- "encoding/json"
- "os"
- "runtime"
- "time"
- )
- 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;37"),
- newBrush("1;36"),
- newBrush("1;35"),
- newBrush("1;31"),
- newBrush("1;33"),
- newBrush("1;32"),
- newBrush("1;34"),
- newBrush("1;34"),
- }
- type consoleWriter struct {
- lg *logWriter
- Level int `json:"level"`
- Colorful bool `json:"color"`
- }
- func NewConsole() Logger {
- cw := &consoleWriter{
- lg: newLogWriter(os.Stdout),
- Level: LevelDebug,
- Colorful: runtime.GOOS != "windows",
- }
- return cw
- }
- func (c *consoleWriter) Init(jsonConfig string) error {
- if len(jsonConfig) == 0 {
- return nil
- }
- err := json.Unmarshal([]byte(jsonConfig), c)
- if runtime.GOOS == "windows" {
- c.Colorful = false
- }
- return err
- }
- func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
- if level > c.Level {
- return nil
- }
- if c.Colorful {
- msg = colors[level](msg)
- }
- c.lg.println(when, msg)
- return nil
- }
- func (c *consoleWriter) Destroy() {
- }
- func (c *consoleWriter) Flush() {
- }
- func init() {
- Register(AdapterConsole, NewConsole)
- }
|