MM-37511 Add userid to http logs (#24516)

* include user_id in http logs
This commit is contained in:
Doug Lauder 2023-09-08 18:24:34 -04:00 committed by GitHub
parent 5ccc039278
commit 5e7d1c6f9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -149,6 +149,11 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
appInstance := app.New(app.ServerConnector(h.Srv.Channels()))
c := &Context{
AppContext: &request.Context{},
App: appInstance,
}
requestID := model.NewId()
var statusCode string
defer func() {
@ -157,6 +162,10 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mlog.String("url", r.URL.Path),
mlog.String("request_id", requestID),
}
// if there is a session then include the user_id
if c.AppContext.Session() != nil {
responseLogFields = append(responseLogFields, mlog.String("user_id", c.AppContext.Session().UserId))
}
// Websockets are returning status code 0 to requests after closing the socket
if statusCode != "0" {
responseLogFields = append(responseLogFields, mlog.String("status_code", statusCode))
@ -164,11 +173,6 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mlog.Debug("Received HTTP request", responseLogFields...)
}()
c := &Context{
AppContext: &request.Context{},
App: appInstance,
}
t, _ := i18n.GetTranslationsAndLocaleFromRequest(r)
c.AppContext = request.NewContext(
context.Background(),