Merge remote-tracking branch 'upstream/master'

This commit is contained in:
Tan Jiang 2016-04-01 19:02:25 +08:00
commit 23ff97e2b0

View File

@ -27,6 +27,8 @@ import (
var logger = New(os.Stdout, NewTextFormatter(), WarningLevel) var logger = New(os.Stdout, NewTextFormatter(), WarningLevel)
func init() { func init() {
logger.callDepth = 3
// TODO add item in configuaration file // TODO add item in configuaration file
lvl := os.Getenv("LOG_LEVEL") lvl := os.Getenv("LOG_LEVEL")
if len(lvl) == 0 { if len(lvl) == 0 {
@ -46,18 +48,20 @@ func init() {
// Logger provides a struct with fields that describe the details of logger. // Logger provides a struct with fields that describe the details of logger.
type Logger struct { type Logger struct {
out io.Writer out io.Writer
fmtter Formatter fmtter Formatter
lvl Level lvl Level
mu sync.Mutex callDepth int
mu sync.Mutex
} }
// New returns a customized Logger // New returns a customized Logger
func New(out io.Writer, fmtter Formatter, lvl Level) *Logger { func New(out io.Writer, fmtter Formatter, lvl Level) *Logger {
return &Logger{ return &Logger{
out: out, out: out,
fmtter: fmtter, fmtter: fmtter,
lvl: lvl, lvl: lvl,
callDepth: 2,
} }
} }
@ -117,7 +121,7 @@ func (l *Logger) output(record *Record) (err error) {
// Debug ... // Debug ...
func (l *Logger) Debug(v ...interface{}) { func (l *Logger) Debug(v ...interface{}) {
if l.lvl <= DebugLevel { if l.lvl <= DebugLevel {
line := line(2) line := line(l.callDepth)
record := NewRecord(time.Now(), fmt.Sprint(v...), line, DebugLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), line, DebugLevel)
l.output(record) l.output(record)
} }
@ -126,7 +130,7 @@ func (l *Logger) Debug(v ...interface{}) {
// Debugf ... // Debugf ...
func (l *Logger) Debugf(format string, v ...interface{}) { func (l *Logger) Debugf(format string, v ...interface{}) {
if l.lvl <= DebugLevel { if l.lvl <= DebugLevel {
line := line(2) line := line(l.callDepth)
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, DebugLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, DebugLevel)
l.output(record) l.output(record)
} }
@ -167,7 +171,7 @@ func (l *Logger) Warningf(format string, v ...interface{}) {
// Error ... // Error ...
func (l *Logger) Error(v ...interface{}) { func (l *Logger) Error(v ...interface{}) {
if l.lvl <= ErrorLevel { if l.lvl <= ErrorLevel {
line := line(2) line := line(l.callDepth)
record := NewRecord(time.Now(), fmt.Sprint(v...), line, ErrorLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), line, ErrorLevel)
l.output(record) l.output(record)
} }
@ -176,7 +180,7 @@ func (l *Logger) Error(v ...interface{}) {
// Errorf ... // Errorf ...
func (l *Logger) Errorf(format string, v ...interface{}) { func (l *Logger) Errorf(format string, v ...interface{}) {
if l.lvl <= ErrorLevel { if l.lvl <= ErrorLevel {
line := line(2) line := line(l.callDepth)
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, ErrorLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, ErrorLevel)
l.output(record) l.output(record)
} }
@ -185,7 +189,7 @@ func (l *Logger) Errorf(format string, v ...interface{}) {
// Fatal ... // Fatal ...
func (l *Logger) Fatal(v ...interface{}) { func (l *Logger) Fatal(v ...interface{}) {
if l.lvl <= FatalLevel { if l.lvl <= FatalLevel {
line := line(2) line := line(l.callDepth)
record := NewRecord(time.Now(), fmt.Sprint(v...), line, FatalLevel) record := NewRecord(time.Now(), fmt.Sprint(v...), line, FatalLevel)
l.output(record) l.output(record)
} }
@ -195,7 +199,7 @@ func (l *Logger) Fatal(v ...interface{}) {
// Fatalf ... // Fatalf ...
func (l *Logger) Fatalf(format string, v ...interface{}) { func (l *Logger) Fatalf(format string, v ...interface{}) {
if l.lvl <= FatalLevel { if l.lvl <= FatalLevel {
line := line(2) line := line(l.callDepth)
record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, FatalLevel) record := NewRecord(time.Now(), fmt.Sprintf(format, v...), line, FatalLevel)
l.output(record) l.output(record)
} }
@ -259,5 +263,12 @@ func line(calldepth int) string {
line = 0 line = 0
} }
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
file = file[i+1:]
break
}
}
return fmt.Sprintf("%s:%d", file, line) return fmt.Sprintf("%s:%d", file, line)
} }