refactor interface like fmt Sprint and Sprintf

I think I will change some projects which use this package too !
This commit is contained in:
siddontang 2015-01-12 09:37:31 +08:00
parent 8f64946c30
commit 51d80cef1a
2 changed files with 81 additions and 29 deletions

View File

@ -142,9 +142,7 @@ func (l *Logger) SetLevel(level int) {
l.level = level l.level = level
} }
//a low interface, maybe you can use it for your special log format func (l *Logger) Output(callDepth int, level int, s string) {
//but it may be not exported later......
func (l *Logger) Output(callDepth int, level int, format string, v ...interface{}) {
if l.closed { if l.closed {
//closed //closed
return return
@ -190,8 +188,6 @@ func (l *Logger) Output(callDepth int, level int, format string, v ...interface{
buf = append(buf, "] "...) buf = append(buf, "] "...)
} }
s := fmt.Sprintf(format, v...)
buf = append(buf, s...) buf = append(buf, s...)
if s[len(s)-1] != '\n' { if s[len(s)-1] != '\n' {
@ -202,59 +198,113 @@ func (l *Logger) Output(callDepth int, level int, format string, v ...interface{
} }
//log with Trace level //log with Trace level
func (l *Logger) Trace(format string, v ...interface{}) { func (l *Logger) Trace(v ...interface{}) {
l.Output(2, LevelTrace, format, v...) l.Output(2, LevelTrace, fmt.Sprint(v...))
} }
//log with Debug level //log with Debug level
func (l *Logger) Debug(format string, v ...interface{}) { func (l *Logger) Debug(v ...interface{}) {
l.Output(2, LevelDebug, format, v...) l.Output(2, LevelDebug, fmt.Sprint(v...))
} }
//log with info level //log with info level
func (l *Logger) Info(format string, v ...interface{}) { func (l *Logger) Info(v ...interface{}) {
l.Output(2, LevelInfo, format, v...) l.Output(2, LevelInfo, fmt.Sprint(v...))
} }
//log with warn level //log with warn level
func (l *Logger) Warn(format string, v ...interface{}) { func (l *Logger) Warn(v ...interface{}) {
l.Output(2, LevelWarn, format, v...) l.Output(2, LevelWarn, fmt.Sprint(v...))
} }
//log with error level //log with error level
func (l *Logger) Error(format string, v ...interface{}) { func (l *Logger) Error(v ...interface{}) {
l.Output(2, LevelError, format, v...) l.Output(2, LevelError, fmt.Sprint(v...))
} }
//log with fatal level //log with fatal level
func (l *Logger) Fatal(format string, v ...interface{}) { func (l *Logger) Fatal(v ...interface{}) {
l.Output(2, LevelFatal, format, v...) 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...))
} }
func SetLevel(level int) { func SetLevel(level int) {
std.SetLevel(level) std.SetLevel(level)
} }
func Trace(format string, v ...interface{}) { func Trace(v ...interface{}) {
std.Output(2, LevelTrace, format, v...) std.Output(2, LevelTrace, fmt.Sprint(v...))
} }
func Debug(format string, v ...interface{}) { func Debug(v ...interface{}) {
std.Output(2, LevelDebug, format, v...) std.Output(2, LevelDebug, fmt.Sprint(v...))
} }
func Info(format string, v ...interface{}) { func Info(v ...interface{}) {
std.Output(2, LevelInfo, format, v...) std.Output(2, LevelInfo, fmt.Sprint(v...))
} }
func Warn(format string, v ...interface{}) { func Warn(v ...interface{}) {
std.Output(2, LevelWarn, format, v...) std.Output(2, LevelWarn, fmt.Sprint(v...))
} }
func Error(format string, v ...interface{}) { func Error(v ...interface{}) {
std.Output(2, LevelError, format, v...) std.Output(2, LevelError, fmt.Sprint(v...))
} }
func Fatal(format string, v ...interface{}) { func Fatal(v ...interface{}) {
std.Output(2, LevelFatal, format, v...) std.Output(2, LevelFatal, fmt.Sprint(v...))
}
func Tracef(format string, v ...interface{}) {
std.Output(2, LevelTrace, fmt.Sprintf(format, v...))
}
func Debugf(format string, v ...interface{}) {
std.Output(2, LevelDebug, fmt.Sprintf(format, v...))
}
func Infof(format string, v ...interface{}) {
std.Output(2, LevelInfo, fmt.Sprintf(format, v...))
}
func Warnf(format string, v ...interface{}) {
std.Output(2, LevelWarn, fmt.Sprintf(format, v...))
}
func Errorf(format string, v ...interface{}) {
std.Output(2, LevelError, fmt.Sprintf(format, v...))
}
func Fatalf(format string, v ...interface{}) {
std.Output(2, LevelFatal, fmt.Sprintf(format, v...))
} }

View File

@ -15,6 +15,8 @@ func TestStdStreamLog(t *testing.T) {
s.Info("can not log") s.Info("can not log")
Info("hello world") Info("hello world")
Infof("%s %d", "Hello", 123)
} }
func TestRotatingFileLog(t *testing.T) { func TestRotatingFileLog(t *testing.T) {