diff --git a/format.go b/format.go index 123a8e4..d2e07fe 100644 --- a/format.go +++ b/format.go @@ -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() diff --git a/logger.go b/logger.go index 80d0fc4..44f4ccd 100644 --- a/logger.go +++ b/logger.go @@ -91,6 +91,7 @@ type Record struct { MetaV string Ctx []interface{} Call stack.Call + CustomCaller string KeyNames RecordKeyNames } @@ -176,6 +177,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} diff --git a/root.go b/root.go index 97f57af..833b4b2 100644 --- a/root.go +++ b/root.go @@ -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) +}