func TestLogReporter(t *testing.T) { tests := []struct { err error out string }{ {errBoom, "request_id=1234 error=\"boom\"\n"}, {&Error{Err: errBoom}, "request_id=1234 error=\"boom\" line=0 file=unknown\n"}, {&Error{Err: errBoom, Backtrace: []*BacktraceLine{&BacktraceLine{File: "foo.go", Line: 1}}}, "request_id=1234 error=\"boom\" line=1 file=foo.go\n"}, } for i, tt := range tests { b := new(bytes.Buffer) l := logger.New(log.New(b, "request_id=1234 ", 0)) h := &LogReporter{} ctx := logger.WithLogger(context.Background(), l) if err := h.Report(ctx, tt.err); err != nil { t.Fatal(err) } if got, want := b.String(), tt.out; got != want { t.Fatalf("#%d: Output => %s; want %s", i, got, want) } } }
// InsertLogger returns an httpx.Handler middleware that will call f to generate // a logger, then insert it into the context. func InsertLogger(h httpx.Handler, f func(context.Context, *http.Request) logger.Logger) httpx.Handler { return httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { l := f(ctx, r) ctx = logger.WithLogger(ctx, l) return h.ServeHTTPContext(ctx, w, r) }) }
// newContext builds a new base Context object. func newContext(c *cli.Context) (ctx *Context, err error) { ctx = &Context{ Context: c, netCtx: context.Background(), } ctx.reporter, err = newReporter(ctx) if err != nil { return } ctx.logger, err = newLogger(ctx) if err != nil { return } ctx.stats, err = newStats(ctx) if err != nil { return } if ctx.reporter != nil { ctx.netCtx = reporter.WithReporter(ctx.netCtx, ctx.reporter) } if ctx.logger != nil { ctx.netCtx = logger.WithLogger(ctx.netCtx, ctx.logger) } if ctx.stats != nil { ctx.netCtx = stats.WithStats(ctx.netCtx, ctx.stats) } return }
func (h *commonTCPHandler) ServeTCP(ctx context.Context, conn net.Conn) { // Add a logger to the context l := logger.New(log.New(os.Stdout, "", 0)) ctx = logger.WithLogger(ctx, l) h.handler.ServeTCP(ctx, conn) }
func Example() { ctx := context.Background() ctx = logger.WithLogger(ctx, logger.Stdout) ping(ctx) // pong trace.id=<uuid> // trace.func=github.com/remind101/pkg/trace_test.ping trace.file=... trace.line=10 }
// PrefixRequestID adds the request as a prefix to the log15.Logger. func PrefixRequestID(h httpx.Handler) httpx.Handler { return httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { if l, ok := logger.FromContext(ctx); ok { if l, ok := l.(log15.Logger); ok { ctx = logger.WithLogger(ctx, l.New("request_id", httpx.RequestID(ctx))) } } return h.ServeHTTPContext(ctx, w, r) }) }
func (h *Logger) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) error { l := logger.New(log.New(h.Device, fmt.Sprintf("request_id=%s ", httpx.RequestID(ctx)), 0)) ctx = logger.WithLogger(ctx, l) l.Log( "at", "request", "method", r.Method, "path", fmt.Sprintf(`"%s"`, r.URL.Path), ) return h.handler.ServeHTTPContext(ctx, w, r) }
// newRootContext returns a new root context.Context with an error reporter and // logger embedded in the context. func newRootContext(c *cli.Context) (context.Context, error) { r, err := newReporter(c) if err != nil { return nil, err } l := newLogger() ctx := context.Background() if r != nil { ctx = reporter.WithReporter(ctx, r) } if l != nil { ctx = logger.WithLogger(ctx, l) } return ctx, nil }