func init() { lock := py.NewLock() defer lock.Unlock() /* Setup default targets */ stdoutTarget := log.NewTextTarget(os.Stdout) bufferingTarget := log.NewBufferingTarget(stdoutTarget) log.Targets["stdout"] = bufferingTarget /* Create package */ var err error var module *py.Module if module, err = modules.Init("gem.log", []py.Method{ {"begin_redirect", Py_begin_redirect, ""}, {"end_redirect", Py_end_redirect, ""}, }); err != nil { panic(err) } /* Register modules */ for _, registerFunc := range moduleRegisterFuncs { if err = registerFunc(module); err != nil { panic(err) } } }
func TestBuffer(t *testing.T) { mtarget := &mockTarget{make([]log.Record, 0)} buffer := log.NewBufferingTarget(mtarget) log.Targets = map[string]log.Handler{} log.Targets["buffer"] = buffer logger := log.New("testing", log.MapContext{ "A": 123, }) entries := []entry{ entry{ log.LvlInfo, "Log Message 1", }, entry{ log.LvlDebug, "Log Message 2", }, } buffer.Redirect() logger.Dispatch(entries[0].lvl, entries[0].msg) if len(buffer.Buffered()) != 1 { t.Error("Buffered length incorrect") } if len(mtarget.records) != 0 { t.Error("Handled length incorrect") } buffer.Flush() if buffer.Buffered() != nil { t.Error("Buffer not emptied") } if len(mtarget.records) != 1 { t.Error("Buffer not flushed") } logger.Dispatch(entries[1].lvl, entries[1].msg) if len(mtarget.records) != 2 { t.Error("Buffer not flushed") } records := mtarget.records for i, r := range records { if r.Level() != entries[i].lvl { t.Error("Log level mismatch") } if r.Tag() != "testing" { t.Error("Log tag mismatch") } if r.Message() != entries[i].msg { t.Error("Log message mismatch") } rctx := r.Context().ContextMap() if v, ok := rctx["A"]; !ok || v != 123 { t.Error("Log context mismatch") } } }