2014-09-24 05:38:19 +04:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
//log level, from low to high, more high means more serious
|
|
|
|
const (
|
|
|
|
LevelTrace = iota
|
|
|
|
LevelDebug
|
|
|
|
LevelInfo
|
|
|
|
LevelWarn
|
|
|
|
LevelError
|
|
|
|
LevelFatal
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
Ltime = 1 << iota //time format "2006/01/02 15:04:05"
|
|
|
|
Lfile //file.go:123
|
|
|
|
Llevel //[Trace|Debug|Info...]
|
|
|
|
)
|
|
|
|
|
|
|
|
var LevelName [6]string = [6]string{"Trace", "Debug", "Info", "Warn", "Error", "Fatal"}
|
|
|
|
|
|
|
|
const TimeFormat = "2006/01/02 15:04:05"
|
|
|
|
|
|
|
|
const maxBufPoolSize = 16
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
level int
|
|
|
|
flag int
|
|
|
|
|
|
|
|
handler Handler
|
|
|
|
|
|
|
|
quit chan struct{}
|
|
|
|
msg chan []byte
|
|
|
|
|
|
|
|
bufs [][]byte
|
2014-10-27 10:24:58 +03:00
|
|
|
|
|
|
|
wg sync.WaitGroup
|
|
|
|
|
|
|
|
closed bool
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//new a logger with specified handler and flag
|
|
|
|
func New(handler Handler, flag int) *Logger {
|
|
|
|
var l = new(Logger)
|
|
|
|
|
|
|
|
l.level = LevelInfo
|
|
|
|
l.handler = handler
|
|
|
|
|
|
|
|
l.flag = flag
|
|
|
|
|
|
|
|
l.quit = make(chan struct{})
|
2014-10-27 10:24:58 +03:00
|
|
|
l.closed = false
|
2014-09-24 05:38:19 +04:00
|
|
|
|
|
|
|
l.msg = make(chan []byte, 1024)
|
|
|
|
|
|
|
|
l.bufs = make([][]byte, 0, 16)
|
|
|
|
|
2014-10-27 10:24:58 +03:00
|
|
|
l.wg.Add(1)
|
2014-09-24 05:38:19 +04:00
|
|
|
go l.run()
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
//new a default logger with specified handler and flag: Ltime|Lfile|Llevel
|
|
|
|
func NewDefault(handler Handler) *Logger {
|
|
|
|
return New(handler, Ltime|Lfile|Llevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStdHandler() *StreamHandler {
|
|
|
|
h, _ := NewStreamHandler(os.Stdout)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
var std = NewDefault(newStdHandler())
|
|
|
|
|
|
|
|
func (l *Logger) run() {
|
2014-10-27 10:24:58 +03:00
|
|
|
defer l.wg.Done()
|
2014-09-24 05:38:19 +04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-l.msg:
|
|
|
|
l.handler.Write(msg)
|
|
|
|
l.putBuf(msg)
|
|
|
|
case <-l.quit:
|
2014-10-27 10:24:58 +03:00
|
|
|
//we must log all msg
|
|
|
|
if len(l.msg) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) popBuf() []byte {
|
|
|
|
l.Lock()
|
|
|
|
var buf []byte
|
|
|
|
if len(l.bufs) == 0 {
|
|
|
|
buf = make([]byte, 0, 1024)
|
|
|
|
} else {
|
|
|
|
buf = l.bufs[len(l.bufs)-1]
|
|
|
|
l.bufs = l.bufs[0 : len(l.bufs)-1]
|
|
|
|
}
|
|
|
|
l.Unlock()
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) putBuf(buf []byte) {
|
|
|
|
l.Lock()
|
|
|
|
if len(l.bufs) < maxBufPoolSize {
|
|
|
|
buf = buf[0:0]
|
|
|
|
l.bufs = append(l.bufs, buf)
|
|
|
|
}
|
|
|
|
l.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Close() {
|
2014-10-27 10:24:58 +03:00
|
|
|
if l.closed {
|
2014-09-24 05:38:19 +04:00
|
|
|
return
|
|
|
|
}
|
2014-10-27 10:24:58 +03:00
|
|
|
l.closed = true
|
2014-09-24 05:38:19 +04:00
|
|
|
|
|
|
|
close(l.quit)
|
2014-10-27 10:24:58 +03:00
|
|
|
|
|
|
|
l.wg.Wait()
|
|
|
|
|
2014-09-24 05:38:19 +04:00
|
|
|
l.quit = nil
|
2014-10-27 10:24:58 +03:00
|
|
|
|
|
|
|
l.handler.Close()
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//set log level, any log level less than it will not log
|
|
|
|
func (l *Logger) SetLevel(level int) {
|
|
|
|
l.level = level
|
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Output(callDepth int, level int, s string) {
|
2014-10-27 10:24:58 +03:00
|
|
|
if l.closed {
|
|
|
|
//closed
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-24 05:38:19 +04:00
|
|
|
if l.level > level {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := l.popBuf()
|
|
|
|
|
|
|
|
if l.flag&Ltime > 0 {
|
|
|
|
now := time.Now().Format(TimeFormat)
|
|
|
|
buf = append(buf, '[')
|
|
|
|
buf = append(buf, now...)
|
|
|
|
buf = append(buf, "] "...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.flag&Lfile > 0 {
|
|
|
|
_, file, line, ok := runtime.Caller(callDepth)
|
|
|
|
if !ok {
|
|
|
|
file = "???"
|
|
|
|
line = 0
|
|
|
|
} else {
|
|
|
|
for i := len(file) - 1; i > 0; i-- {
|
|
|
|
if file[i] == '/' {
|
|
|
|
file = file[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = append(buf, file...)
|
|
|
|
buf = append(buf, ':')
|
|
|
|
|
|
|
|
buf = strconv.AppendInt(buf, int64(line), 10)
|
|
|
|
buf = append(buf, ' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.flag&Llevel > 0 {
|
|
|
|
buf = append(buf, '[')
|
|
|
|
buf = append(buf, LevelName[level]...)
|
|
|
|
buf = append(buf, "] "...)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = append(buf, s...)
|
|
|
|
|
|
|
|
if s[len(s)-1] != '\n' {
|
|
|
|
buf = append(buf, '\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
l.msg <- buf
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with Trace level
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Trace(v ...interface{}) {
|
|
|
|
l.Output(2, LevelTrace, fmt.Sprint(v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//log with Debug level
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Debug(v ...interface{}) {
|
|
|
|
l.Output(2, LevelDebug, fmt.Sprint(v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//log with info level
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Info(v ...interface{}) {
|
|
|
|
l.Output(2, LevelInfo, fmt.Sprint(v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//log with warn level
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Warn(v ...interface{}) {
|
|
|
|
l.Output(2, LevelWarn, fmt.Sprint(v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//log with error level
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Error(v ...interface{}) {
|
|
|
|
l.Output(2, LevelError, fmt.Sprint(v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//log with fatal level
|
2015-01-12 04:37:31 +03:00
|
|
|
func (l *Logger) Fatal(v ...interface{}) {
|
|
|
|
l.Output(2, LevelFatal, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with Trace level
|
|
|
|
func (l *Logger) Tracef(format string, v ...interface{}) {
|
|
|
|
l.Output(2, LevelTrace, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with Debug level
|
|
|
|
func (l *Logger) Debugf(format string, v ...interface{}) {
|
|
|
|
l.Output(2, LevelDebug, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with info level
|
|
|
|
func (l *Logger) Infof(format string, v ...interface{}) {
|
|
|
|
l.Output(2, LevelInfo, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with warn level
|
|
|
|
func (l *Logger) Warnf(format string, v ...interface{}) {
|
|
|
|
l.Output(2, LevelWarn, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with error level
|
|
|
|
func (l *Logger) Errorf(format string, v ...interface{}) {
|
|
|
|
l.Output(2, LevelError, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
//log with fatal level
|
|
|
|
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|
|
|
l.Output(2, LevelFatal, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetLevel(level int) {
|
|
|
|
std.SetLevel(level)
|
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func Trace(v ...interface{}) {
|
|
|
|
std.Output(2, LevelTrace, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(v ...interface{}) {
|
|
|
|
std.Output(2, LevelDebug, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Info(v ...interface{}) {
|
|
|
|
std.Output(2, LevelInfo, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warn(v ...interface{}) {
|
|
|
|
std.Output(2, LevelWarn, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(v ...interface{}) {
|
|
|
|
std.Output(2, LevelError, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(v ...interface{}) {
|
|
|
|
std.Output(2, LevelFatal, fmt.Sprint(v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Tracef(format string, v ...interface{}) {
|
|
|
|
std.Output(2, LevelTrace, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
|
|
std.Output(2, LevelDebug, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func Infof(format string, v ...interface{}) {
|
|
|
|
std.Output(2, LevelInfo, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func Warnf(format string, v ...interface{}) {
|
|
|
|
std.Output(2, LevelWarn, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
std.Output(2, LevelError, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 04:37:31 +03:00
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
|
|
std.Output(2, LevelFatal, fmt.Sprintf(format, v...))
|
2014-09-24 05:38:19 +04:00
|
|
|
}
|