119 lines
2.5 KiB
Go
119 lines
2.5 KiB
Go
|
package log
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
FG_BLACK int = 30
|
||
|
FG_RED = 31
|
||
|
FG_GREEN = 32
|
||
|
FG_YELLOW = 33
|
||
|
FG_BLUE = 34
|
||
|
FG_PURPLE = 35
|
||
|
FG_CYAN = 36
|
||
|
FG_GREY = 37
|
||
|
)
|
||
|
|
||
|
type Console struct {
|
||
|
Level int
|
||
|
EnableColor int
|
||
|
prefix string
|
||
|
}
|
||
|
|
||
|
func (log *Console) SetLevel(lv int) {
|
||
|
log.Level = lv
|
||
|
}
|
||
|
|
||
|
func (log *Console) Prefix(s string) {
|
||
|
log.prefix = s
|
||
|
}
|
||
|
|
||
|
func (log *Console) Print(i ...interface{}) {
|
||
|
log.write(TraceLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Printf(format string, args ...interface{}) {
|
||
|
log.write(TraceLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Debug(i ...interface{}) {
|
||
|
log.write(DebugLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Debugf(format string, args ...interface{}) {
|
||
|
log.write(DebugLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Info(i ...interface{}) {
|
||
|
log.write(InfoLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Infof(format string, args ...interface{}) {
|
||
|
log.write(InfoLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Warn(i ...interface{}) {
|
||
|
log.write(WarnLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Warnf(format string, args ...interface{}) {
|
||
|
log.write(WarnLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Error(i ...interface{}) {
|
||
|
log.write(ErrorLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Errorf(format string, args ...interface{}) {
|
||
|
log.write(ErrorLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Fatal(i ...interface{}) {
|
||
|
log.write(FatalLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Fatalf(format string, args ...interface{}) {
|
||
|
log.write(FatalLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Panic(i ...interface{}) {
|
||
|
log.write(PanicLevel, fmt.Sprint(i...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) Panicf(format string, args ...interface{}) {
|
||
|
log.write(PanicLevel, fmt.Sprintf(format, args...))
|
||
|
}
|
||
|
|
||
|
func (log *Console) write(level int, s string) {
|
||
|
if log.Level > level {
|
||
|
return
|
||
|
}
|
||
|
lvColor := map[int]int{
|
||
|
TraceLevel: FG_GREY,
|
||
|
DebugLevel: FG_BLUE,
|
||
|
InfoLevel: FG_GREEN,
|
||
|
WarnLevel: FG_PURPLE,
|
||
|
ErrorLevel: FG_RED,
|
||
|
FatalLevel: FG_RED,
|
||
|
PanicLevel: FG_RED,
|
||
|
}
|
||
|
var ls string
|
||
|
if log.EnableColor > 0 {
|
||
|
ls = fmt.Sprintf("\033[0m\033[%dm[%s]\033[0m", lvColor[level], getLevelText(level))
|
||
|
} else {
|
||
|
ls = getLevelText(level)
|
||
|
}
|
||
|
if log.prefix != "" {
|
||
|
ls += " [" + log.prefix + "]"
|
||
|
}
|
||
|
fmt.Println(time.Now().Format("2006-01-02 15:04:05") + " " + ls + " " + s)
|
||
|
}
|
||
|
|
||
|
func NewConsoleLogger() *Console {
|
||
|
return &Console{
|
||
|
EnableColor: 1,
|
||
|
}
|
||
|
}
|