Add log format function
This commit is contained in:
parent
3cbefefec5
commit
e535bc4a46
36
log.go
36
log.go
@ -10,30 +10,46 @@ import (
|
|||||||
|
|
||||||
// All log functions
|
// All log functions
|
||||||
var (
|
var (
|
||||||
Debug = logFunc(LevelDebug)
|
Debug = logFunc(LevelDebug)
|
||||||
Info = logFunc(LevelInfo)
|
Debugf = logfFunc(LevelDebug)
|
||||||
Warning = logFunc(LevelWarning)
|
Info = logFunc(LevelInfo)
|
||||||
Error = logFunc(LevelError)
|
Infof = logfFunc(LevelInfo)
|
||||||
Critical = logFunc(LevelCritical)
|
Warning = logFunc(LevelWarning)
|
||||||
|
Warningf = logfFunc(LevelWarning)
|
||||||
|
Error = logFunc(LevelError)
|
||||||
|
Errorf = logfFunc(LevelError)
|
||||||
|
Critical = logFunc(LevelCritical)
|
||||||
|
Criticalf = logfFunc(LevelCritical)
|
||||||
)
|
)
|
||||||
|
|
||||||
func logFunc(level LevelType) func(...interface{}) {
|
func logFunc(level LevelType) func(...interface{}) {
|
||||||
return func(message ...interface{}) {
|
return func(message ...interface{}) {
|
||||||
writeMessage(level, message)
|
writeMessage(level, ``, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func logfFunc(level LevelType) func(string, ...interface{}) {
|
||||||
|
return func(format string, a ...interface{}) {
|
||||||
|
writeMessage(level, format, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var mu sync.Mutex
|
var mu sync.Mutex
|
||||||
|
|
||||||
func writeMessage(level LevelType, message []interface{}) {
|
func writeMessage(level LevelType, format string, message []interface{}) {
|
||||||
if MinLevel > level {
|
if MinLevel > level {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := LogEntry{
|
entry := LogEntry{
|
||||||
Level: level,
|
Level: level,
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
Message: fmt.Sprint(message...),
|
}
|
||||||
|
|
||||||
|
if format == `` {
|
||||||
|
entry.Message = fmt.Sprint(message...)
|
||||||
|
} else {
|
||||||
|
entry.Message = fmt.Sprintf(format, message...)
|
||||||
}
|
}
|
||||||
|
|
||||||
if MinStackLevel <= level {
|
if MinStackLevel <= level {
|
||||||
|
10
log_test.go
10
log_test.go
@ -55,6 +55,16 @@ func TestLogLevel(t *testing.T) {
|
|||||||
MinLevel, Output = LevelInfo, nil
|
MinLevel, Output = LevelInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogf(t *testing.T) {
|
||||||
|
b := bytes.Buffer{}
|
||||||
|
Output = &b
|
||||||
|
|
||||||
|
Infof(`Test%s-%d`, `tt`, 1)
|
||||||
|
assert.True(t, strings.Contains(b.String(), `Testtt-1`))
|
||||||
|
|
||||||
|
Output = nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestStackTrace(t *testing.T) {
|
func TestStackTrace(t *testing.T) {
|
||||||
b := bytes.Buffer{}
|
b := bytes.Buffer{}
|
||||||
Output = &b
|
Output = &b
|
||||||
|
Loading…
Reference in New Issue
Block a user