2023-01-23 11:35:44 +08:00
|
|
|
package gologger
|
|
|
|
|
2023-01-23 12:08:55 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2023-01-23 11:35:44 +08:00
|
|
|
|
|
|
|
const (
|
2023-11-28 18:29:30 +08:00
|
|
|
ErrorTemplate = "\033[1;31m%s\033[0m\n"
|
|
|
|
WarnTemplate = "\033[1;33m%s\033[0m\n"
|
|
|
|
InfoTemplate = "\033[1;32m%s\033[0m\n"
|
|
|
|
DebugTemplate = "\033[1;34m%s\033[0m\n"
|
|
|
|
TraceTemplate = "\033[1;35m%s\033[0m\n"
|
2023-01-23 11:35:44 +08:00
|
|
|
)
|
|
|
|
|
2023-11-28 18:29:30 +08:00
|
|
|
type ConsoleAppender struct {
|
|
|
|
}
|
2023-01-23 11:35:44 +08:00
|
|
|
|
|
|
|
func (c *ConsoleAppender) GetName() string {
|
|
|
|
return "console"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConsoleAppender) Append(logEvent LogEvent) {
|
|
|
|
data := []interface{}{
|
|
|
|
logEvent.Ts.Format("2006-01-02 15:04:05"),
|
2023-01-23 12:08:55 +08:00
|
|
|
" ",
|
2023-01-23 11:35:44 +08:00
|
|
|
}
|
2023-01-23 12:08:55 +08:00
|
|
|
dmsg := fmt.Sprint(logEvent.Data)
|
|
|
|
dmsg = strings.TrimLeft(dmsg, "[")
|
|
|
|
dmsg = strings.TrimRight(dmsg, "]")
|
2023-11-28 18:29:30 +08:00
|
|
|
data = append(data, "["+logEvent.Category+"] ")
|
|
|
|
|
2023-01-23 12:08:55 +08:00
|
|
|
data = append(data, dmsg)
|
2023-01-23 11:35:44 +08:00
|
|
|
logMsg := fmt.Sprint(data...)
|
|
|
|
switch logEvent.Level {
|
|
|
|
case Error:
|
|
|
|
fmt.Printf(ErrorTemplate, logMsg)
|
|
|
|
case Warn:
|
|
|
|
fmt.Printf(WarnTemplate, logMsg)
|
|
|
|
case Info:
|
|
|
|
fmt.Printf(InfoTemplate, logMsg)
|
|
|
|
case Debug:
|
|
|
|
fmt.Printf(DebugTemplate, logMsg)
|
|
|
|
case Trace:
|
|
|
|
fmt.Printf(TraceTemplate, logMsg)
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 18:29:30 +08:00
|
|
|
func makeConsoleAppender(appenderConfig LogAppenderConfig) LoggerAppender {
|
|
|
|
var appender LoggerAppender = &ConsoleAppender{}
|
|
|
|
return appender
|
|
|
|
}
|
|
|
|
func init() {
|
|
|
|
RegistAppender("console", makeConsoleAppender)
|
|
|
|
}
|