go/log/log.go

187 lines
3.1 KiB
Go
Raw Normal View History

2014-01-03 10:26:29 +04:00
package log
import (
"fmt"
"os"
2014-04-06 18:09:23 +04:00
"runtime"
"time"
2014-01-03 10:26:29 +04:00
)
const (
LevelTrace = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
)
2014-04-06 18:09:23 +04:00
const (
Ltime = iota << 1 //time format "2006/01/02 15:04:05"
Lfile //file.go:123
Llevel //[Trace|Debug|Info...]
)
2014-01-03 10:26:29 +04:00
var LevelName [6]string = [6]string{"Trace", "Debug", "Info", "Warn", "Error", "Fatal"}
2014-04-06 18:09:23 +04:00
const TimeFormat = "2006/01/02 15:04:05"
2014-03-24 17:45:22 +04:00
type Logger struct {
2014-04-06 18:09:23 +04:00
level int
flag int
2014-01-03 10:26:29 +04:00
handler Handler
2014-04-06 18:09:23 +04:00
quit chan struct{}
msg chan []byte
}
2014-03-28 10:11:15 +04:00
2014-03-24 17:45:22 +04:00
func New(handler Handler, flag int) *Logger {
var l = new(Logger)
2014-04-06 18:09:23 +04:00
2014-01-03 10:26:29 +04:00
l.level = LevelInfo
l.handler = handler
2014-04-06 18:09:23 +04:00
l.flag = flag
l.quit = make(chan struct{})
l.msg = make(chan []byte, 1024)
go l.run()
2014-01-03 10:26:29 +04:00
return l
}
2014-03-24 17:45:22 +04:00
func NewDefault(handler Handler) *Logger {
2014-04-06 18:09:23 +04:00
return New(handler, Ltime|Lfile|Llevel)
2014-01-03 10:26:29 +04:00
}
func newStdHandler() *StreamHandler {
2014-04-06 18:09:23 +04:00
h, _ := NewStreamHandler(os.Stdout)
2014-01-03 10:26:29 +04:00
return h
}
var std = NewDefault(newStdHandler())
2014-04-06 18:09:23 +04:00
func (l *Logger) run() {
for {
select {
case msg := <-l.msg:
l.handler.Write(msg)
case <-l.quit:
l.handler.Close()
}
}
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Close() {
2014-04-06 18:09:23 +04:00
if l.quit == nil {
return
}
close(l.quit)
l.quit = nil
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) SetLevel(level int) {
2014-01-03 10:26:29 +04:00
l.level = level
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Output(callDepth int, level int, format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
if l.level > level {
return
2014-01-03 10:26:29 +04:00
}
2014-04-06 18:09:23 +04:00
buf := make([]byte, 0, 1024)
if l.flag&Ltime > 0 {
now := time.Now().Format(TimeFormat)
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, fmt.Sprintf("%s:%d ", file, line)...)
}
if l.flag&Llevel > 0 {
buf = append(buf, fmt.Sprintf("[%s] ", LevelName[level])...)
}
s := fmt.Sprintf(format, v...)
buf = append(buf, s...)
2014-04-29 12:17:25 +04:00
if s[len(s)-1] != '\n' {
buf = append(buf, "\n"...)
}
2014-04-06 18:09:23 +04:00
l.msg <- buf
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Trace(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
l.Output(2, LevelTrace, format, v...)
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Debug(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
l.Output(2, LevelDebug, format, v...)
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Info(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
l.Output(2, LevelInfo, format, v...)
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Warn(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
l.Output(2, LevelWarn, format, v...)
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Error(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
l.Output(2, LevelError, format, v...)
2014-01-03 10:26:29 +04:00
}
2014-03-24 17:45:22 +04:00
func (l *Logger) Fatal(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
l.Output(2, LevelFatal, format, v...)
2014-01-03 10:26:29 +04:00
}
func SetLevel(level int) {
std.SetLevel(level)
}
func Trace(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
std.Output(2, LevelTrace, format, v...)
2014-01-03 10:26:29 +04:00
}
func Debug(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
std.Output(2, LevelDebug, format, v...)
2014-01-03 10:26:29 +04:00
}
func Info(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
std.Output(2, LevelInfo, format, v...)
2014-01-03 10:26:29 +04:00
}
func Warn(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
std.Output(2, LevelWarn, format, v...)
2014-01-03 10:26:29 +04:00
}
func Error(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
std.Output(2, LevelError, format, v...)
2014-01-03 10:26:29 +04:00
}
func Fatal(format string, v ...interface{}) {
2014-04-06 18:09:23 +04:00
std.Output(2, LevelFatal, format, v...)
2014-01-03 10:26:29 +04:00
}