Skip to content

Commit

Permalink
Fix cache logic
Browse files Browse the repository at this point in the history
  • Loading branch information
karantin2020 committed Nov 26, 2017
1 parent 64054c3 commit ba667a0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
14 changes: 7 additions & 7 deletions examples/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
)

func example_default() {
log.Debug("failed to fetch URL")
log.Info("failed to fetch URL")
log.Error("failed to fetch URL")
log.Debug("Debug: failed to fetch URL")
log.Info("Info: failed to fetch URL")
log.Error("Error: failed to fetch URL")
log.INFO.Fields(
qlog.F{"service", "new"},
qlog.F{"source", "after"},
).Msgf("failed to fetch %s", "URL")
log.Warn("failed to fetch URL")
log.Critical("failed to fetch URL")
// log.Panic("failed to fetch URL")
).Msgf("Info: failed to fetch %s", "URL")
log.Warn("Warn: failed to fetch URL")
log.Critical("Critical: failed to fetch URL")
// log.Panic("Panic: failed to fetch URL")
}
8 changes: 4 additions & 4 deletions examples/newInstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ func example_new() {
Timestamp().
SetOutput(qlog.Template("${time}\t${level}\t${message}\t${fields}\n"))
// fmt.Printf("%+v\n", nlog)
nlog.INFO.Msgf("failed to fetch %s", "URL")
nlog.INFO.Msg("failed to fetch 'URL'")
nlog.INFO.Msgf("INFO1: failed to fetch %s", "URL")
nlog.INFO.Msg("INFO2: failed to fetch 'URL'")

newlog := nlog.WithFields(
qlog.F{"service", "new"},
qlog.F{"source", "after"},
)
// fmt.Printf("%+v\n", newlog)
newlog.INFO.Msgf("failed to fetch %s", "URL")
newlog.INFO.Msg("failed to fetch 'URL'")
newlog.INFO.Msgf("INFO3: failed to fetch %s", "URL")
newlog.INFO.Msg("INFO4: failed to fetch 'URL'")
}
15 changes: 10 additions & 5 deletions template_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ func Template(template string, opts ...func(*TemplateOptions) error) func(np *No
buf.bb = buf.bb[:len(t.Tags)]
if c, ok := cache.fromCache(e.Logger); ok {
c.copyTo(buf)
GetEntryFields(e, t.Tags, options.upperTags, &buf.bb, &buf.pl, true)
} else {
GetEntryFields(e, t.Tags, options.upperTags, &buf.bb, &buf.pl)
GetEntryFields(e, t.Tags, options.upperTags, &buf.bb, &buf.pl, false)
cache.toCache(e.Logger, buf)
GetEntryFields(e, t.Tags, options.upperTags, &buf.bb, &buf.pl, true)
}
for i, t := range t.Tags {
if t == options.FieldsName {
Expand Down Expand Up @@ -240,7 +242,7 @@ func newTemplateOptions() *TemplateOptions {
}
}

func GetEntryFields(e *Entry, tags []string, upperTags []bool, tfields *[][]byte, fields *PairList) {
func GetEntryFields(e *Entry, tags []string, upperTags []bool, tfields *[][]byte, fields *PairList, cached bool) {

addFld := func(data []Field, encValues []*buffer.Buffer) {
for j, v := range data {
Expand All @@ -257,9 +259,12 @@ func GetEntryFields(e *Entry, tags []string, upperTags []bool, tfields *[][]byte
}
}
}
addFld(e.Logger.Notepad.Context, e.Logger.Notepad.CtxBuffer)
addFld(e.Logger.Context, e.Logger.CtxBuffer)
addFld(e.Data, e.Buffer)
if !cached {
addFld(e.Logger.Notepad.Context, e.Logger.Notepad.CtxBuffer)
addFld(e.Logger.Context, e.Logger.CtxBuffer)
} else {
addFld(e.Data, e.Buffer)
}
}

func stringInSlice(a string, list []string) (int, bool) {
Expand Down

0 comments on commit ba667a0

Please sign in to comment.