|
|
@ -14,13 +14,27 @@ import (
|
|
|
|
"github.com/subchen/gstack/gls"
|
|
|
|
"github.com/subchen/gstack/gls"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Log Level
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
|
DEBUG = iota
|
|
|
|
// log level
|
|
|
|
INFO
|
|
|
|
L_DEBUG = iota
|
|
|
|
WARN
|
|
|
|
L_INFO
|
|
|
|
ERROR
|
|
|
|
L_WARN
|
|
|
|
FATAL
|
|
|
|
L_ERROR
|
|
|
|
|
|
|
|
L_FATAL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Bits or'ed together to control what's printed.
|
|
|
|
|
|
|
|
F_TIME = 1 << iota
|
|
|
|
|
|
|
|
F_LONG_FILE
|
|
|
|
|
|
|
|
F_SHORT_FILE
|
|
|
|
|
|
|
|
F_PID
|
|
|
|
|
|
|
|
F_GID
|
|
|
|
|
|
|
|
F_COLOR
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// default flags
|
|
|
|
|
|
|
|
DEFAULT_FLAGS = F_TIME | F_SHORT_FILE | F_PID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// default time format
|
|
|
|
|
|
|
|
DEFAULT_TIME_FORMAT = "2006-01-02 15:04:05,000"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
@ -38,32 +52,34 @@ var (
|
|
|
|
"\033[31mERROR\033[0m",
|
|
|
|
"\033[31mERROR\033[0m",
|
|
|
|
"\033[35mFATAL\033[0m",
|
|
|
|
"\033[35mFATAL\033[0m",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buffer = sync.Pool{
|
|
|
|
|
|
|
|
New: func() interface{} {
|
|
|
|
|
|
|
|
return new(bytes.Buffer)
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func New(out io.Writer) *Logger {
|
|
|
|
func New(out io.Writer) *Logger {
|
|
|
|
return &Logger{
|
|
|
|
return &Logger{
|
|
|
|
out: out,
|
|
|
|
writer: out,
|
|
|
|
level: INFO,
|
|
|
|
level: L_INFO,
|
|
|
|
pid: os.Getpid(),
|
|
|
|
pid: os.Getpid(),
|
|
|
|
name: "",
|
|
|
|
name: "",
|
|
|
|
timeLayout: "2006-01-02 15:04:05.000",
|
|
|
|
timeFormat: DEFAULT_TIME_FORMAT,
|
|
|
|
goroutineId: false,
|
|
|
|
flags: DEFAULT_FLAGS,
|
|
|
|
longFileFormat: false,
|
|
|
|
|
|
|
|
colorizedLevel: false,
|
|
|
|
|
|
|
|
callerSkip: 2,
|
|
|
|
callerSkip: 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
type Logger struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
m sync.Mutex
|
|
|
|
out io.Writer
|
|
|
|
writer io.Writer
|
|
|
|
level int
|
|
|
|
level int
|
|
|
|
pid int
|
|
|
|
pid int
|
|
|
|
name string
|
|
|
|
name string
|
|
|
|
timeLayout string
|
|
|
|
timeFormat string
|
|
|
|
goroutineId bool
|
|
|
|
flags int
|
|
|
|
longFileFormat bool
|
|
|
|
|
|
|
|
colorizedLevel bool
|
|
|
|
|
|
|
|
callerSkip int
|
|
|
|
callerSkip int
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -72,130 +88,205 @@ func (l *Logger) GetLevel() int {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetLevel(level int) *Logger {
|
|
|
|
func (l *Logger) SetLevel(level int) *Logger {
|
|
|
|
|
|
|
|
if level < L_DEBUG || level > L_FATAL {
|
|
|
|
|
|
|
|
panic("unknown log level")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
l.m.Lock()
|
|
|
|
l.level = level
|
|
|
|
l.level = level
|
|
|
|
|
|
|
|
l.m.Unlock()
|
|
|
|
return l
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetName(name string) *Logger {
|
|
|
|
func (l *Logger) GetLevelName() string {
|
|
|
|
l.name = name
|
|
|
|
return levelStr[l.level]
|
|
|
|
return l
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetTimeLayout(layout string) *Logger {
|
|
|
|
func (l *Logger) SetLevelName(level string) {
|
|
|
|
l.timeLayout = layout
|
|
|
|
level = strings.ToUpper(level)
|
|
|
|
return l
|
|
|
|
for i, v := range levelStr {
|
|
|
|
|
|
|
|
if v == level {
|
|
|
|
|
|
|
|
l.SetLevel(i)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
panic("unknown log level: " + level)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) GetFlags() int {
|
|
|
|
|
|
|
|
return l.flags
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) EnableGoroutineId(enable bool) *Logger {
|
|
|
|
func (l *Logger) SetFlags(flags int) *Logger {
|
|
|
|
l.goroutineId = enable
|
|
|
|
l.m.Lock()
|
|
|
|
|
|
|
|
l.flags = flags
|
|
|
|
|
|
|
|
l.m.Unlock()
|
|
|
|
return l
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) EnableLongFileFormat(enable bool) *Logger {
|
|
|
|
func (l *Logger) GetAppName() string {
|
|
|
|
l.longFileFormat = enable
|
|
|
|
return l.name
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetAppName(name string) *Logger {
|
|
|
|
|
|
|
|
l.m.Lock()
|
|
|
|
|
|
|
|
l.name = name
|
|
|
|
|
|
|
|
l.m.Unlock()
|
|
|
|
return l
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) EnableColorizedLevel(enable bool) *Logger {
|
|
|
|
func (l *Logger) GetTimeFormat() string {
|
|
|
|
l.colorizedLevel = enable
|
|
|
|
return l.timeFormat
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetTimeFormat(format string) *Logger {
|
|
|
|
|
|
|
|
l.m.Lock()
|
|
|
|
|
|
|
|
l.timeFormat = format
|
|
|
|
|
|
|
|
l.m.Unlock()
|
|
|
|
return l
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SkipCaller(skip int) *Logger {
|
|
|
|
func (l *Logger) SkipCaller(skip int) *Logger {
|
|
|
|
|
|
|
|
l.m.Lock()
|
|
|
|
|
|
|
|
defer l.m.Unlock()
|
|
|
|
l.callerSkip = skip
|
|
|
|
l.callerSkip = skip
|
|
|
|
return l
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetWriter(w io.Writer) *Logger {
|
|
|
|
func (l *Logger) SetWriter(w io.Writer) *Logger {
|
|
|
|
l.out = w
|
|
|
|
l.m.Lock()
|
|
|
|
|
|
|
|
defer l.m.Unlock()
|
|
|
|
|
|
|
|
l.writer = w
|
|
|
|
return l
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) IsDebugEnabled() bool {
|
|
|
|
func (l *Logger) IsDebugEnabled() bool {
|
|
|
|
return l.level <= DEBUG
|
|
|
|
return l.level <= L_DEBUG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) IsInfoEnabled() bool {
|
|
|
|
func (l *Logger) IsInfoEnabled() bool {
|
|
|
|
return l.level <= INFO
|
|
|
|
return l.level <= L_INFO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) IsWarnEnabled() bool {
|
|
|
|
func (l *Logger) IsWarnEnabled() bool {
|
|
|
|
return l.level <= WARN
|
|
|
|
return l.level <= L_WARN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) IsErrorEnabled() bool {
|
|
|
|
func (l *Logger) IsErrorEnabled() bool {
|
|
|
|
return l.level <= ERROR
|
|
|
|
return l.level <= L_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Debug(msg string, args ...interface{}) {
|
|
|
|
func (l *Logger) Debug(obj ...interface{}) {
|
|
|
|
if l.level <= DEBUG {
|
|
|
|
if l.level <= L_DEBUG {
|
|
|
|
l.log(DEBUG, msg, args...)
|
|
|
|
l.log(L_DEBUG, fmt.Sprint(obj...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Info(msg string, args ...interface{}) {
|
|
|
|
func (l *Logger) Info(obj ...interface{}) {
|
|
|
|
if l.level <= INFO {
|
|
|
|
if l.level <= L_INFO {
|
|
|
|
l.log(INFO, msg, args...)
|
|
|
|
l.log(L_INFO, fmt.Sprint(obj...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Warn(msg string, args ...interface{}) {
|
|
|
|
func (l *Logger) Warn(obj ...interface{}) {
|
|
|
|
if l.level <= WARN {
|
|
|
|
if l.level <= L_WARN {
|
|
|
|
l.log(WARN, msg, args...)
|
|
|
|
l.log(L_WARN, fmt.Sprint(obj...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Error(msg string, args ...interface{}) {
|
|
|
|
func (l *Logger) Error(obj ...interface{}) {
|
|
|
|
if l.level <= ERROR {
|
|
|
|
if l.level <= L_ERROR {
|
|
|
|
l.log(ERROR, msg, args...)
|
|
|
|
l.log(L_ERROR, fmt.Sprint(obj...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Fatal(msg string, args ...interface{}) {
|
|
|
|
func (l *Logger) Fatal(obj ...interface{}) {
|
|
|
|
l.log(FATAL, msg, args...)
|
|
|
|
if l.level <= L_FATAL {
|
|
|
|
|
|
|
|
l.log(L_FATAL, fmt.Sprint(obj...))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) log(level int, msg string, args ...interface{}) {
|
|
|
|
func (l *Logger) Debugf(msg string, args ...interface{}) {
|
|
|
|
_, file, line, ok := runtime.Caller(l.callerSkip)
|
|
|
|
if l.level <= L_DEBUG {
|
|
|
|
if !ok {
|
|
|
|
l.log(L_DEBUG, fmt.Sprintf(msg, args...))
|
|
|
|
file = "???"
|
|
|
|
}
|
|
|
|
line = 0
|
|
|
|
}
|
|
|
|
} else if !l.longFileFormat {
|
|
|
|
|
|
|
|
if index := strings.LastIndex(file, "/"); index >= 0 {
|
|
|
|
func (l *Logger) Infof(msg string, args ...interface{}) {
|
|
|
|
file = file[index+1:]
|
|
|
|
if l.level <= L_INFO {
|
|
|
|
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
|
|
|
|
l.log(L_INFO, fmt.Sprintf(msg, args...))
|
|
|
|
file = file[index+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Warnf(msg string, args ...interface{}) {
|
|
|
|
|
|
|
|
if l.level <= L_WARN {
|
|
|
|
|
|
|
|
l.log(L_WARN, fmt.Sprintf(msg, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Errorf(msg string, args ...interface{}) {
|
|
|
|
|
|
|
|
if l.level <= L_ERROR {
|
|
|
|
|
|
|
|
l.log(L_ERROR, fmt.Sprintf(msg, args...))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Fatalf(msg string, args ...interface{}) {
|
|
|
|
|
|
|
|
l.log(L_FATAL, fmt.Sprintf(msg, args...))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Logger) log(level int, msg string) {
|
|
|
|
// output format: DATE PID [NAME] [GID] LEVEL file:line message
|
|
|
|
// output format: DATE PID [NAME] [GID] LEVEL file:line message
|
|
|
|
// 2001-10-10 12:00:00,000+0800 1234 app 987 INFO main.go:1234 log message ...
|
|
|
|
// 2001-10-10 12:00:00,000+0800 1234 app 987 INFO main.go:1234 log message ...
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
|
|
|
|
buf := buffer.Get().(*bytes.Buffer)
|
|
|
|
|
|
|
|
defer buffer.Put(buf)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if l.flags&F_TIME != 0 {
|
|
|
|
|
|
|
|
timeStr := time.Now().Format(l.timeFormat)
|
|
|
|
|
|
|
|
buf.WriteString(timeStr)
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.flags&F_PID != 0 {
|
|
|
|
buf.WriteString(strconv.Itoa(l.pid))
|
|
|
|
buf.WriteString(strconv.Itoa(l.pid))
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
|
|
|
|
}
|
|
|
|
if l.name != "" {
|
|
|
|
if l.name != "" {
|
|
|
|
buf.WriteString(l.name)
|
|
|
|
buf.WriteString(l.name)
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l.goroutineId || l.level == DEBUG {
|
|
|
|
if l.flags&F_GID != 0 {
|
|
|
|
buf.WriteString(strconv.FormatUint(gls.GoroutineID(), 10))
|
|
|
|
buf.WriteString(strconv.FormatUint(gls.GoroutineID(), 10))
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if l.colorizedLevel {
|
|
|
|
if l.flags&F_COLOR != 0 {
|
|
|
|
buf.WriteString(levelStrWithColor[level])
|
|
|
|
buf.WriteString(levelStrWithColor[level])
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
buf.WriteString(levelStr[level])
|
|
|
|
buf.WriteString(levelStr[level])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if l.flags&(F_LONG_FILE|F_SHORT_FILE) != 0 {
|
|
|
|
|
|
|
|
_, file, line, ok := runtime.Caller(l.callerSkip)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
file = "???"
|
|
|
|
|
|
|
|
line = 0
|
|
|
|
|
|
|
|
} else if l.flags&F_SHORT_FILE != 0 {
|
|
|
|
|
|
|
|
if index := strings.LastIndex(file, "/"); index >= 0 {
|
|
|
|
|
|
|
|
file = file[index+1:]
|
|
|
|
|
|
|
|
} else if index = strings.LastIndex(file, "\\"); index >= 0 {
|
|
|
|
|
|
|
|
file = file[index+1:]
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
buf.WriteString(file)
|
|
|
|
buf.WriteString(file)
|
|
|
|
buf.WriteByte(':')
|
|
|
|
buf.WriteByte(':')
|
|
|
|
buf.WriteString(strconv.Itoa(line))
|
|
|
|
buf.WriteString(strconv.Itoa(line))
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
buf.WriteByte(' ')
|
|
|
|
fmt.Fprintf(buf, msg, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString(msg)
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
buf.WriteByte('\n')
|
|
|
|
|
|
|
|
|
|
|
|
if level == FATAL {
|
|
|
|
if level == L_FATAL {
|
|
|
|
for i := l.callerSkip; ; i++ {
|
|
|
|
for i := l.callerSkip; ; i++ {
|
|
|
|
pc, file, line, ok := runtime.Caller(i)
|
|
|
|
pc, file, line, ok := runtime.Caller(i)
|
|
|
|
if !ok {
|
|
|
|
if !ok {
|
|
|
@ -205,11 +296,11 @@ func (l *Logger) log(level int, msg string, args ...interface{}) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
l.mu.Lock()
|
|
|
|
line := buf.Bytes()
|
|
|
|
defer l.mu.Unlock()
|
|
|
|
buf.Reset()
|
|
|
|
|
|
|
|
|
|
|
|
timeStr := time.Now().Format(l.timeLayout)
|
|
|
|
l.m.Lock()
|
|
|
|
|
|
|
|
defer l.m.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
l.out.Write([]byte(timeStr))
|
|
|
|
l.writer.Write(line)
|
|
|
|
l.out.Write(buf.Bytes())
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|