func testHandler() (log.Handler, *log.Record) { rec := new(log.Record) return log.FuncHandler(func(r *log.Record) error { *rec = *r return nil }), rec }
// EscalateErrHandler wraps another handler and passes all records through // unchanged except if the logged context contains a non-nil error // value in its context. In that case, the record's level is raised // to LvlError unless it was already more serious (LvlCrit). // // This allows you to log the result of all functions for debugging // and still capture error conditions when in production with a single // log line. As an example, the following the log record will be written // out only if there was an error writing a value to redis: // // logger := logext.EscalateErrHandler( // log.LvlFilterHandler(log.LvlInfo, log.StdoutHandler)) // // reply, err := redisConn.Do("SET", "foo", "bar") // logger.Debug("Wrote value to redis", "reply", reply, "err", err) // if err != nil { // return err // } // func EscalateErrHandler(h log.Handler) log.Handler { return log.FuncHandler(func(r *log.Record) error { if r.Lvl > log.LvlError { for i := 1; i < len(r.Ctx); i++ { if v, ok := r.Ctx[i].(error); ok && v != nil { r.Lvl = log.LvlError break } } } return h.Log(r) }) }