mirror of
https://github.com/gomods/athens
synced 2026-02-03 11:00:32 +00:00
Adds a log format setting as ATHENS_LOG_FORMAT that can be either plain or JSON when CloudRuntime is none (the default). Does not break or change any existing behavior.
40 lines
939 B
Go
40 lines
939 B
Go
package middleware
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gomods/athens/pkg/log"
|
|
"github.com/gorilla/mux"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestLogContext(t *testing.T) {
|
|
h := func(w http.ResponseWriter, r *http.Request) {
|
|
e := log.EntryFromContext(r.Context())
|
|
e.Infof("test")
|
|
}
|
|
|
|
r := mux.NewRouter()
|
|
r.HandleFunc("/test", h)
|
|
|
|
var buf bytes.Buffer
|
|
lggr := log.New("", logrus.DebugLevel, "")
|
|
lggr.Formatter = &logrus.JSONFormatter{DisableTimestamp: true}
|
|
lggr.Out = &buf
|
|
|
|
r.Use(LogEntryMiddleware(lggr))
|
|
|
|
w := httptest.NewRecorder()
|
|
req, _ := http.NewRequest("GET", "/test", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
expected := `{"http-method":"GET","http-path":"/test","level":"info","msg":"test","request-id":""}`
|
|
assert.True(t, strings.Contains(buf.String(), expected), fmt.Sprintf("%s should contain: %s", buf.String(), expected))
|
|
}
|