Beispiel #1
0
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)
		}
	}
}
Beispiel #2
0
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)
}
Beispiel #3
0
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)
}
Beispiel #4
0
func stdLogger(out io.Writer) func(context.Context, *http.Request) logger.Logger {
	return func(ctx context.Context, r *http.Request) logger.Logger {
		return logger.New(log.New(out, fmt.Sprintf("request_id=%s ", httpx.RequestID(ctx)), 0))
	}
}