Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xiaomi-tc/log15
Browse files Browse the repository at this point in the history
  • Loading branch information
devuser committed Jun 25, 2019
2 parents 2b5131c + 76b7e98 commit b3c99b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
9 changes: 8 additions & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ func LogfmtFormat() Format {
logMetaKey = r.MetaK
logMetaValue = r.MetaV

common := []interface{}{r.KeyNames.Time, r.Time, r.KeyNames.Lvl, r.Lvl, r.KeyNames.Call, r.Call.String(), r.KeyNames.Msg, r.Msg}
var caller string
if r.CustomCaller == "" {
caller = r.Call.String()
} else {
caller = r.CustomCaller
}

common := []interface{}{r.KeyNames.Time, r.Time, r.KeyNames.Lvl, r.Lvl, r.KeyNames.Call, caller, r.KeyNames.Msg, r.Msg}
buf := &bytes.Buffer{}
logfmt(buf, append(common, r.Ctx...), 0)
return buf.Bytes()
Expand Down
22 changes: 22 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type Record struct {
MetaV string
Ctx []interface{}
Call stack.Call
CustomCaller string
KeyNames RecordKeyNames
}

Expand Down Expand Up @@ -179,6 +180,27 @@ func (l *logger) writeMeta(msg string, lvl Lvl, metaType Meta, metaData interfac
}
}

func (l *logger) writeGorm(msg string, lvl Lvl, caller string, ctx []interface{}) {
if lvl <= l.setLv {
newCtx := make([]interface{}, 0, len(ctx))
newCtx = append(newCtx, ctx...)

l.h.Log(&Record{
Time: time.Now(),
Lvl: lvl,
Msg: msg,
Ctx: newContext(l.ctx, newCtx),
CustomCaller:caller,
KeyNames: RecordKeyNames{
Time: timeKey,
Msg: msgKey,
Lvl: lvlKey,
Call: callKey,
},
})
}
}

func (l *logger) New(ctx ...interface{}) Logger {
//child := &logger{newContext(l.ctx, ctx), new(swapHandler)} // increase one parament --[stevenmi]
child := &logger{newContext(l.ctx, ctx), new(swapHandler), LvlDebug}
Expand Down
5 changes: 5 additions & 0 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ func Crit(msg string, ctx ...interface{}) {
func MetaDebug(msg string, metaType Meta, metaData interface{}, ctx ...interface{}) {
root.writeMeta(msg, LvlDebug, metaType, metaData, ctx)
}

// GormInfo is used to support gorm logger
func GormInfo(msg string, caller string, ctx ...interface{}) {
root.writeGorm(msg, LvlInfo, caller, ctx)
}

0 comments on commit b3c99b6

Please sign in to comment.