diff --git a/appservice/appservice.go b/appservice/appservice.go index d94a483e0c..4d61a3cefd 100644 --- a/appservice/appservice.go +++ b/appservice/appservice.go @@ -21,7 +21,6 @@ import ( "github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/appservice/consumers" @@ -29,6 +28,7 @@ import ( roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" "github.com/matrix-org/dendrite/setup/config" userapi "github.com/matrix-org/dendrite/userapi/api" + log "github.com/rs/zerolog/log" ) // NewInternalAPI returns a concerete implementation of the internal API. Callers @@ -59,9 +59,7 @@ func NewInternalAPI( for _, appservice := range cfg.Derived.ApplicationServices { // Create bot account for this AS if it doesn't already exist if err := generateAppServiceAccount(userAPI, appservice, cfg.Global.ServerName); err != nil { - logrus.WithFields(logrus.Fields{ - "appservice": appservice.ID, - }).WithError(err).Panicf("failed to generate bot account for appservice") + log.Logger.Panic().Str("appservice", appservice.ID).Err(err).Msg("failed to generate bot account for appservice") } } @@ -73,7 +71,7 @@ func NewInternalAPI( js, rsAPI, ) if err := consumer.Start(); err != nil { - logrus.WithError(err).Panicf("failed to start appservice roomserver consumer") + log.Panic().Err(err).Msg("failed to start appservice roomserver consumer") } return appserviceQueryAPI diff --git a/appservice/consumers/roomserver.go b/appservice/consumers/roomserver.go index c02d904041..d70ac000cc 100644 --- a/appservice/consumers/roomserver.go +++ b/appservice/consumers/roomserver.go @@ -36,7 +36,7 @@ import ( "github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/dendrite/syncapi/synctypes" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) // ApplicationServiceTransaction is the transaction that is sent off to an @@ -104,7 +104,7 @@ func (s *OutputRoomEventConsumer) Start() error { func (s *OutputRoomEventConsumer) onMessage( ctx context.Context, state *appserviceState, msgs []*nats.Msg, ) bool { - log.WithField("appservice", state.ID).Tracef("Appservice worker received %d message(s) from roomserver", len(msgs)) + log.Trace().Str("appservice", state.ID).Msgf("Appservice worker received %d message(s) from roomserver", len(msgs)) events := make([]*types.HeaderedEvent, 0, len(msgs)) for _, msg := range msgs { // Only handle events we care about @@ -116,7 +116,7 @@ func (s *OutputRoomEventConsumer) onMessage( var output api.OutputEvent if err := json.Unmarshal(msg.Data, &output); err != nil { // If the message was invalid, log it and move on to the next message in the stream - log.WithField("appservice", state.ID).WithError(err).Errorf("Appservice failed to parse message, ignoring") + log.Error().Str("appservice", state.ID).Err(err).Msg("Appservice failed to parse message, ignoring") continue } switch output.Type { @@ -139,7 +139,7 @@ func (s *OutputRoomEventConsumer) onMessage( } if len(eventsReq.EventIDs) > 0 { if err := s.rsAPI.QueryEventsByID(s.ctx, eventsReq, eventsRes); err != nil { - log.WithError(err).Errorf("s.rsAPI.QueryEventsByID failed") + log.Error().Err(err).Msg("s.rsAPI.QueryEventsByID failed") return false } events = append(events, eventsRes.Events...) @@ -167,7 +167,7 @@ func (s *OutputRoomEventConsumer) onMessage( // Send event to any relevant application services. If we hit // an error here, return false, so that we negatively ack. - log.WithField("appservice", state.ID).Debugf("Appservice worker sending %d events(s) from roomserver", len(events)) + log.Debug().Str("appservice", state.ID).Msgf("Appservice worker sending %d events(s) from roomserver", len(events)) return s.sendEvents(ctx, state, events, txnID) == nil } @@ -223,7 +223,7 @@ func (s *appserviceState) backoffAndPause(err error) error { s.backoff++ } duration := time.Second * time.Duration(math.Pow(2, float64(s.backoff))) - log.WithField("appservice", s.ID).WithError(err).Errorf("Unable to send transaction to appservice, backing off for %s", duration.String()) + log.Error().Str("appservice", s.ID).Err(err).Msgf("Unable to send transaction to appservice, backing off for %s", duration.String()) time.Sleep(duration) return err } @@ -258,10 +258,7 @@ func (s *OutputRoomEventConsumer) appserviceIsInterestedInEvent(ctx context.Cont } } } else { - log.WithFields(log.Fields{ - "appservice": appservice.ID, - "room_id": event.RoomID(), - }).WithError(err).Errorf("Unable to get aliases for room") + log.Error().Str("appservice", appservice.ID).Str("room_id", event.RoomID()).Err(err).Msg("Unable to get aliases for room") } // Check if any of the members in the room match the appservice @@ -303,10 +300,7 @@ func (s *OutputRoomEventConsumer) appserviceJoinedAtEvent(ctx context.Context, e } } } else { - log.WithFields(log.Fields{ - "appservice": appservice.ID, - "room_id": event.RoomID(), - }).WithError(err).Errorf("Unable to get membership for room") + log.Error().Str("appservice", appservice.ID).Str("room_id", event.RoomID()).Err(err).Msg("Unable to get membership for room") } return false } diff --git a/appservice/query/query.go b/appservice/query/query.go index ca8d7b3a32..05e0e9cf17 100644 --- a/appservice/query/query.go +++ b/appservice/query/query.go @@ -25,7 +25,7 @@ import ( "strings" "sync" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/internal" @@ -77,15 +77,12 @@ func (a *AppServiceQueryAPI) RoomAliasExists( defer func() { err = resp.Body.Close() if err != nil { - log.WithFields(log.Fields{ - "appservice_id": appservice.ID, - "status_code": resp.StatusCode, - }).WithError(err).Error("Unable to close application service response body") + log.Error().Err(err).Str("appservice_id", appservice.ID).Int("status_code", resp.StatusCode).Msg("Unable to close application service response body") } }() } if err != nil { - log.WithError(err).Errorf("Issue querying room alias on application service %s", appservice.ID) + log.Error().Err(err).Msgf("Issue querying room alias on application service %s", appservice.ID) return err } switch resp.StatusCode { @@ -97,10 +94,7 @@ func (a *AppServiceQueryAPI) RoomAliasExists( // Room does not exist default: // Application service reported an error. Warn - log.WithFields(log.Fields{ - "appservice_id": appservice.ID, - "status_code": resp.StatusCode, - }).Warn("Application service responded with non-OK status code") + log.Warn().Str("appservice_id", appservice.ID).Int("status_code", resp.StatusCode).Msg("Application service responded with non-OK status code") } } } @@ -141,17 +135,12 @@ func (a *AppServiceQueryAPI) UserIDExists( defer func() { err = resp.Body.Close() if err != nil { - log.WithFields(log.Fields{ - "appservice_id": appservice.ID, - "status_code": resp.StatusCode, - }).Error("Unable to close application service response body") + log.Error().Str("appservice_id", appservice.ID).Int("status_code", resp.StatusCode).Msg("Unable to close application service response body") } }() } if err != nil { - log.WithFields(log.Fields{ - "appservice_id": appservice.ID, - }).WithError(err).Error("issue querying user ID on application service") + log.Error().Err(err).Str("appservice_id", appservice.ID).Msg("issue querying user ID on application service") return err } if resp.StatusCode == http.StatusOK { @@ -161,10 +150,7 @@ func (a *AppServiceQueryAPI) UserIDExists( } // Log non OK - log.WithFields(log.Fields{ - "appservice_id": appservice.ID, - "status_code": resp.StatusCode, - }).Warn("application service responded with non-OK status code") + log.Warn().Str("appservice_id", appservice.ID).Int("status_code", resp.StatusCode).Msg("application service responded with non-OK status code") } } @@ -217,7 +203,7 @@ func (a *AppServiceQueryAPI) Locations( } if err := requestDo[[]api.ASLocationResponse](as.HTTPClient, url+"?"+params.Encode(), &asLocations); err != nil { - log.WithError(err).Error("unable to get 'locations' from application service") + log.Error().Err(err).Msg("unable to get 'locations' from application service") continue } @@ -252,7 +238,7 @@ func (a *AppServiceQueryAPI) User( } if err := requestDo[[]api.ASUserResponse](as.HTTPClient, url+"?"+params.Encode(), &asUsers); err != nil { - log.WithError(err).Error("unable to get 'user' from application service") + log.Error().Err(err).Msg("unable to get 'user' from application service") continue } @@ -290,7 +276,7 @@ func (a *AppServiceQueryAPI) Protocols( for _, as := range a.Cfg.Derived.ApplicationServices { var proto api.ASProtocolResponse if err := requestDo[api.ASProtocolResponse](as.HTTPClient, as.RequestUrl()+api.ASProtocolPath+req.Protocol, &proto); err != nil { - log.WithError(err).Error("unable to get 'protocol' from application service") + log.Error().Err(err).Msg("unable to get 'protocol' from application service") continue } @@ -320,7 +306,7 @@ func (a *AppServiceQueryAPI) Protocols( for _, p := range as.Protocols { var proto api.ASProtocolResponse if err := requestDo[api.ASProtocolResponse](as.HTTPClient, as.RequestUrl()+api.ASProtocolPath+p, &proto); err != nil { - log.WithError(err).Error("unable to get 'protocol' from application service") + log.Error().Err(err).Msg("unable to get 'protocol' from application service") continue } existing, ok := response[p] diff --git a/clientapi/auth/user_interactive.go b/clientapi/auth/user_interactive.go index 9971bf8a44..9c3354973b 100644 --- a/clientapi/auth/user_interactive.go +++ b/clientapi/auth/user_interactive.go @@ -24,8 +24,9 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/util" - "github.com/sirupsen/logrus" "github.com/tidwall/gjson" + + log "github.com/rs/zerolog/log" ) // Type represents an auth type @@ -177,7 +178,7 @@ func (u *UserInteractive) challenge(sessionID string) *util.JSONResponse { func (u *UserInteractive) NewSession() *util.JSONResponse { sessionID, err := GenerateAccessToken() if err != nil { - logrus.WithError(err).Error("failed to generate session ID") + log.Error().Err(err).Msg("failed to generate session ID") res := jsonerror.InternalServerError() return &res } diff --git a/clientapi/jsonerror/jsonerror.go b/clientapi/jsonerror/jsonerror.go index 436e168ab5..adc6fad833 100644 --- a/clientapi/jsonerror/jsonerror.go +++ b/clientapi/jsonerror/jsonerror.go @@ -21,7 +21,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) // MatrixError represents the "standard error response" in Matrix. @@ -235,7 +235,7 @@ func NotTrusted(serverName string) *MatrixError { // InternalAPIError is returned when Dendrite failed to reach an internal API. func InternalAPIError(ctx context.Context, err error) util.JSONResponse { - logrus.WithContext(ctx).WithError(err).Error("Error reaching an internal API") + log.Error().Any("context", ctx).Err(err).Msg("Error reaching an internal API") return util.JSONResponse{ Code: http.StatusInternalServerError, JSON: &MatrixError{ diff --git a/clientapi/producers/syncapi.go b/clientapi/producers/syncapi.go index 905ecce4d0..7edf88fe19 100644 --- a/clientapi/producers/syncapi.go +++ b/clientapi/producers/syncapi.go @@ -24,7 +24,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/nats-io/nats.go" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/syncapi/types" @@ -56,7 +56,7 @@ func (p *SyncAPIProducer) SendReceipt( m.Header.Set("type", receiptType) m.Header.Set("timestamp", fmt.Sprintf("%d", timestamp)) - log.WithFields(log.Fields{}).Tracef("Producing to topic '%s'", p.TopicReceiptEvent) + log.Trace().Msgf("Producing to topic '%s'", p.TopicReceiptEvent) _, err := p.JetStream.PublishMsg(m, nats.Context(ctx)) return err } @@ -91,11 +91,7 @@ func (p *SyncAPIProducer) SendToDevice( devices = append(devices, deviceID) } - log.WithFields(log.Fields{ - "user_id": userID, - "num_devices": len(devices), - "type": eventType, - }).Tracef("Producing to topic '%s'", p.TopicSendToDeviceEvent) + log.Trace().Str("user_id", userID).Int("num_devices", len(devices)).Str("type", eventType).Msgf("Producing to topic '%s'", p.TopicSendToDeviceEvent) for i, device := range devices { ote := &types.OutputSendToDeviceEvent{ UserID: userID, @@ -109,7 +105,7 @@ func (p *SyncAPIProducer) SendToDevice( eventJSON, err := json.Marshal(ote) if err != nil { - log.WithError(err).Error("sendToDevice failed json.Marshal") + log.Error().Err(err).Msg("sendToDevice failed json.Marshal") return err } m := nats.NewMsg(p.TopicSendToDeviceEvent) @@ -119,10 +115,10 @@ func (p *SyncAPIProducer) SendToDevice( if _, err = p.JetStream.PublishMsg(m, nats.Context(ctx)); err != nil { if i < len(devices)-1 { - log.WithError(err).Warn("sendToDevice failed to PublishMsg, trying further devices") + log.Warn().Err(err).Msg("sendToDevice failed to PublishMsg, trying further devices") continue } - log.WithError(err).Error("sendToDevice failed to PublishMsg for all devices") + log.Error().Err(err).Msg("sendToDevice failed to PublishMsg for all devices") return err } } diff --git a/clientapi/routing/admin.go b/clientapi/routing/admin.go index a5fc4ec486..cd09543c5e 100644 --- a/clientapi/routing/admin.go +++ b/clientapi/routing/admin.go @@ -15,7 +15,8 @@ import ( "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/util" "github.com/nats-io/nats.go" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/internal/httputil" @@ -40,7 +41,7 @@ func AdminEvacuateRoom(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAP JSON: jsonerror.NotFound(err.Error()), } default: - logrus.WithError(err).WithField("roomID", vars["roomID"]).Error("Failed to evacuate room") + log.Error().Err(err).Str("roomID", vars["roomID"]).Msg("Failed to evacuate room") return util.ErrorResponse(err) } return util.JSONResponse{ @@ -59,7 +60,7 @@ func AdminEvacuateUser(req *http.Request, rsAPI roomserverAPI.ClientRoomserverAP affected, err := rsAPI.PerformAdminEvacuateUser(req.Context(), vars["userID"]) if err != nil { - logrus.WithError(err).WithField("userID", vars["userID"]).Error("Failed to evacuate user") + log.Error().Err(err).Str("userID", vars["userID"]).Msg("Failed to evacuate user") return util.MessageResponse(http.StatusBadRequest, err.Error()) } @@ -169,7 +170,7 @@ func AdminResetPassword(req *http.Request, cfg *config.ClientAPI, device *api.De func AdminReindex(req *http.Request, cfg *config.ClientAPI, device *api.Device, natsClient *nats.Conn) util.JSONResponse { _, err := natsClient.RequestMsg(nats.NewMsg(cfg.Matrix.JetStream.Prefixed(jetstream.InputFulltextReindex)), time.Second*10) if err != nil { - logrus.WithError(err).Error("failed to publish nats message") + log.Error().Err(err).Msg("failed to publish nats message") return jsonerror.InternalServerError() } return util.JSONResponse{ @@ -238,11 +239,7 @@ func AdminDownloadState(req *http.Request, device *api.Device, rsAPI roomserverA JSON: jsonerror.NotFound(eventutil.ErrRoomNoExists.Error()), } } - logrus.WithError(err).WithFields(logrus.Fields{ - "userID": device.UserID, - "serverName": serverName, - "roomID": roomID, - }).Error("failed to download state") + log.Error().Err(err).Str("userID", device.UserID).Str("serverName", serverName).Str("roomID", roomID).Msg("failed to download state") return util.ErrorResponse(err) } return util.JSONResponse{ diff --git a/clientapi/routing/createroom.go b/clientapi/routing/createroom.go index abf5b4f465..bf4502626e 100644 --- a/clientapi/routing/createroom.go +++ b/clientapi/routing/createroom.go @@ -37,7 +37,7 @@ import ( "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) // https://matrix.org/docs/spec/client_server/r0.2.0.html#post-matrix-client-r0-createroom diff --git a/clientapi/routing/password.go b/clientapi/routing/password.go index f7f9da622b..57027643e5 100644 --- a/clientapi/routing/password.go +++ b/clientapi/routing/password.go @@ -12,7 +12,7 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/util" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) type newPasswordRequest struct { @@ -37,10 +37,7 @@ func Password( var r newPasswordRequest r.LogoutDevices = true - logrus.WithFields(logrus.Fields{ - "sessionId": device.SessionID, - "userId": device.UserID, - }).Debug("Changing password") + log.Debug().Int64("sessionId", device.SessionID).Str("userId", device.UserID).Msg("Changing password") // Unmarshal the request. resErr := httputil.UnmarshalJSONRequest(req, &r) diff --git a/clientapi/routing/peekroom.go b/clientapi/routing/peekroom.go index 3937b9ad25..30b2ad2bf4 100644 --- a/clientapi/routing/peekroom.go +++ b/clientapi/routing/peekroom.go @@ -24,7 +24,7 @@ import ( "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/util" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) func PeekRoomByIDOrAlias( @@ -75,7 +75,7 @@ func PeekRoomByIDOrAlias( } case nil: default: - logrus.WithError(err).WithField("roomID", roomIDOrAlias).Errorf("Failed to peek room") + log.Error().Err(err).Str("roomID",roomIDOrAlias).Msg("Failed to peek room") return jsonerror.InternalServerError() } @@ -111,7 +111,7 @@ func UnpeekRoomByID( } case nil: default: - logrus.WithError(err).WithField("roomID", roomID).Errorf("Failed to un-peek room") + log.Error().Err(err).Str("roomID",roomID).Msg("Failed to un-peek room") return jsonerror.InternalServerError() } diff --git a/clientapi/routing/presence.go b/clientapi/routing/presence.go index c50b09434f..c345877861 100644 --- a/clientapi/routing/presence.go +++ b/clientapi/routing/presence.go @@ -30,7 +30,7 @@ import ( "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/util" "github.com/nats-io/nats.go" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) type presenceReq struct { @@ -72,7 +72,7 @@ func SetPresence( } err := producer.SendPresence(req.Context(), userID, presenceStatus, presence.StatusMsg) if err != nil { - log.WithError(err).Errorf("failed to update presence") + log.Error().Err(err).Msgf("failed to update presence") return util.JSONResponse{ Code: http.StatusInternalServerError, JSON: jsonerror.InternalServerError(), @@ -97,7 +97,7 @@ func GetPresence( presence, err := natsClient.RequestMsg(msg, time.Second*10) if err != nil { - log.WithError(err).Errorf("unable to get presence") + log.Error().Err(err).Msg("unable to get presence") return util.JSONResponse{ Code: http.StatusInternalServerError, JSON: jsonerror.InternalServerError(), @@ -107,7 +107,7 @@ func GetPresence( statusMsg := presence.Header.Get("status_msg") e := presence.Header.Get("error") if e != "" { - log.Errorf("received error msg from nats: %s", e) + log.Error().Msgf("received error msg from nats: %s", e) return util.JSONResponse{ Code: http.StatusOK, JSON: types.PresenceClientResponse{ diff --git a/clientapi/routing/receipt.go b/clientapi/routing/receipt.go index 634b60b717..368649ef7a 100644 --- a/clientapi/routing/receipt.go +++ b/clientapi/routing/receipt.go @@ -27,19 +27,14 @@ import ( "github.com/matrix-org/dendrite/userapi/api" userapi "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/util" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" ) func SetReceipt(req *http.Request, userAPI api.ClientUserAPI, syncProducer *producers.SyncAPIProducer, device *userapi.Device, roomID, receiptType, eventID string) util.JSONResponse { timestamp := spec.AsTimestamp(time.Now()) - logrus.WithFields(logrus.Fields{ - "roomID": roomID, - "receiptType": receiptType, - "eventID": eventID, - "userId": device.UserID, - "timestamp": timestamp, - }).Debug("Setting receipt") - + /// TODO: Check timestamp => timestamp.Time().Unix() for compat issues + log.Debug().Str("roomID", roomID).Str("receiptType", receiptType).Str("eventID", eventID).Str("userId", device.UserID).Int64("timestamp", timestamp.Time().Unix()).Msg("Setting receipt") switch receiptType { case "m.read", "m.read.private": if err := syncProducer.SendReceipt(req.Context(), device.UserID, roomID, eventID, receiptType, timestamp); err != nil { diff --git a/clientapi/routing/register.go b/clientapi/routing/register.go index 35dd4846f8..43ab0fdcb1 100644 --- a/clientapi/routing/register.go +++ b/clientapi/routing/register.go @@ -36,19 +36,18 @@ import ( "github.com/matrix-org/dendrite/internal/eventutil" "github.com/matrix-org/dendrite/setup/config" - "github.com/matrix-org/gomatrixserverlib" - "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/matrix-org/gomatrixserverlib/tokens" - "github.com/matrix-org/util" - "github.com/prometheus/client_golang/prometheus" - log "github.com/sirupsen/logrus" - "github.com/matrix-org/dendrite/clientapi/auth" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" "github.com/matrix-org/dendrite/clientapi/userutil" userapi "github.com/matrix-org/dendrite/userapi/api" + "github.com/matrix-org/gomatrixserverlib" + "github.com/matrix-org/gomatrixserverlib/spec" + "github.com/matrix-org/gomatrixserverlib/tokens" + "github.com/matrix-org/util" + "github.com/prometheus/client_golang/prometheus" + log "github.com/rs/zerolog/log" ) var ( diff --git a/clientapi/routing/routing.go b/clientapi/routing/routing.go index 70299e14d6..5bf93bc7d1 100644 --- a/clientapi/routing/routing.go +++ b/clientapi/routing/routing.go @@ -20,15 +20,6 @@ import ( "strings" "github.com/gorilla/mux" - "github.com/matrix-org/dendrite/setup/base" - userapi "github.com/matrix-org/dendrite/userapi/api" - "github.com/matrix-org/gomatrixserverlib/fclient" - "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/matrix-org/util" - "github.com/nats-io/nats.go" - "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" - appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/api" "github.com/matrix-org/dendrite/clientapi/auth" @@ -39,8 +30,16 @@ import ( "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/internal/transactions" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/dendrite/setup/base" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" + userapi "github.com/matrix-org/dendrite/userapi/api" + "github.com/matrix-org/gomatrixserverlib/fclient" + "github.com/matrix-org/gomatrixserverlib/spec" + "github.com/matrix-org/util" + "github.com/nats-io/nats.go" + "github.com/prometheus/client_golang/prometheus" + log "github.com/rs/zerolog/log" ) // Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client @@ -86,7 +85,7 @@ func Setup( } if cfg.Matrix.WellKnownClientName != "" { - logrus.Infof("Setting m.homeserver base_url as %s at /.well-known/matrix/client", cfg.Matrix.WellKnownClientName) + log.Info().Msgf("Setting m.homeserver base_url as %s at /.well-known/matrix/client", cfg.Matrix.WellKnownClientName) wkMux.Handle("/client", httputil.MakeExternalAPI("wellknown", func(r *http.Request) util.JSONResponse { return util.JSONResponse{ Code: http.StatusOK, @@ -129,7 +128,7 @@ func Setup( ).Methods(http.MethodGet, http.MethodOptions) if cfg.RegistrationSharedSecret != "" { - logrus.Info("Enabling shared secret registration at /_synapse/admin/v1/register") + log.Info().Msg("Enabling shared secret registration at /_synapse/admin/v1/register") sr := NewSharedSecretRegistration(cfg.RegistrationSharedSecret) synapseAdminRouter.Handle("/admin/v1/register", httputil.MakeExternalAPI("shared_secret_registration", func(req *http.Request) util.JSONResponse { @@ -198,10 +197,10 @@ func Setup( // server notifications if cfg.Matrix.ServerNotices.Enabled { - logrus.Info("Enabling server notices at /_synapse/admin/v1/send_server_notice") + log.Info().Msg("Enabling server notices at /_synapse/admin/v1/send_server_notice") serverNotificationSender, err := getSenderDevice(context.Background(), rsAPI, userAPI, cfg) if err != nil { - logrus.WithError(err).Fatal("unable to get account for sending sending server notices") + log.Fatal().Err(err).Msg("unable to get account for sending sending server notices") } synapseAdminRouter.Handle("/admin/v1/send_server_notice/{txnID}", diff --git a/clientapi/routing/server_notices.go b/clientapi/routing/server_notices.go index a9967adfe6..775695eeed 100644 --- a/clientapi/routing/server_notices.go +++ b/clientapi/routing/server_notices.go @@ -21,15 +21,15 @@ import ( "net/http" "time" + "github.com/matrix-org/dendrite/roomserver/types" + "github.com/matrix-org/dendrite/roomserver/version" "github.com/matrix-org/gomatrix" "github.com/matrix-org/gomatrixserverlib/tokens" "github.com/matrix-org/util" "github.com/prometheus/client_golang/prometheus" + log "github.com/rs/zerolog/log" "github.com/sirupsen/logrus" - "github.com/matrix-org/dendrite/roomserver/types" - "github.com/matrix-org/dendrite/roomserver/version" - appserviceAPI "github.com/matrix-org/dendrite/appservice/api" "github.com/matrix-org/dendrite/clientapi/httputil" "github.com/matrix-org/dendrite/clientapi/jsonerror" @@ -208,7 +208,7 @@ func SendServerNotice( } e, resErr := generateSendEvent(ctx, request, senderDevice, roomID, "m.room.message", nil, cfgClient, rsAPI, time.Now()) if resErr != nil { - logrus.Errorf("failed to send message: %+v", resErr) + log.Error().Msgf("failed to send message: %+v", resErr) return *resErr } timeToGenerateEvent := time.Since(startedGeneratingEvent) diff --git a/clientapi/routing/state.go b/clientapi/routing/state.go index 705782e884..3740973623 100644 --- a/clientapi/routing/state.go +++ b/clientapi/routing/state.go @@ -28,7 +28,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/matrix-org/util" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) type stateEventInStateResp struct { diff --git a/cmd/create-account/main.go b/cmd/create-account/main.go index 772778680d..da0462843e 100644 --- a/cmd/create-account/main.go +++ b/cmd/create-account/main.go @@ -29,9 +29,10 @@ import ( "time" "github.com/matrix-org/dendrite/internal" + + "github.com/rs/zerolog" "github.com/tidwall/gjson" - "github.com/sirupsen/logrus" "golang.org/x/term" "github.com/matrix-org/dendrite/setup" @@ -73,6 +74,8 @@ var cl = http.Client{ Transport: http.DefaultTransport, } +var logger = zerolog.New(os.Stdout).With().Timestamp().Logger() + func main() { name := os.Args[0] flag.Usage = func() { @@ -82,11 +85,11 @@ func main() { cfg := setup.ParseFlags(true) if *resetPassword { - logrus.Fatalf("The reset-password flag has been replaced by the POST /_dendrite/admin/resetPassword/{localpart} admin API.") + logger.Fatal().Msgf("The reset-password flag has been replaced by the POST /_dendrite/admin/resetPassword/{localpart} admin API.") } if cfg.ClientAPI.RegistrationSharedSecret == "" { - logrus.Fatalln("Shared secret registration is not enabled, enable it by setting a shared secret in the config: 'client_api.registration_shared_secret'") + logger.Fatal().Msgf("Shared secret registration is not enabled, enable it by setting a shared secret in the config: 'client_api.registration_shared_secret'") } if *username == "" { @@ -95,17 +98,17 @@ func main() { } if err := internal.ValidateUsername(*username, cfg.Global.ServerName); err != nil { - logrus.WithError(err).Error("Specified username is invalid") + logger.Error().Err(err).Msg("Specified username is invalid") os.Exit(1) } pass, err := getPassword(*password, *pwdFile, *pwdStdin, os.Stdin) if err != nil { - logrus.Fatalln(err) + logger.Fatal().Err(err) } if err = internal.ValidatePassword(pass); err != nil { - logrus.WithError(err).Error("Specified password is invalid") + logger.Error().Err(err).Msg("Specified password is invalid") os.Exit(1) } @@ -113,10 +116,10 @@ func main() { accessToken, err := sharedSecretRegister(cfg.ClientAPI.RegistrationSharedSecret, *serverURL, *username, pass, *isAdmin) if err != nil { - logrus.Fatalln("Failed to create the account:", err.Error()) + logger.Fatal().Err(err).Msg("Failed to create the account.") } - logrus.Infof("Created account: %s (AccessToken: %s)", *username, accessToken) + logger.Info().Msgf("Created account: %s (AccessToken: %s)", *username, accessToken) } type sharedSecretRegistrationRequest struct { diff --git a/cmd/dendrite-demo-pinecone/main.go b/cmd/dendrite-demo-pinecone/main.go index 18d1dd31f7..1471e740e4 100644 --- a/cmd/dendrite-demo-pinecone/main.go +++ b/cmd/dendrite-demo-pinecone/main.go @@ -34,7 +34,8 @@ import ( "github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" pineconeRouter "github.com/matrix-org/pinecone/router" ) @@ -112,7 +113,7 @@ func main() { for { conn, err := listener.Accept() if err != nil { - logrus.WithError(err).Error("listener.Accept failed") + log.Error().Err(err).Msg("listener.Accept failed") continue } @@ -121,7 +122,7 @@ func main() { pineconeRouter.ConnectionPeerType(pineconeRouter.PeerTypeRemote), ) if err != nil { - logrus.WithError(err).Error("pSwitch.Connect failed") + log.Error().Err(err).Msg("pSwitch.Connect failed") continue } diff --git a/cmd/dendrite-demo-pinecone/monolith/monolith.go b/cmd/dendrite-demo-pinecone/monolith/monolith.go index 397473865f..490bd946e2 100644 --- a/cmd/dendrite-demo-pinecone/monolith/monolith.go +++ b/cmd/dendrite-demo-pinecone/monolith/monolith.go @@ -54,6 +54,8 @@ import ( "github.com/matrix-org/gomatrixserverlib/spec" "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" + pineconeConnections "github.com/matrix-org/pinecone/connections" pineconeMulticast "github.com/matrix-org/pinecone/multicast" pineconeRouter "github.com/matrix-org/pinecone/router" @@ -115,6 +117,8 @@ func GenerateDefaultConfig(sk ed25519.PrivateKey, storageDir string, cacheDir st func (p *P2PMonolith) SetupPinecone(sk ed25519.PrivateKey) { p.EventChannel = make(chan pineconeEvents.Event) + // log.Logger.With().Str("pinecone", "router").Logger() + // not compatible with pinecone types.logger => Printf missing p.Router = pineconeRouter.NewRouter(logrus.WithField("pinecone", "router"), sk) p.Router.EnableHopLimiting() p.Router.EnableWakeupBroadcasts() @@ -166,7 +170,7 @@ func (p *P2PMonolith) SetupDendrite( UserAPI: userAPI, } relayAPI := relayapi.NewRelayInternalAPI(cfg, cm, federation, rsAPI, keyRing, producer, enableRelaying, caches) - logrus.Infof("Relaying enabled: %v", relayAPI.RelayingEnabled()) + log.Info().Msgf("Relaying enabled: %v", relayAPI.RelayingEnabled()) p.dendrite = setup.Monolith{ Config: cfg, @@ -206,10 +210,10 @@ func (p *P2PMonolith) StartMonolith() { } func (p *P2PMonolith) Stop() { - logrus.Info("Stopping monolith") + log.Info().Msg("Stopping monolith") p.ProcessCtx.ShutdownDendrite() p.WaitForShutdown() - logrus.Info("Stopped monolith") + log.Info().Msg("Stopped monolith") } func (p *P2PMonolith) WaitForShutdown() { @@ -218,7 +222,7 @@ func (p *P2PMonolith) WaitForShutdown() { } func (p *P2PMonolith) closeAllResources() { - logrus.Info("Closing monolith resources") + log.Info().Msg("Closing monolith resources") p.httpServerMu.Lock() if p.httpServer != nil { _ = p.httpServer.Shutdown(context.Background()) @@ -245,7 +249,7 @@ func (p *P2PMonolith) closeAllResources() { if p.Router != nil { _ = p.Router.Close() } - logrus.Info("Monolith resources closed") + log.Info().Msg("Monolith resources closed") } func (p *P2PMonolith) Addr() string { @@ -268,7 +272,7 @@ func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider, p.httpMux.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) { c, err := wsUpgrader.Upgrade(w, r, nil) if err != nil { - logrus.WithError(err).Error("Failed to upgrade WebSocket connection") + log.Error().Err(err).Msg("Failed to upgrade WebSocket connection") return } conn := conn.WrapWebSocketConn(c) @@ -277,7 +281,7 @@ func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider, pineconeRouter.ConnectionZone("websocket"), pineconeRouter.ConnectionPeerType(pineconeRouter.PeerTypeRemote), ); err != nil { - logrus.WithError(err).Error("Failed to connect WebSocket peer to Pinecone switch") + log.Error().Err(err).Msg("Failed to connect WebSocket peer to Pinecone switch") } }) } @@ -317,34 +321,35 @@ func (p *P2PMonolith) startHTTPServers() { p.httpServerMu.Unlock() pubkey := p.Router.PublicKey() pubkeyString := hex.EncodeToString(pubkey[:]) - logrus.Info("Listening on ", pubkeyString) + log.Info().Msgf("Listening on %s", pubkeyString) switch p.httpServer.Serve(p.Sessions.Protocol(SessionProtocol)) { case net.ErrClosed, http.ErrServerClosed: - logrus.Info("Stopped listening on ", pubkeyString) + log.Info().Msgf("Stopped listening on %s", pubkeyString) default: - logrus.Error("Stopped listening on ", pubkeyString) + log.Error().Msgf("Stopped listening on %s", pubkeyString) } - logrus.Info("Stopped goroutine listening on ", pubkeyString) + log.Info().Msgf("Stopped goroutine listening on %s", pubkeyString) }() p.httpListenAddr = fmt.Sprintf(":%d", p.port) go func() { - logrus.Info("Listening on ", p.httpListenAddr) + log.Info().Msgf("Listening on %s", p.httpListenAddr) switch http.ListenAndServe(p.httpListenAddr, p.httpMux) { case net.ErrClosed, http.ErrServerClosed: - logrus.Info("Stopped listening on ", p.httpListenAddr) + log.Info().Msgf("Stopped listening on %s", p.httpListenAddr) default: - logrus.Error("Stopped listening on ", p.httpListenAddr) + log.Error().Msgf("Stopped listening on %s", p.httpListenAddr) } - logrus.Info("Stopped goroutine listening on ", p.httpListenAddr) + log.Info().Msgf("Stopped goroutine listening on %s", p.httpListenAddr) }() } func (p *P2PMonolith) startEventHandler() { p.stopHandlingEvents = make(chan bool) stopRelayServerSync := make(chan bool) - eLog := logrus.WithField("pinecone", "events") + logger := log.Logger.With().Str("pinecone", "events").Logger() + //eLog := logrus.WithField("pinecone", "events") p.RelayRetriever = relay.NewRelayServerRetriever( context.Background(), spec.ServerName(p.Router.PublicKey().String()), @@ -352,7 +357,7 @@ func (p *P2PMonolith) startEventHandler() { p.dendrite.RelayAPI, stopRelayServerSync, ) - p.RelayRetriever.InitializeRelayServers(eLog) + p.RelayRetriever.InitializeRelayServers(&logger) go func(ch <-chan pineconeEvents.Event) { for { @@ -377,17 +382,17 @@ func (p *P2PMonolith) startEventHandler() { } res := &federationAPI.PerformWakeupServersResponse{} if err := p.dendrite.FederationAPI.PerformWakeupServers(p.ProcessCtx.Context(), req, res); err != nil { - eLog.WithError(err).Error("Failed to wakeup destination", e.PeerID) + log.Error().Err(err).Msgf("Failed to wakeup destination %s", e.PeerID) } } case <-p.stopHandlingEvents: - logrus.Info("Stopping processing pinecone events") + log.Info().Msg("Stopping processing pinecone events") // NOTE: Don't block on channel select { case stopRelayServerSync <- true: default: } - logrus.Info("Stopped processing pinecone events") + log.Info().Msg("Stopped processing pinecone events") return } } diff --git a/cmd/dendrite-demo-pinecone/relay/retriever.go b/cmd/dendrite-demo-pinecone/relay/retriever.go index 3c76ad6007..e941a2db57 100644 --- a/cmd/dendrite-demo-pinecone/relay/retriever.go +++ b/cmd/dendrite-demo-pinecone/relay/retriever.go @@ -22,7 +22,8 @@ import ( federationAPI "github.com/matrix-org/dendrite/federationapi/api" relayServerAPI "github.com/matrix-org/dendrite/relayapi/api" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" + log "github.com/rs/zerolog/log" "go.uber.org/atomic" ) @@ -59,12 +60,12 @@ func NewRelayServerRetriever( } } -func (r *RelayServerRetriever) InitializeRelayServers(eLog *logrus.Entry) { +func (r *RelayServerRetriever) InitializeRelayServers(eLog *zerolog.Logger) { request := federationAPI.P2PQueryRelayServersRequest{Server: spec.ServerName(r.serverName)} response := federationAPI.P2PQueryRelayServersResponse{} err := r.federationAPI.P2PQueryRelayServers(r.ctx, &request, &response) if err != nil { - eLog.Warnf("Failed obtaining list of this node's relay servers: %s", err.Error()) + eLog.Warn().Msgf("Failed obtaining list of this node's relay servers: %s", err.Error()) } r.queriedServersMutex.Lock() @@ -73,7 +74,7 @@ func (r *RelayServerRetriever) InitializeRelayServers(eLog *logrus.Entry) { r.relayServersQueried[server] = false } - eLog.Infof("Registered relay servers: %v", response.RelayServers) + eLog.Info().Msgf("Registered relay servers: %v", response.RelayServers) } func (r *RelayServerRetriever) SetRelayServers(servers []spec.ServerName) { @@ -114,7 +115,7 @@ func (r *RelayServerRetriever) GetQueriedServerStatus() map[spec.ServerName]bool func (r *RelayServerRetriever) StartSync() { if !r.running.Load() { - logrus.Info("Starting relay server sync") + log.Info().Msg("Starting relay server sync") go r.SyncRelayServers(r.quit) } } @@ -140,7 +141,7 @@ func (r *RelayServerRetriever) SyncRelayServers(stop <-chan bool) { }() if len(relayServersToQuery) == 0 { // All relay servers have been synced. - logrus.Info("Finished syncing with all known relays") + log.Info().Msg("Finished syncing with all known relays") return } r.queryRelayServers(relayServersToQuery) @@ -151,7 +152,7 @@ func (r *RelayServerRetriever) SyncRelayServers(stop <-chan bool) { if !t.Stop() { <-t.C } - logrus.Info("Stopped relay server retriever") + log.Info().Msg("Stopped relay server retriever") return case <-t.C: } @@ -159,14 +160,14 @@ func (r *RelayServerRetriever) SyncRelayServers(stop <-chan bool) { } func (r *RelayServerRetriever) queryRelayServers(relayServers []spec.ServerName) { - logrus.Info("Querying relay servers for any available transactions") + log.Info().Msg("Querying relay servers for any available transactions") for _, server := range relayServers { userID, err := spec.NewUserID("@user:"+string(r.serverName), false) if err != nil { return } - logrus.Infof("Syncing with relay: %s", string(server)) + log.Info().Msgf("Syncing with relay: %s", string(server)) err = r.relayAPI.PerformRelayServerSync(context.Background(), *userID, server) if err == nil { func() { @@ -181,7 +182,7 @@ func (r *RelayServerRetriever) queryRelayServers(relayServers []spec.ServerName) // What if you miss this message? // Maybe you should try querying them again after a certain period of time as a backup? } else { - logrus.Errorf("Failed querying relay server: %s", err.Error()) + log.Error().Msgf("Failed querying relay server: %s", err.Error()) } } } @@ -197,7 +198,7 @@ func UpdateNodeRelayServers( response := federationAPI.P2PQueryRelayServersResponse{} err := fedAPI.P2PQueryRelayServers(ctx, &request, &response) if err != nil { - logrus.Warnf("Failed obtaining list of relay servers for %s: %s", node, err.Error()) + log.Warn().Msgf("Failed obtaining list of relay servers for %s: %s", node, err.Error()) } // Remove old, non-matching relays @@ -222,7 +223,7 @@ func UpdateNodeRelayServers( removeResponse := federationAPI.P2PRemoveRelayServersResponse{} err = fedAPI.P2PRemoveRelayServers(ctx, &removeRequest, &removeResponse) if err != nil { - logrus.Warnf("Failed removing old relay servers for %s: %s", node, err.Error()) + log.Warn().Msgf("Failed removing old relay servers for %s: %s", node, err.Error()) } // Add new relays @@ -233,6 +234,6 @@ func UpdateNodeRelayServers( addResponse := federationAPI.P2PAddRelayServersResponse{} err = fedAPI.P2PAddRelayServers(ctx, &addRequest, &addResponse) if err != nil { - logrus.Warnf("Failed adding relay servers for %s: %s", node, err.Error()) + log.Warn().Msgf("Failed adding relay servers for %s: %s", node, err.Error()) } } diff --git a/cmd/dendrite-demo-pinecone/relay/retriever_test.go b/cmd/dendrite-demo-pinecone/relay/retriever_test.go index 6ec9aaf5db..0b01436d30 100644 --- a/cmd/dendrite-demo-pinecone/relay/retriever_test.go +++ b/cmd/dendrite-demo-pinecone/relay/retriever_test.go @@ -22,7 +22,8 @@ import ( federationAPI "github.com/matrix-org/dendrite/federationapi/api" relayServerAPI "github.com/matrix-org/dendrite/relayapi/api" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" "github.com/stretchr/testify/assert" "gotest.tools/v3/poll" ) @@ -63,7 +64,8 @@ func TestRelayRetrieverInitialization(t *testing.T) { make(chan bool), ) - retriever.InitializeRelayServers(logrus.WithField("test", "relay")) + logger := log.Logger.With().Str("test", "relay").Logger() + retriever.InitializeRelayServers(&logger) relayServers := retriever.GetQueriedServerStatus() assert.Equal(t, 2, len(relayServers)) } @@ -77,7 +79,8 @@ func TestRelayRetrieverSync(t *testing.T) { make(chan bool), ) - retriever.InitializeRelayServers(logrus.WithField("test", "relay")) + logger := log.Logger.With().Str("test", "relay").Logger() + retriever.InitializeRelayServers(&logger) relayServers := retriever.GetQueriedServerStatus() assert.Equal(t, 2, len(relayServers)) diff --git a/cmd/dendrite-demo-yggdrasil/main.go b/cmd/dendrite-demo-yggdrasil/main.go index 25c1475cbe..d6eb2ec740 100644 --- a/cmd/dendrite-demo-yggdrasil/main.go +++ b/cmd/dendrite-demo-yggdrasil/main.go @@ -52,7 +52,8 @@ import ( "github.com/matrix-org/dendrite/setup/mscs" "github.com/matrix-org/dendrite/test" "github.com/matrix-org/dendrite/userapi" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" ) var ( @@ -153,29 +154,29 @@ func main() { cfg.Verify(configErrors) if len(*configErrors) > 0 { for _, err := range *configErrors { - logrus.Errorf("Configuration error: %s", err) + log.Error().Msgf("Configuration error: %s", err) } - logrus.Fatalf("Failed to start due to configuration errors") + log.Fatal().Msg("Failed to start due to configuration errors") } internal.SetupStdLogging() internal.SetupHookLogging(cfg.Logging) internal.SetupPprof() - logrus.Infof("Dendrite version %s", internal.VersionString()) + log.Info().Msgf("Dendrite version %s", internal.VersionString()) if !cfg.ClientAPI.RegistrationDisabled && cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled { - logrus.Warn("Open registration is enabled") + log.Warn().Msg("Open registration is enabled") } closer, err := cfg.SetupTracing() if err != nil { - logrus.WithError(err).Panicf("failed to start opentracing") + log.Panic().Err(err).Msgf("failed to start opentracing") } defer closer.Close() // nolint: errcheck if cfg.Global.Sentry.Enabled { - logrus.Info("Setting up Sentry for debugging...") + log.Info().Msg("Setting up Sentry for debugging...") err = sentry.Init(sentry.ClientOptions{ Dsn: cfg.Global.Sentry.DSN, Environment: cfg.Global.Sentry.Environment, @@ -185,7 +186,7 @@ func main() { AttachStacktrace: true, }) if err != nil { - logrus.WithError(err).Panic("failed to start Sentry") + log.Panic().Err(err).Msg("failed to start Sentry") } } @@ -239,7 +240,7 @@ func main() { } monolith.AddAllPublicRoutes(processCtx, cfg, routers, cm, &natsInstance, caches, caching.EnableMetrics) if err := mscs.Enable(cfg, cm, routers, &monolith, caches); err != nil { - logrus.WithError(err).Fatalf("Failed to enable MSCs") + log.Fatal().Err(err).Msgf("Failed to enable MSCs") } httpRouter := mux.NewRouter().SkipClean(true).UseEncodedPath() @@ -267,20 +268,20 @@ func main() { } go func() { - logrus.Info("Listening on ", ygg.DerivedServerName()) - logrus.Fatal(httpServer.Serve(ygg)) + log.Info().Msgf("Listening on %s", ygg.DerivedServerName()) + log.Fatal().Err(httpServer.Serve(ygg)) }() go func() { httpBindAddr := fmt.Sprintf(":%d", *instancePort) - logrus.Info("Listening on ", httpBindAddr) - logrus.Fatal(http.ListenAndServe(httpBindAddr, httpRouter)) + log.Info().Msgf("Listening on %s", httpBindAddr) + log.Fatal().Err(http.ListenAndServe(httpBindAddr, httpRouter)) }() go func() { - logrus.Info("Sending wake-up message to known nodes") + log.Info().Msg("Sending wake-up message to known nodes") req := &api.PerformBroadcastEDURequest{} res := &api.PerformBroadcastEDUResponse{} if err := fsAPI.PerformBroadcastEDU(context.TODO(), req, res); err != nil { - logrus.WithError(err).Error("Failed to send wake-up message to known nodes") + log.Error().Err(err).Msg("Failed to send wake-up message to known nodes") } }() diff --git a/cmd/dendrite-demo-yggdrasil/yggconn/node.go b/cmd/dendrite-demo-yggdrasil/yggconn/node.go index 26c30e4892..71d1d85b23 100644 --- a/cmd/dendrite-demo-yggdrasil/yggconn/node.go +++ b/cmd/dendrite-demo-yggdrasil/yggconn/node.go @@ -25,15 +25,13 @@ import ( "github.com/matrix-org/gomatrixserverlib/spec" "github.com/neilalexander/utp" - "github.com/sirupsen/logrus" ironwoodtypes "github.com/Arceliar/ironwood/types" - "github.com/yggdrasil-network/yggdrasil-go/src/core" yggdrasilcore "github.com/yggdrasil-network/yggdrasil-go/src/core" - "github.com/yggdrasil-network/yggdrasil-go/src/multicast" yggdrasilmulticast "github.com/yggdrasil-network/yggdrasil-go/src/multicast" gologme "github.com/gologme/log" + log "github.com/rs/zerolog/log" ) type Node struct { @@ -56,8 +54,9 @@ func (n *Node) DialerContext(ctx context.Context, _, address string) (net.Conn, } func Setup(sk ed25519.PrivateKey, instanceName, storageDirectory, peerURI, listenURI string) (*Node, error) { + // TODO: gologme v.s. zerolog n := &Node{ - log: gologme.New(logrus.StandardLogger().Writer(), "", 0), + log: gologme.New(log.Logger, "", 0), incoming: make(chan net.Conn), } @@ -78,7 +77,7 @@ func Setup(sk ed25519.PrivateKey, instanceName, storageDirectory, peerURI, liste }) } } - if n.core, err = core.New(sk[:], n.log, options...); err != nil { + if n.core, err = yggdrasilcore.New(sk[:], n.log, options...); err != nil { panic(err) } n.core.SetLogger(n.log) @@ -91,8 +90,8 @@ func Setup(sk ed25519.PrivateKey, instanceName, storageDirectory, peerURI, liste // Setup the multicast module. { var err error - options := []multicast.SetupOption{ - multicast.MulticastInterface{ + options := []yggdrasilmulticast.SetupOption{ + yggdrasilmulticast.MulticastInterface{ Regex: regexp.MustCompile(".*"), Beacon: true, Listen: true, @@ -100,7 +99,7 @@ func Setup(sk ed25519.PrivateKey, instanceName, storageDirectory, peerURI, liste Priority: 0, }, } - if n.multicast, err = multicast.New(n.core, n.log, options...); err != nil { + if n.multicast, err = yggdrasilmulticast.New(n.core, n.log, options...); err != nil { panic(err) } } diff --git a/cmd/dendrite/main.go b/cmd/dendrite/main.go index 66eb88f875..c08d79de35 100644 --- a/cmd/dendrite/main.go +++ b/cmd/dendrite/main.go @@ -19,23 +19,23 @@ import ( "time" "github.com/getsentry/sentry-go" + "github.com/matrix-org/dendrite/appservice" + "github.com/matrix-org/dendrite/federationapi" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/caching" "github.com/matrix-org/dendrite/internal/httputil" "github.com/matrix-org/dendrite/internal/sqlutil" - "github.com/matrix-org/dendrite/setup/jetstream" - "github.com/matrix-org/dendrite/setup/process" - "github.com/matrix-org/gomatrixserverlib/fclient" - "github.com/sirupsen/logrus" - - "github.com/matrix-org/dendrite/appservice" - "github.com/matrix-org/dendrite/federationapi" "github.com/matrix-org/dendrite/roomserver" "github.com/matrix-org/dendrite/setup" basepkg "github.com/matrix-org/dendrite/setup/base" "github.com/matrix-org/dendrite/setup/config" + "github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/mscs" + "github.com/matrix-org/dendrite/setup/process" "github.com/matrix-org/dendrite/userapi" + "github.com/matrix-org/gomatrixserverlib/fclient" + + log "github.com/rs/zerolog/log" ) var ( @@ -58,18 +58,18 @@ func main() { if *unixSocket == "" { http, err := config.HTTPAddress("http://" + *httpBindAddr) if err != nil { - logrus.WithError(err).Fatalf("Failed to parse http address") + log.Fatal().Err(err).Msg("Failed to parse http address") } httpAddr = http https, err := config.HTTPAddress("https://" + *httpsBindAddr) if err != nil { - logrus.WithError(err).Fatalf("Failed to parse https address") + log.Fatal().Err(err).Msg("Failed to parse https address") } httpsAddr = https } else { socket, err := config.UnixSocketAddress(*unixSocket, *unixSocketPermission) if err != nil { - logrus.WithError(err).Fatalf("Failed to parse unix socket") + log.Fatal().Err(err).Msg("Failed to parse unix socket") } httpAddr = socket } @@ -78,9 +78,9 @@ func main() { cfg.Verify(configErrors) if len(*configErrors) > 0 { for _, err := range *configErrors { - logrus.Errorf("Configuration error: %s", err) + log.Error().Msgf("Configuration error: %s", err) } - logrus.Fatalf("Failed to start due to configuration errors") + log.Fatal().Msgf("Failed to start due to configuration errors") } processCtx := process.NewProcessContext() @@ -90,9 +90,9 @@ func main() { basepkg.PlatformSanityChecks() - logrus.Infof("Dendrite version %s", internal.VersionString()) + log.Info().Msgf("Dendrite version %s", internal.VersionString()) if !cfg.ClientAPI.RegistrationDisabled && cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled { - logrus.Warn("Open registration is enabled") + log.Warn().Msg("Open registration is enabled") } // create DNS cache @@ -102,7 +102,7 @@ func main() { cfg.Global.DNSCache.CacheSize, cfg.Global.DNSCache.CacheLifetime, ) - logrus.Infof( + log.Info().Msgf( "DNS cache enabled (size %d, lifetime %s)", cfg.Global.DNSCache.CacheSize, cfg.Global.DNSCache.CacheLifetime, @@ -112,13 +112,13 @@ func main() { // setup tracing closer, err := cfg.SetupTracing() if err != nil { - logrus.WithError(err).Panicf("failed to start opentracing") + log.Panic().Err(err).Msg("failed to start opentracing") } defer closer.Close() // nolint: errcheck // setup sentry if cfg.Global.Sentry.Enabled { - logrus.Info("Setting up Sentry for debugging...") + log.Info().Msg("Setting up Sentry for debugging...") err = sentry.Init(sentry.ClientOptions{ Dsn: cfg.Global.Sentry.DSN, Environment: cfg.Global.Sentry.Environment, @@ -128,13 +128,13 @@ func main() { AttachStacktrace: true, }) if err != nil { - logrus.WithError(err).Panic("failed to start Sentry") + log.Panic().Err(err).Msg("failed to start Sentry") } go func() { processCtx.ComponentStarted() <-processCtx.WaitForShutdown() if !sentry.Flush(time.Second * 5) { - logrus.Warnf("failed to flush all Sentry events!") + log.Warn().Msg("failed to flush all Sentry events!") } processCtx.ComponentFinished() }() @@ -183,7 +183,7 @@ func main() { if len(cfg.MSCs.MSCs) > 0 { if err := mscs.Enable(cfg, cm, routers, &monolith, caches); err != nil { - logrus.WithError(err).Fatalf("Failed to enable MSCs") + log.Fatal().Err(err).Msg("Failed to enable MSCs") } } diff --git a/cmd/generate-config/main.go b/cmd/generate-config/main.go index cb57ed78fb..9f94eb6819 100644 --- a/cmd/generate-config/main.go +++ b/cmd/generate-config/main.go @@ -57,7 +57,7 @@ func main() { cfg.MediaAPI.BasePath = config.Path(filepath.Join(*dirPath, "media")) cfg.Global.JetStream.StoragePath = config.Path(*dirPath) cfg.SyncAPI.Fulltext.IndexPath = config.Path(filepath.Join(*dirPath, "searchindex")) - cfg.Logging = []config.LogrusHook{ + cfg.Logging = []config.LogHook{ { Type: "file", Level: "info", diff --git a/cmd/generate-keys/main.go b/cmd/generate-keys/main.go index d4c8cf78ae..a814f29ab5 100644 --- a/cmd/generate-keys/main.go +++ b/cmd/generate-keys/main.go @@ -17,7 +17,6 @@ package main import ( "flag" "fmt" - "log" "os" "github.com/matrix-org/dendrite/test" @@ -56,7 +55,7 @@ func main() { if *tlsCertFile != "" || *tlsKeyFile != "" { if *tlsCertFile == "" || *tlsKeyFile == "" { - log.Fatal("Zero or both of --tls-key and --tls-cert must be supplied") + panic("Zero or both of --tls-key and --tls-cert must be supplied") } if *authorityCertFile == "" && *authorityKeyFile == "" { if err := test.NewTLSKey(*tlsKeyFile, *tlsCertFile, *keySize); err != nil { diff --git a/cmd/resolve-state/main.go b/cmd/resolve-state/main.go index 1278b1cc80..77db2ae360 100644 --- a/cmd/resolve-state/main.go +++ b/cmd/resolve-state/main.go @@ -36,7 +36,7 @@ var difference = flag.Bool("difference", false, "whether to calculate the differ func main() { ctx := context.Background() cfg := setup.ParseFlags(true) - cfg.Logging = append(cfg.Logging[:0], config.LogrusHook{ + cfg.Logging = append(cfg.Logging[:0], config.LogHook{ Type: "std", Level: "error", }) diff --git a/federationapi/consumers/keychange.go b/federationapi/consumers/keychange.go index 3fdc835bb1..21e45ee129 100644 --- a/federationapi/consumers/keychange.go +++ b/federationapi/consumers/keychange.go @@ -22,7 +22,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/nats-io/nats.go" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/federationapi/queue" "github.com/matrix-org/dendrite/federationapi/storage" @@ -82,7 +82,7 @@ func (t *KeyChangeConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) boo var m api.DeviceMessage if err := json.Unmarshal(msg.Data, &m); err != nil { sentry.CaptureException(err) - logrus.WithError(err).Errorf("failed to read device message from key change topic") + log.Error().Err(err).Msg("failed to read device message from key change topic") return true } if m.DeviceKeys == nil && m.OutputCrossSigningKeyUpdate == nil { @@ -104,13 +104,13 @@ func (t *KeyChangeConsumer) onDeviceKeyMessage(m api.DeviceMessage) bool { if m.DeviceKeys == nil { return true } - logger := logrus.WithField("user_id", m.UserID) + logger := log.With().Str("user_id", m.UserID).Logger() // only send key change events which originated from us _, originServerName, err := gomatrixserverlib.SplitID('@', m.UserID) if err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("Failed to extract domain from key change event") + logger.Error().Err(err).Msg("Failed to extract domain from key change event") return true } if !t.isLocalServerName(originServerName) { @@ -124,7 +124,7 @@ func (t *KeyChangeConsumer) onDeviceKeyMessage(m api.DeviceMessage) bool { }, &queryRes) if err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("failed to calculate joined rooms for user") + logger.Error().Err(err).Msg("failed to calculate joined rooms for user") return true } @@ -132,7 +132,7 @@ func (t *KeyChangeConsumer) onDeviceKeyMessage(m api.DeviceMessage) bool { destinations, err := t.db.GetJoinedHostsForRooms(t.ctx, queryRes.RoomIDs, true, true) if err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("failed to calculate joined hosts for rooms user is in") + logger.Error().Err(err).Msg("failed to calculate joined hosts for rooms user is in") return true } @@ -155,11 +155,11 @@ func (t *KeyChangeConsumer) onDeviceKeyMessage(m api.DeviceMessage) bool { } if edu.Content, err = json.Marshal(event); err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("failed to marshal EDU JSON") + logger.Error().Err(err).Msg("failed to marshal EDU JSON") return true } - logger.Debugf("Sending device list update message to %q", destinations) + logger.Debug().Msgf("Sending device list update message to %q", destinations) err = t.queues.SendEDU(edu, originServerName, destinations) return err == nil } @@ -169,7 +169,7 @@ func (t *KeyChangeConsumer) onCrossSigningMessage(m api.DeviceMessage) bool { _, host, err := gomatrixserverlib.SplitID('@', output.UserID) if err != nil { sentry.CaptureException(err) - logrus.WithError(err).Errorf("fedsender key change consumer: user ID parse failure") + log.Error().Err(err).Msgf("fedsender key change consumer: user ID parse failure") return true } if !t.isLocalServerName(host) { @@ -177,7 +177,7 @@ func (t *KeyChangeConsumer) onCrossSigningMessage(m api.DeviceMessage) bool { // end up parroting information we received from other servers. return true } - logger := logrus.WithField("user_id", output.UserID) + logger := log.With().Str("user_id", m.UserID).Logger() var queryRes roomserverAPI.QueryRoomsForUserResponse err = t.rsAPI.QueryRoomsForUser(t.ctx, &roomserverAPI.QueryRoomsForUserRequest{ @@ -186,14 +186,14 @@ func (t *KeyChangeConsumer) onCrossSigningMessage(m api.DeviceMessage) bool { }, &queryRes) if err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("fedsender key change consumer: failed to calculate joined rooms for user") + logger.Error().Err(err).Msg("fedsender key change consumer: failed to calculate joined rooms for user") return true } // send this key change to all servers who share rooms with this user. destinations, err := t.db.GetJoinedHostsForRooms(t.ctx, queryRes.RoomIDs, true, true) if err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("fedsender key change consumer: failed to calculate joined hosts for rooms user is in") + logger.Error().Err(err).Msg("fedsender key change consumer: failed to calculate joined hosts for rooms user is in") return true } @@ -208,11 +208,11 @@ func (t *KeyChangeConsumer) onCrossSigningMessage(m api.DeviceMessage) bool { } if edu.Content, err = json.Marshal(output); err != nil { sentry.CaptureException(err) - logger.WithError(err).Error("fedsender key change consumer: failed to marshal output, dropping") + logger.Error().Err(err).Msg("fedsender key change consumer: failed to marshal output, dropping") return true } - logger.Debugf("Sending cross-signing update message to %q", destinations) + logger.Debug().Msgf("Sending cross-signing update message to %q", destinations) err = t.queues.SendEDU(edu, host, destinations) return err == nil } diff --git a/federationapi/consumers/presence.go b/federationapi/consumers/presence.go index e751b65d4b..001b842abf 100644 --- a/federationapi/consumers/presence.go +++ b/federationapi/consumers/presence.go @@ -30,7 +30,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/nats-io/nats.go" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) // OutputReceiptConsumer consumes events that originate in the clientapi. @@ -87,7 +87,7 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msgs []*nats.Msg userID := msg.Header.Get(jetstream.UserID) _, serverName, err := gomatrixserverlib.SplitID('@', userID) if err != nil { - log.WithError(err).WithField("user_id", userID).Error("failed to extract domain from receipt sender") + log.Error().Err(err).Str("user_id", userID).Msg("failed to extract domain from receipt sender") return true } if !t.isLocalServerName(serverName) { @@ -100,7 +100,7 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msgs []*nats.Msg WantMembership: "join", }, &queryRes) if err != nil { - log.WithError(err).Error("failed to calculate joined rooms for user") + log.Error().Err(err).Msg("failed to calculate joined rooms for user") return true } @@ -114,7 +114,7 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msgs []*nats.Msg // send this presence to all servers who share rooms with this user. joined, err := t.db.GetJoinedHostsForRooms(t.ctx, queryRes.RoomIDs, true, true) if err != nil { - log.WithError(err).Error("failed to get joined hosts") + log.Error().Err(err).Msg("failed to get joined hosts") return true } @@ -147,13 +147,13 @@ func (t *OutputPresenceConsumer) onMessage(ctx context.Context, msgs []*nats.Msg Origin: string(serverName), } if edu.Content, err = json.Marshal(content); err != nil { - log.WithError(err).Error("failed to marshal EDU JSON") + log.Error().Err(err).Msg("failed to marshal EDU JSON") return true } - log.Tracef("sending presence EDU to %d servers", len(joined)) + log.Trace().Msgf("sending presence EDU to %d servers", len(joined)) if err = t.queues.SendEDU(edu, serverName, joined); err != nil { - log.WithError(err).Error("failed to send EDU") + log.Error().Err(err).Msg("failed to send EDU") return false } diff --git a/federationapi/consumers/receipts.go b/federationapi/consumers/receipts.go index 1407a88b77..3c784c2232 100644 --- a/federationapi/consumers/receipts.go +++ b/federationapi/consumers/receipts.go @@ -30,7 +30,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/nats-io/nats.go" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) // OutputReceiptConsumer consumes events that originate in the clientapi. @@ -93,7 +93,7 @@ func (t *OutputReceiptConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) // only send receipt events which originated from us _, receiptServerName, err := gomatrixserverlib.SplitID('@', receipt.UserID) if err != nil { - log.WithError(err).WithField("user_id", receipt.UserID).Error("failed to extract domain from receipt sender") + log.Error().Err(err).Str("user_id", receipt.UserID).Msg("failed to extract domain from receipt sender") return true } if !t.isLocalServerName(receiptServerName) { @@ -103,7 +103,7 @@ func (t *OutputReceiptConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) timestamp, err := strconv.ParseUint(msg.Header.Get("timestamp"), 10, 64) if err != nil { // If the message was invalid, log it and move on to the next message in the stream - log.WithError(err).Errorf("EDU output log: message parse failure") + log.Error().Err(err).Msgf("EDU output log: message parse failure") sentry.CaptureException(err) return true } @@ -112,7 +112,7 @@ func (t *OutputReceiptConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) joined, err := t.db.GetJoinedHosts(ctx, receipt.RoomID) if err != nil { - log.WithError(err).WithField("room_id", receipt.RoomID).Error("failed to get joined hosts for room") + log.Error().Err(err).Str("room_id", receipt.RoomID).Msg("failed to get joined hosts for room") return false } @@ -138,12 +138,12 @@ func (t *OutputReceiptConsumer) onMessage(ctx context.Context, msgs []*nats.Msg) Origin: string(receiptServerName), } if edu.Content, err = json.Marshal(content); err != nil { - log.WithError(err).Error("failed to marshal EDU JSON") + log.Error().Err(err).Msg("failed to marshal EDU JSON") return true } if err := t.queues.SendEDU(edu, receiptServerName, names); err != nil { - log.WithError(err).Error("failed to send EDU") + log.Error().Err(err).Msg("failed to send EDU") return false } diff --git a/federationapi/consumers/roomserver.go b/federationapi/consumers/roomserver.go index c6ad3f7485..c7380a77e1 100644 --- a/federationapi/consumers/roomserver.go +++ b/federationapi/consumers/roomserver.go @@ -26,8 +26,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/nats-io/nats.go" - "github.com/sirupsen/logrus" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/federationapi/queue" "github.com/matrix-org/dendrite/federationapi/storage" @@ -103,7 +102,7 @@ func (s *OutputRoomEventConsumer) onMessage(ctx context.Context, msgs []*nats.Ms var output api.OutputEvent if err := json.Unmarshal(msg.Data, &output); err != nil { // If the message was invalid, log it and move on to the next message in the stream - log.WithError(err).Errorf("roomserver output log: message parse failure") + log.Error().Err(err).Msg("roomserver output log: message parse failure") return true } @@ -112,36 +111,31 @@ func (s *OutputRoomEventConsumer) onMessage(ctx context.Context, msgs []*nats.Ms ev := output.NewRoomEvent.Event if err := s.processMessage(*output.NewRoomEvent, output.NewRoomEvent.RewritesState); err != nil { // panic rather than continue with an inconsistent database - log.WithFields(log.Fields{ - "event_id": ev.EventID(), - "event": string(ev.JSON()), - "add": output.NewRoomEvent.AddsStateEventIDs, - "del": output.NewRoomEvent.RemovesStateEventIDs, - log.ErrorKey: err, - }).Panicf("roomserver output log: write room event failure") + log.Panic(). + Str("event_id", ev.EventID()). + Str("event", string(ev.JSON())). + Any("add", output.NewRoomEvent.AddsStateEventIDs). + Any("del", output.NewRoomEvent.RemovesStateEventIDs). + Err(err).Msg("roomserver output log: write room event failure") } case api.OutputTypeNewInboundPeek: if err := s.processInboundPeek(*output.NewInboundPeek); err != nil { - log.WithFields(log.Fields{ - "event": output.NewInboundPeek, - log.ErrorKey: err, - }).Panicf("roomserver output log: remote peek event failure") + //log.ErrorKey == ? + log.Panic().Any("event", output.NewInboundPeek).Err(err).Msg("roomserver output log: remote peek event failure") return false } case api.OutputTypePurgeRoom: - log.WithField("room_id", output.PurgeRoom.RoomID).Warn("Purging room from federation API") + log.Warn().Str("room_id", output.PurgeRoom.RoomID).Msg("Purging room from federation API") if err := s.db.PurgeRoom(ctx, output.PurgeRoom.RoomID); err != nil { - logrus.WithField("room_id", output.PurgeRoom.RoomID).WithError(err).Error("Failed to purge room from federation API") + log.Error().Str("room_id", output.PurgeRoom.RoomID).Err(err).Msg("Failed to purge room from federation API") } else { - logrus.WithField("room_id", output.PurgeRoom.RoomID).Warn("Room purged from federation API") + log.Warn().Str("room_id", output.PurgeRoom.RoomID).Msg("Room purged from federation API") } default: - log.WithField("type", output.Type).Debug( - "roomserver output log: ignoring unknown output type", - ) + log.Debug().Any("type", output.Type).Msg("roomserver output log: ignoring unknown output type") } return true @@ -263,7 +257,7 @@ func (s *OutputRoomEventConsumer) sendPresence(roomID string, addedJoined []type RoomID: roomID, }, &queryRes) if err != nil { - log.WithError(err).Error("failed to calculate joined rooms for user") + log.Error().Err(err).Msg("failed to calculate joined rooms for user") return } @@ -276,7 +270,7 @@ func (s *OutputRoomEventConsumer) sendPresence(roomID string, addedJoined []type var presence *nats.Msg presence, err = s.natsClient.RequestMsg(msg, time.Second*10) if err != nil { - log.WithError(err).Errorf("unable to get presence") + log.Error().Err(err).Msg("unable to get presence") continue } @@ -311,11 +305,11 @@ func (s *OutputRoomEventConsumer) sendPresence(roomID string, addedJoined []type Origin: string(s.cfg.Matrix.ServerName), } if edu.Content, err = json.Marshal(content); err != nil { - log.WithError(err).Error("failed to marshal EDU JSON") + log.Error().Err(err).Msg("failed to marshal EDU JSON") return } if err := s.queues.SendEDU(edu, s.cfg.Matrix.ServerName, joined); err != nil { - log.WithError(err).Error("failed to send EDU") + log.Error().Err(err).Msg("failed to send EDU") } } diff --git a/federationapi/federationapi.go b/federationapi/federationapi.go index ee15a8a6e0..cb13692623 100644 --- a/federationapi/federationapi.go +++ b/federationapi/federationapi.go @@ -17,13 +17,6 @@ package federationapi import ( "time" - "github.com/matrix-org/dendrite/internal/httputil" - "github.com/matrix-org/dendrite/internal/sqlutil" - "github.com/matrix-org/dendrite/setup/config" - "github.com/matrix-org/dendrite/setup/process" - "github.com/matrix-org/gomatrixserverlib/fclient" - "github.com/sirupsen/logrus" - "github.com/matrix-org/dendrite/federationapi/api" federationAPI "github.com/matrix-org/dendrite/federationapi/api" "github.com/matrix-org/dendrite/federationapi/consumers" @@ -33,9 +26,15 @@ import ( "github.com/matrix-org/dendrite/federationapi/statistics" "github.com/matrix-org/dendrite/federationapi/storage" "github.com/matrix-org/dendrite/internal/caching" + "github.com/matrix-org/dendrite/internal/httputil" + "github.com/matrix-org/dendrite/internal/sqlutil" roomserverAPI "github.com/matrix-org/dendrite/roomserver/api" + "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" + "github.com/matrix-org/dendrite/setup/process" userapi "github.com/matrix-org/dendrite/userapi/api" + "github.com/matrix-org/gomatrixserverlib/fclient" + log "github.com/rs/zerolog/log" "github.com/matrix-org/gomatrixserverlib" @@ -107,7 +106,7 @@ func NewInternalAPI( federationDB, err := storage.NewDatabase(processContext.Context(), cm, &cfg.Database, caches, dendriteCfg.Global.IsLocalServerName) if err != nil { - logrus.WithError(err).Panic("failed to connect to federation sender db") + log.Panic().Err(err).Msg("failed to connect to federation sender db") } if resetBlacklist { @@ -135,45 +134,45 @@ func NewInternalAPI( federationDB, rsAPI, ) if err = rsConsumer.Start(); err != nil { - logrus.WithError(err).Panic("failed to start room server consumer") + log.Panic().Err(err).Msg("failed to start room server consumer") } tsConsumer := consumers.NewOutputSendToDeviceConsumer( processContext, cfg, js, queues, federationDB, ) if err = tsConsumer.Start(); err != nil { - logrus.WithError(err).Panic("failed to start send-to-device consumer") + log.Panic().Err(err).Msg("failed to start send-to-device consumer") } receiptConsumer := consumers.NewOutputReceiptConsumer( processContext, cfg, js, queues, federationDB, ) if err = receiptConsumer.Start(); err != nil { - logrus.WithError(err).Panic("failed to start receipt consumer") + log.Panic().Err(err).Msg("failed to start receipt consumer") } typingConsumer := consumers.NewOutputTypingConsumer( processContext, cfg, js, queues, federationDB, ) if err = typingConsumer.Start(); err != nil { - logrus.WithError(err).Panic("failed to start typing consumer") + log.Panic().Err(err).Msg("failed to start typing consumer") } keyConsumer := consumers.NewKeyChangeConsumer( processContext, &dendriteCfg.KeyServer, js, queues, federationDB, rsAPI, ) if err = keyConsumer.Start(); err != nil { - logrus.WithError(err).Panic("failed to start key server consumer") + log.Panic().Err(err).Msg("failed to start key server consumer") } presenceConsumer := consumers.NewOutputPresenceConsumer( processContext, cfg, js, queues, federationDB, rsAPI, ) if err = presenceConsumer.Start(); err != nil { - logrus.WithError(err).Panic("failed to start presence consumer") + log.Panic().Err(err).Msg("failed to start presence consumer") } var cleanExpiredEDUs func() cleanExpiredEDUs = func() { - logrus.Infof("Cleaning expired EDUs") + log.Info().Msg("Cleaning expired EDUs") if err := federationDB.DeleteExpiredEDUs(processContext.Context()); err != nil { - logrus.WithError(err).Error("Failed to clean expired EDUs") + log.Error().Err(err).Msg("Failed to clean expired EDUs") } time.AfterFunc(time.Hour, cleanExpiredEDUs) } diff --git a/internal/log.go b/internal/log.go index 8fe98f20c9..1832b2d7db 100644 --- a/internal/log.go +++ b/internal/log.go @@ -28,8 +28,8 @@ import ( "github.com/matrix-org/util" - "github.com/matrix-org/dugong" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/setup/config" ) @@ -37,34 +37,34 @@ import ( // logrus is using a global variable when we're using `logrus.AddHook` // this unfortunately results in us adding the same hook multiple times. // This map ensures we only ever add one level hook. -var stdLevelLogAdded = make(map[logrus.Level]bool) +var stdLevelLogAdded = make(map[zerolog.Level]bool) var levelLogAddedMu = &sync.Mutex{} type utcFormatter struct { - logrus.Formatter + zerolog.Formatter } -func (f utcFormatter) Format(entry *logrus.Entry) ([]byte, error) { - entry.Time = entry.Time.UTC() - return f.Formatter.Format(entry) +func (f utcFormatter) Format(entry *zerolog.Event) ([]byte, error) { + return f.Format(entry) } // Logrus hook which wraps another hook and filters log entries according to their level. // (Note that we cannot use solely logrus.SetLevel, because Dendrite supports multiple // levels of logging at the same time.) type logLevelHook struct { - level logrus.Level - logrus.Hook + level zerolog.Level + zerolog.Hook } // Levels returns all the levels supported by this hook. -func (h *logLevelHook) Levels() []logrus.Level { - levels := make([]logrus.Level, 0) - - for _, level := range logrus.AllLevels { +func (h *logLevelHook) Levels() []zerolog.Level { + levels := make([]zerolog.Level, 0) + var level zerolog.Level = -1 + for level <= 5 { if level <= h.level { levels = append(levels, level) } + level = level + 1 } return levels @@ -92,9 +92,9 @@ func callerPrettyfier(f *runtime.Frame) (string, string) { // simplest, and it gives us the freedom to run pprof on a separate port. func SetupPprof() { if hostPort := os.Getenv("PPROFLISTEN"); hostPort != "" { - logrus.Warn("Starting pprof on ", hostPort) + log.Warn().Msgf("Starting pprof on %s", hostPort) go func() { - logrus.WithError(http.ListenAndServe(hostPort, nil)).Error("Failed to setup pprof listener") + log.Error().Err(http.ListenAndServe(hostPort, nil)).Msg("Failed to setup pprof listener") }() } } @@ -103,9 +103,14 @@ func SetupPprof() { func SetupStdLogging() { levelLogAddedMu.Lock() defer levelLogAddedMu.Unlock() - logrus.SetReportCaller(true) - logrus.SetFormatter(&utcFormatter{ - &logrus.TextFormatter{ + //zerolog.SetReportCaller(true) + //TimeFieldFormat => dont need + //DisableColors => dont need + //DisableTimestamp => dont need + //Quo0teEmptyFields => dont need + //callprettyfier => what? + /*zerolog.Formatter(&utcFormatter{ + &zerolog.TextFormatter{ TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", FullTimestamp: true, DisableColors: false, @@ -113,46 +118,39 @@ func SetupStdLogging() { QuoteEmptyFields: true, CallerPrettyfier: callerPrettyfier, }, - }) + })*/ } // File type hooks should be provided a path to a directory to store log files func checkFileHookParams(params map[string]interface{}) { path, ok := params["path"] if !ok { - logrus.Fatalf("Expecting a parameter \"path\" for logging hook of type \"file\"") + log.Fatal().Msg("Expecting a parameter \"path\" for logging hook of type \"file\"") } if _, ok := path.(string); !ok { - logrus.Fatalf("Parameter \"path\" for logging hook of type \"file\" should be a string") + log.Fatal().Msg("Parameter \"path\" for logging hook of type \"file\" should be a string") } } // Add a new FSHook to the logger. Each component will log in its own file -func setupFileHook(hook config.LogrusHook, level logrus.Level) { +func setupFileHook(hook config.LogHook, level zerolog.Level) { dirPath := (hook.Params["path"]).(string) fullPath := filepath.Join(dirPath, "dendrite.log") if err := os.MkdirAll(path.Dir(fullPath), os.ModePerm); err != nil { - logrus.Fatalf("Couldn't create directory %s: %q", path.Dir(fullPath), err) + log.Fatal().Msgf("Couldn't create directory %s: %q", path.Dir(fullPath), err) } - - logrus.AddHook(&logLevelHook{ - level, - dugong.NewFSHook( - fullPath, - &utcFormatter{ - &logrus.TextFormatter{ - TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00", - DisableColors: true, - DisableTimestamp: false, - DisableSorting: false, - QuoteEmptyFields: true, - }, - }, - &dugong.DailyRotationSchedule{GZip: true}, - ), - }) + /* + log.Hook(&logLevelHook{ + level, + dugong.NewFSHook( + fullPath, + &utcFormatter{log.Logger}, + &dugong.DailyRotationSchedule{GZip: true}, + ), + })*/ + /// TODO: Dugong FSHook! } // CloseAndLogIfError Closes io.Closer and logs the error if any diff --git a/internal/log_unix.go b/internal/log_unix.go index 3f15063d18..07d012c253 100644 --- a/internal/log_unix.go +++ b/internal/log_unix.go @@ -19,11 +19,10 @@ package internal import ( "io" - "log/syslog" + "os" - "github.com/MFAshby/stdemuxerhook" - "github.com/sirupsen/logrus" - lSyslog "github.com/sirupsen/logrus/hooks/syslog" + "github.com/rs/zerolog" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/setup/config" ) @@ -31,20 +30,20 @@ import ( // SetupHookLogging configures the logging hooks defined in the configuration. // If something fails here it means that the logging was improperly configured, // so we just exit with the error -func SetupHookLogging(hooks []config.LogrusHook) { +func SetupHookLogging(hooks []config.LogHook) { levelLogAddedMu.Lock() defer levelLogAddedMu.Unlock() for _, hook := range hooks { // Check we received a proper logging level - level, err := logrus.ParseLevel(hook.Level) + level, err := zerolog.ParseLevel(hook.Level) if err != nil { - logrus.Fatalf("Unrecognised logging level %s: %q", hook.Level, err) + log.Fatal().Msgf("Unrecognised logging level %s: %q", hook.Level, err) } // Perform a first filter on the logs according to the lowest level of all // (Eg: If we have hook for info and above, prevent logrus from processing debug logs) - if logrus.GetLevel() < level { - logrus.SetLevel(level) + if log.Logger.GetLevel() < level { + log.Logger = log.Logger.Level(level) } switch hook.Type { @@ -57,46 +56,50 @@ func SetupHookLogging(hooks []config.LogrusHook) { case "std": setupStdLogHook(level) default: - logrus.Fatalf("Unrecognised logging hook type: %s", hook.Type) + log.Fatal().Msgf("Unrecognised logging hook type: %s", hook.Type) } } - setupStdLogHook(logrus.InfoLevel) + setupStdLogHook(zerolog.InfoLevel) // Hooks are now configured for stdout/err, so throw away the default logger output - logrus.SetOutput(io.Discard) + log.Logger = log.Logger.Output(io.Discard) } func checkSyslogHookParams(params map[string]interface{}) { addr, ok := params["address"] if !ok { - logrus.Fatalf("Expecting a parameter \"address\" for logging hook of type \"syslog\"") + log.Fatal().Msg("Expecting a parameter \"address\" for logging hook of type \"syslog\"") } if _, ok := addr.(string); !ok { - logrus.Fatalf("Parameter \"address\" for logging hook of type \"syslog\" should be a string") + log.Fatal().Msg("Parameter \"address\" for logging hook of type \"syslog\" should be a string") } proto, ok2 := params["protocol"] if !ok2 { - logrus.Fatalf("Expecting a parameter \"protocol\" for logging hook of type \"syslog\"") + log.Fatal().Msg("Expecting a parameter \"protocol\" for logging hook of type \"syslog\"") } if _, ok2 := proto.(string); !ok2 { - logrus.Fatalf("Parameter \"protocol\" for logging hook of type \"syslog\" should be a string") + log.Fatal().Msg("Parameter \"protocol\" for logging hook of type \"syslog\" should be a string") } } -func setupStdLogHook(level logrus.Level) { +func setupStdLogHook(level zerolog.Level) { if stdLevelLogAdded[level] { return } - logrus.AddHook(&logLevelHook{level, stdemuxerhook.New(logrus.StandardLogger())}) + //log.Logger.Hook(&logLevelHook{level, stdemuxerhook.New(log.Logger)}) + //log.Logger.Hook(&logLevelHook{level, zerolog.New(os.Stdout)}) + log.Logger = zerolog.New(os.Stdout) stdLevelLogAdded[level] = true } -func setupSyslogHook(hook config.LogrusHook, level logrus.Level) { - syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, "dendrite") +func setupSyslogHook(hook config.LogHook, level zerolog.Level) { + // rewrite to: + // https://stackoverflow.com/questions/73064915/logging-to-syslog-file-with-zerolog-golang + /*syslogHook, err := lSyslog.NewSyslogHook(hook.Params["protocol"].(string), hook.Params["address"].(string), syslog.LOG_INFO, "dendrite") if err == nil { - logrus.AddHook(&logLevelHook{level, syslogHook}) - } + log.Logger.Hook(&logLevelHook{level, syslogHook}) + }*/ } diff --git a/setup/config/config.go b/setup/config/config.go index 41396ae361..1882add685 100644 --- a/setup/config/config.go +++ b/setup/config/config.go @@ -27,14 +27,18 @@ import ( "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" + "golang.org/x/crypto/ed25519" "gopkg.in/yaml.v2" + "github.com/uber/jaeger-client-go" jaegerconfig "github.com/uber/jaeger-client-go/config" jaegermetrics "github.com/uber/jaeger-lib/metrics" ) +var logger = zerolog.New(os.Stdout).With().Timestamp().Logger() + // keyIDRegexp defines allowable characters in Key IDs. var keyIDRegexp = regexp.MustCompile("^ed25519:[a-zA-Z0-9_]+$") @@ -74,8 +78,8 @@ type Dendrite struct { Jaeger jaegerconfig.Configuration `yaml:"jaeger"` } `yaml:"tracing"` - // The config for logging informations. Each hook will be added to logrus. - Logging []LogrusHook `yaml:"logging"` + // The config for logging informations. Each hook will be added. + Logging []LogHook `yaml:"logging"` // Any information derived from the configuration options for later use. Derived Derived `yaml:"-"` @@ -146,10 +150,10 @@ type ThumbnailSize struct { ResizeMethod string `yaml:"method,omitempty"` } -// LogrusHook represents a single logrus hook. At this point, only parsing and +// LogHook represents a single log hook. At this point, only parsing and // verification of the proper values for type and level are done. -// Validity/integrity checks on the parameters are done when configuring logrus. -type LogrusHook struct { +// Validity/integrity checks on the parameters are done when configuring log. +type LogHook struct { // The type of hook, currently only "file" is supported. Type string `yaml:"type"` @@ -398,9 +402,9 @@ func checkPositive(configErrs *ConfigErrors, key string, value int64) { // checkLogging verifies the parameters logging.* are valid. func (config *Dendrite) checkLogging(configErrs *ConfigErrors) { - for _, logrusHook := range config.Logging { - checkNotEmpty(configErrs, "logging.type", string(logrusHook.Type)) - checkNotEmpty(configErrs, "logging.level", string(logrusHook.Level)) + for _, logHook := range config.Logging { + checkNotEmpty(configErrs, "logging.type", string(logHook.Type)) + checkNotEmpty(configErrs, "logging.level", string(logHook.Level)) } } @@ -480,20 +484,31 @@ func (config *Dendrite) SetupTracing() (closer io.Closer, err error) { } return config.Tracing.Jaeger.InitGlobalTracer( "Dendrite", - jaegerconfig.Logger(logrusLogger{logrus.StandardLogger()}), + jaegerconfig.Logger(NewJaegerLogger(&logger)), jaegerconfig.Metrics(jaegermetrics.NullFactory), ) } -// logrusLogger is a small wrapper that implements jaeger.Logger using logrus. -type logrusLogger struct { - l *logrus.Logger +type jaegerLogger struct { + logger *zerolog.Logger +} + +func NewJaegerLogger(logger *zerolog.Logger) jaeger.Logger { + return &jaegerLogger{logger} +} + +type Logger interface { + // Error logs a message at error priority + Error(msg string) + + // Infof logs a message at info priority + Infof(msg string, args ...interface{}) } -func (l logrusLogger) Error(msg string) { - l.l.Error(msg) +func (jl *jaegerLogger) Error(msg string) { + jl.logger.Error().Msg(msg) } -func (l logrusLogger) Infof(msg string, args ...interface{}) { - l.l.Infof(msg, args...) +func (jl *jaegerLogger) Infof(msg string, args ...interface{}) { + jl.logger.Info().Msgf(msg, args...) } diff --git a/setup/flags.go b/setup/flags.go index 869caa280b..cb42d9e8eb 100644 --- a/setup/flags.go +++ b/setup/flags.go @@ -21,7 +21,7 @@ import ( "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/setup/config" - "github.com/sirupsen/logrus" + "github.com/rs/zerolog" ) var ( @@ -30,6 +30,8 @@ var ( enableRegistrationWithoutVerification = flag.Bool("really-enable-open-registration", false, "This allows open registration without secondary verification (reCAPTCHA). This is NOT RECOMMENDED and will SIGNIFICANTLY increase the risk that your server will be used to send spam or conduct attacks, which may result in your server being banned from rooms.") ) +var logger = zerolog.New(os.Stdout).With().Timestamp().Logger() + // ParseFlags parses the commandline flags and uses them to create a config. func ParseFlags(monolith bool) *config.Dendrite { flag.Parse() @@ -40,13 +42,13 @@ func ParseFlags(monolith bool) *config.Dendrite { } if *configPath == "" { - logrus.Fatal("--config must be supplied") + logger.Fatal().Msg("--config must be supplied") } cfg, err := config.Load(*configPath) if err != nil { - logrus.Fatalf("Invalid config file: %s", err) + logger.Fatal().Msgf("Invalid config file: %s", err) } if *enableRegistrationWithoutVerification { diff --git a/userapi/consumers/signingkeyupdate.go b/userapi/consumers/signingkeyupdate.go index 457a61838b..c0bc640b85 100644 --- a/userapi/consumers/signingkeyupdate.go +++ b/userapi/consumers/signingkeyupdate.go @@ -22,7 +22,7 @@ import ( "github.com/matrix-org/gomatrixserverlib/fclient" "github.com/matrix-org/gomatrixserverlib/spec" "github.com/nats-io/nats.go" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/jetstream" @@ -73,18 +73,18 @@ func (t *SigningKeyUpdateConsumer) onMessage(ctx context.Context, msgs []*nats.M msg := msgs[0] // Guaranteed to exist if onMessage is called var updatePayload api.CrossSigningKeyUpdate if err := json.Unmarshal(msg.Data, &updatePayload); err != nil { - logrus.WithError(err).Errorf("Failed to read from signing key update input topic") + log.Error().Err(err).Msg("Failed to read from signing key update input topic") return true } origin := spec.ServerName(msg.Header.Get("origin")) if _, serverName, err := gomatrixserverlib.SplitID('@', updatePayload.UserID); err != nil { - logrus.WithError(err).Error("failed to split user id") + log.Error().Err(err).Msgf("failed to split user id") return true } else if t.isLocalServerName(serverName) { - logrus.Warn("dropping device key update from ourself") + log.Warn().Msg("dropping device key update from ourself") return true } else if serverName != origin { - logrus.Warnf("dropping device key update, %s != %s", serverName, origin) + log.Warn().Msgf("dropping device key update, %s != %s", serverName, origin) return true } @@ -101,11 +101,11 @@ func (t *SigningKeyUpdateConsumer) onMessage(ctx context.Context, msgs []*nats.M } uploadRes := &api.PerformUploadDeviceKeysResponse{} if err := t.userAPI.PerformUploadDeviceKeys(ctx, uploadReq, uploadRes); err != nil { - logrus.WithError(err).Error("failed to upload device keys") + log.Error().Err(err).Msg("failed to upload device keys") return false } if uploadRes.Error != nil { - logrus.WithError(uploadRes.Error).Error("failed to upload device keys") + log.Error().Err(uploadRes.Error).Msg("failed to upload device keys") return true } diff --git a/userapi/internal/cross_signing.go b/userapi/internal/cross_signing.go index ea7b84f6be..93077007fd 100644 --- a/userapi/internal/cross_signing.go +++ b/userapi/internal/cross_signing.go @@ -27,7 +27,8 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib/fclient" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" + "golang.org/x/crypto/curve25519" ) @@ -469,7 +470,7 @@ func (a *UserInternalAPI) crossSigningKeysFromDatabase( for targetUserID := range req.UserToDevices { keys, err := a.KeyDatabase.CrossSigningKeysForUser(ctx, targetUserID) if err != nil { - logrus.WithError(err).Errorf("Failed to get cross-signing keys for user %q", targetUserID) + log.Error().Err(err).Msgf("Failed to get cross-signing keys for user %q", targetUserID) continue } @@ -482,7 +483,7 @@ func (a *UserInternalAPI) crossSigningKeysFromDatabase( sigMap, err := a.KeyDatabase.CrossSigningSigsForTarget(ctx, req.UserID, targetUserID, keyID) if err != nil && err != sql.ErrNoRows { - logrus.WithError(err).Errorf("Failed to get cross-signing signatures for user %q key %q", targetUserID, keyID) + log.Error().Err(err).Msgf("Failed to get cross-signing signatures for user %q key %q", targetUserID, keyID) continue } diff --git a/userapi/producers/keychange.go b/userapi/producers/keychange.go index da6cea31a7..70e9be6166 100644 --- a/userapi/producers/keychange.go +++ b/userapi/producers/keychange.go @@ -22,7 +22,8 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage" "github.com/nats-io/nats.go" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" ) // KeyChange produces key change events for the sync API and federation sender to consume @@ -61,10 +62,7 @@ func (p *KeyChange) ProduceKeyChanges(keys []api.DeviceMessage) error { userToDeviceCount[key.UserID]++ } for userID, count := range userToDeviceCount { - logrus.WithFields(logrus.Fields{ - "user_id": userID, - "num_key_changes": count, - }).Tracef("Produced to key change topic '%s'", p.Topic) + log.Trace().Str("user_id", userID).Int("num_key_changes", count).Msgf("Produced to key change topic '%s'", p.Topic) } return nil } @@ -100,8 +98,6 @@ func (p *KeyChange) ProduceSigningKeyUpdate(key api.CrossSigningKeyUpdate) error return err } - logrus.WithFields(logrus.Fields{ - "user_id": key.UserID, - }).Tracef("Produced to cross-signing update topic '%s'", p.Topic) + log.Trace().Str("user_id", key.UserID).Msgf("Produced to cross-signing update topic '%s'", p.Topic) return nil } diff --git a/userapi/producers/syncapi.go b/userapi/producers/syncapi.go index 165de89940..b7e4d2e374 100644 --- a/userapi/producers/syncapi.go +++ b/userapi/producers/syncapi.go @@ -6,7 +6,7 @@ import ( "github.com/matrix-org/gomatrixserverlib" "github.com/nats-io/nats.go" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal/eventutil" "github.com/matrix-org/dendrite/setup/jetstream" @@ -48,11 +48,7 @@ func (p *SyncAPI) SendAccountData(userID string, data eventutil.AccountData) err return err } - log.WithFields(log.Fields{ - "user_id": userID, - "room_id": data.RoomID, - "data_type": data.Type, - }).Tracef("Producing to topic '%s'", p.clientDataTopic) + log.Trace().Str("user_id", userID).Str("room_id", data.RoomID).Str("data_type", data.Type).Msgf("Producing to topic '%s'", p.clientDataTopic) _, err = p.producer.PublishMsg(m) return err @@ -92,10 +88,7 @@ func (p *SyncAPI) sendNotificationData(userID string, data *eventutil.Notificati return err } - log.WithFields(log.Fields{ - "user_id": userID, - "room_id": data.RoomID, - }).Tracef("Producing to topic '%s'", p.clientDataTopic) + log.Trace().Str("user_id", userID).Str("room_id", data.RoomID).Msgf("Producing to topic '%s'", p.clientDataTopic) _, err = p.producer.PublishMsg(m) return err diff --git a/userapi/storage/postgres/accounts_table.go b/userapi/storage/postgres/accounts_table.go index 5b38c5f4b6..f2bed98fef 100644 --- a/userapi/storage/postgres/accounts_table.go +++ b/userapi/storage/postgres/accounts_table.go @@ -27,7 +27,7 @@ import ( "github.com/matrix-org/dendrite/userapi/storage/tables" "github.com/matrix-org/gomatrixserverlib/spec" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) const accountsSchema = ` @@ -178,7 +178,7 @@ func (s *accountsStatements) SelectAccountByLocalpart( err := stmt.QueryRowContext(ctx, localpart, serverName).Scan(&acc.Localpart, &acc.ServerName, &appserviceIDPtr, &acc.AccountType) if err != nil { if err != sql.ErrNoRows { - log.WithError(err).Error("Unable to retrieve user from the db") + log.Error().Err(err).Msg("Unable to retrieve user from the db") } return nil, err } diff --git a/userapi/storage/postgres/notifications_table.go b/userapi/storage/postgres/notifications_table.go index acb9e42bcf..ca1062c390 100644 --- a/userapi/storage/postgres/notifications_table.go +++ b/userapi/storage/postgres/notifications_table.go @@ -20,7 +20,7 @@ import ( "encoding/json" "time" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -137,7 +137,7 @@ func (s *notificationsStatements) DeleteUpTo(ctx context.Context, txn *sql.Tx, l if err != nil { return true, err } - log.WithFields(log.Fields{"localpart": localpart, "room_id": roomID, "stream_pos": pos}).Tracef("DeleteUpTo: %d rows affected", nrows) + log.Trace().Str("localpart", localpart).Str("room_id", roomID).Uint64("stream_pos", pos).Msgf("DeleteUpTo: %d rows affected", nrows) return nrows > 0, nil } @@ -151,7 +151,7 @@ func (s *notificationsStatements) UpdateRead(ctx context.Context, txn *sql.Tx, l if err != nil { return true, err } - log.WithFields(log.Fields{"localpart": localpart, "room_id": roomID, "stream_pos": pos}).Tracef("UpdateRead: %d rows affected", nrows) + log.Trace().Str("localpart", localpart).Str("room_id", roomID).Uint64("stream_pos", pos).Msgf("UpdateRead: %d rows affected", nrows) return nrows > 0, nil } diff --git a/userapi/storage/postgres/openid_table.go b/userapi/storage/postgres/openid_table.go index 345877d11c..490bac6b59 100644 --- a/userapi/storage/postgres/openid_table.go +++ b/userapi/storage/postgres/openid_table.go @@ -9,7 +9,7 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage/tables" "github.com/matrix-org/gomatrixserverlib/spec" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) const openIDTokenSchema = ` @@ -80,7 +80,7 @@ func (s *openIDTokenStatements) SelectOpenIDTokenAtrributes( openIDTokenAttrs.UserID = fmt.Sprintf("@%s:%s", localpart, serverName) if err != nil { if err != sql.ErrNoRows { - log.WithError(err).Error("Unable to retrieve token from the db") + log.Error().Err(err).Msg("Unable to retrieve token from the db") } return nil, err } diff --git a/userapi/storage/postgres/pusher_table.go b/userapi/storage/postgres/pusher_table.go index 2e88aa8e98..8ca37d259c 100644 --- a/userapi/storage/postgres/pusher_table.go +++ b/userapi/storage/postgres/pusher_table.go @@ -19,7 +19,7 @@ import ( "database/sql" "encoding/json" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -140,7 +140,7 @@ func (s *pushersStatements) SelectPushers( pushers = append(pushers, pusher) } - logrus.Tracef("Database returned %d pushers", len(pushers)) + log.Trace().Msgf("Database returned %d pushers", len(pushers)) return pushers, rows.Err() } diff --git a/userapi/storage/postgres/stats_table.go b/userapi/storage/postgres/stats_table.go index a7949e4bab..a34e092dd9 100644 --- a/userapi/storage/postgres/stats_table.go +++ b/userapi/storage/postgres/stats_table.go @@ -21,7 +21,7 @@ import ( "github.com/lib/pq" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -235,9 +235,9 @@ func NewPostgresStatsTable(db *sql.DB, serverName spec.ServerName) (tables.Stats func (s *statsStatements) startTimers() { var updateStatsFunc func() updateStatsFunc = func() { - logrus.Infof("Executing UpdateUserDailyVisits") + log.Info().Msg("Executing UpdateUserDailyVisits") if err := s.UpdateUserDailyVisits(context.Background(), nil, time.Now(), s.lastUpdate); err != nil { - logrus.WithError(err).Error("failed to update daily user visits") + log.Error().Err(err).Msg("failed to update daily user visits") } time.AfterFunc(time.Hour*3, updateStatsFunc) } diff --git a/userapi/storage/sqlite3/accounts_table.go b/userapi/storage/sqlite3/accounts_table.go index d01915a764..58117a44bb 100644 --- a/userapi/storage/sqlite3/accounts_table.go +++ b/userapi/storage/sqlite3/accounts_table.go @@ -26,7 +26,7 @@ import ( "github.com/matrix-org/dendrite/userapi/storage/tables" "github.com/matrix-org/gomatrixserverlib/spec" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) const accountsSchema = ` @@ -178,7 +178,7 @@ func (s *accountsStatements) SelectAccountByLocalpart( err := stmt.QueryRowContext(ctx, localpart, serverName).Scan(&acc.Localpart, &acc.ServerName, &appserviceIDPtr, &acc.AccountType) if err != nil { if err != sql.ErrNoRows { - log.WithError(err).Error("Unable to retrieve user from the db") + log.Error().Err(err).Msg("Unable to retrieve user from the db") } return nil, err } diff --git a/userapi/storage/sqlite3/deltas/2022110411000000_server_names.go b/userapi/storage/sqlite3/deltas/2022110411000000_server_names.go index 76f39a9086..b949c3cf7f 100644 --- a/userapi/storage/sqlite3/deltas/2022110411000000_server_names.go +++ b/userapi/storage/sqlite3/deltas/2022110411000000_server_names.go @@ -8,7 +8,8 @@ import ( "github.com/lib/pq" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" ) var serverNamesTables = []string{ @@ -57,7 +58,7 @@ func UpServerNames(ctx context.Context, tx *sql.Tx, serverName spec.ServerName) pq.QuoteIdentifier(table), ) if err := tx.QueryRowContext(ctx, q).Scan(&c); err != nil || c == 1 { - logrus.Infof("Table %s already has column, skipping", table) + log.Info().Msgf("Table %s already has column, skipping", table) continue } if c == 0 { diff --git a/userapi/storage/sqlite3/notifications_table.go b/userapi/storage/sqlite3/notifications_table.go index 94d6c7294c..d26f8b2b81 100644 --- a/userapi/storage/sqlite3/notifications_table.go +++ b/userapi/storage/sqlite3/notifications_table.go @@ -20,7 +20,7 @@ import ( "encoding/json" "time" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -137,7 +137,7 @@ func (s *notificationsStatements) DeleteUpTo(ctx context.Context, txn *sql.Tx, l if err != nil { return true, err } - log.WithFields(log.Fields{"localpart": localpart, "room_id": roomID, "stream_pos": pos}).Tracef("DeleteUpTo: %d rows affected", nrows) + log.Trace().Str("localpart", localpart).Str("room_id", roomID).Uint64("stream_pos", pos).Msgf("DeleteUpTo: %d rows affected", nrows) return nrows > 0, nil } @@ -151,7 +151,7 @@ func (s *notificationsStatements) UpdateRead(ctx context.Context, txn *sql.Tx, l if err != nil { return true, err } - log.WithFields(log.Fields{"localpart": localpart, "room_id": roomID, "stream_pos": pos}).Tracef("UpdateRead: %d rows affected", nrows) + log.Trace().Str("localpart", localpart).Str("room_id", roomID).Uint64("stream_pos", pos).Msgf("UpdateRead: %d rows affected", nrows) return nrows > 0, nil } diff --git a/userapi/storage/sqlite3/openid_table.go b/userapi/storage/sqlite3/openid_table.go index def0074d2b..f998d025d2 100644 --- a/userapi/storage/sqlite3/openid_table.go +++ b/userapi/storage/sqlite3/openid_table.go @@ -9,7 +9,7 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage/tables" "github.com/matrix-org/gomatrixserverlib/spec" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) const openIDTokenSchema = ` @@ -82,7 +82,7 @@ func (s *openIDTokenStatements) SelectOpenIDTokenAtrributes( openIDTokenAttrs.UserID = fmt.Sprintf("@%s:%s", localpart, serverName) if err != nil { if err != sql.ErrNoRows { - log.WithError(err).Error("Unable to retrieve token from the db") + log.Error().Err(err).Msg("Unable to retrieve token from the db") } return nil, err } diff --git a/userapi/storage/sqlite3/pusher_table.go b/userapi/storage/sqlite3/pusher_table.go index e09f9c78fc..6d243c370d 100644 --- a/userapi/storage/sqlite3/pusher_table.go +++ b/userapi/storage/sqlite3/pusher_table.go @@ -19,7 +19,7 @@ import ( "database/sql" "encoding/json" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -140,7 +140,7 @@ func (s *pushersStatements) SelectPushers( pushers = append(pushers, pusher) } - logrus.Tracef("Database returned %d pushers", len(pushers)) + log.Trace().Msgf("Database returned %d pushers", len(pushers)) return pushers, rows.Err() } diff --git a/userapi/storage/sqlite3/stats_table.go b/userapi/storage/sqlite3/stats_table.go index 71d80d4d41..a15667fe88 100644 --- a/userapi/storage/sqlite3/stats_table.go +++ b/userapi/storage/sqlite3/stats_table.go @@ -21,7 +21,8 @@ import ( "time" "github.com/matrix-org/gomatrixserverlib/spec" - "github.com/sirupsen/logrus" + + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -241,9 +242,9 @@ func NewSQLiteStatsTable(db *sql.DB, serverName spec.ServerName) (tables.StatsTa func (s *statsStatements) startTimers() { var updateStatsFunc func() updateStatsFunc = func() { - logrus.Infof("Executing UpdateUserDailyVisits") + log.Info().Msg("Executing UpdateUserDailyVisits") if err := s.UpdateUserDailyVisits(context.Background(), nil, time.Now(), s.lastUpdate); err != nil { - logrus.WithError(err).Error("failed to update daily user visits") + log.Error().Err(err).Msg("failed to update daily user visits") } time.AfterFunc(time.Hour*3, updateStatsFunc) } diff --git a/userapi/util/devices.go b/userapi/util/devices.go index 117da08ea9..ccb2b318c4 100644 --- a/userapi/util/devices.go +++ b/userapi/util/devices.go @@ -8,7 +8,7 @@ import ( "github.com/matrix-org/dendrite/userapi/api" "github.com/matrix-org/dendrite/userapi/storage" "github.com/matrix-org/gomatrixserverlib/spec" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) type PusherDevice struct { @@ -40,30 +40,20 @@ func GetPushDevices(ctx context.Context, localpart string, serverName spec.Serve var ok bool format, ok = fmtIface.(string) if ok && format != "event_id_only" { - log.WithFields(log.Fields{ - "localpart": localpart, - "app_id": pusher.AppID, - }).Errorf("Only data.format event_id_only or empty is supported") + log.Error().Str("localpart", localpart).Str("app_id", pusher.AppID).Msg("Only data.format event_id_only or empty is supported") continue } urlIface := pusher.Data["url"] url, ok = urlIface.(string) if !ok { - log.WithFields(log.Fields{ - "localpart": localpart, - "app_id": pusher.AppID, - }).Errorf("No data.url configured for HTTP Pusher") + log.Error().Str("localpart", localpart).Str("app_id", pusher.AppID).Msg("No data.url configured for HTTP Pusher") continue } data = mapWithout(data, "url") default: - log.WithFields(log.Fields{ - "localpart": localpart, - "app_id": pusher.AppID, - "kind": pusher.Kind, - }).Errorf("Unhandled pusher kind") + log.Error().Str("localpart", localpart).Str("app_id", pusher.AppID).Any("kind", pusher.Kind).Msg("Unhandled pusher kind") continue } diff --git a/userapi/util/notify.go b/userapi/util/notify.go index 45d37525c1..fa081d5d42 100644 --- a/userapi/util/notify.go +++ b/userapi/util/notify.go @@ -9,7 +9,7 @@ import ( "github.com/matrix-org/dendrite/userapi/storage" "github.com/matrix-org/dendrite/userapi/storage/tables" "github.com/matrix-org/gomatrixserverlib/spec" - log "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) // NotifyUserCountsAsync sends notifications to a local user's @@ -32,11 +32,7 @@ func NotifyUserCountsAsync(ctx context.Context, pgClient pushgateway.Client, loc return err } - log.WithFields(log.Fields{ - "localpart": localpart, - "app_id0": pusherDevices[0].Device.AppID, - "pushkey": pusherDevices[0].Device.PushKey, - }).Tracef("Notifying HTTP push gateway about notification counts") + log.Trace().Str("localpart", localpart).Str("app_id0", pusherDevices[0].Device.AppID).Str("pushkey", pusherDevices[0].Device.PushKey).Msgf("Notifying HTTP push gateway about notification counts") // TODO: think about bounding this to one per user, and what // ordering guarantees we must provide. @@ -63,11 +59,7 @@ func NotifyUserCountsAsync(ctx context.Context, pgClient pushgateway.Client, loc }, } if err := pgClient.Notify(ctx, pusherDevice.URL, &req, &pushgateway.NotifyResponse{}); err != nil { - log.WithFields(log.Fields{ - "localpart": localpart, - "app_id0": pusherDevice.Device.AppID, - "pushkey": pusherDevice.Device.PushKey, - }).WithError(err).Error("HTTP push gateway request failed") + log.Error().Err(err).Str("localpart", localpart).Str("app_id0", pusherDevice.Device.AppID).Str("pushkey", pusherDevice.Device.PushKey).Msg("HTTP push gateway request failed") return } } diff --git a/userapi/util/phonehomestats.go b/userapi/util/phonehomestats.go index 4bf9a5d886..d1ac49bd78 100644 --- a/userapi/util/phonehomestats.go +++ b/userapi/util/phonehomestats.go @@ -24,7 +24,7 @@ import ( "syscall" "time" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" "github.com/matrix-org/dendrite/internal" "github.com/matrix-org/dendrite/setup/config" @@ -92,7 +92,7 @@ func (p *phoneHomeStats) collect() { // cpu and memory usage information err := getMemoryStats(p) if err != nil { - logrus.WithError(err).Warn("unable to get memory/cpu stats, using defaults") + log.Warn().Err(err).Msg("unable to get memory/cpu stats, using defaults") } // configuration information @@ -113,7 +113,7 @@ func (p *phoneHomeStats) collect() { messageStats, activeRooms, activeE2EERooms, err := p.db.DailyRoomsMessages(ctx, p.serverName) if err != nil { - logrus.WithError(err).Warn("unable to query message stats, using default values") + log.Warn().Err(err).Msg("unable to query message stats, using default values") } p.stats["daily_messages"] = messageStats.Messages p.stats["daily_sent_messages"] = messageStats.SentMessages @@ -125,7 +125,7 @@ func (p *phoneHomeStats) collect() { // user stats and DB engine userStats, db, err := p.db.UserStatistics(ctx) if err != nil { - logrus.WithError(err).Warn("unable to query userstats, using default values") + log.Warn().Err(err).Msg("unable to query userstats, using default values") } p.stats["database_engine"] = db.Engine p.stats["database_server_version"] = db.Version @@ -145,22 +145,22 @@ func (p *phoneHomeStats) collect() { output := bytes.Buffer{} if err = json.NewEncoder(&output).Encode(p.stats); err != nil { - logrus.WithError(err).Error("Unable to encode phone-home statistics") + log.Error().Err(err).Msg("Unable to encode phone-home statistics") return } - logrus.Infof("Reporting stats to %s: %s", p.cfg.Global.ReportStats.Endpoint, output.String()) + log.Info().Msgf("Reporting stats to %s: %s", p.cfg.Global.ReportStats.Endpoint, output.String()) request, err := http.NewRequestWithContext(ctx, http.MethodPost, p.cfg.Global.ReportStats.Endpoint, &output) if err != nil { - logrus.WithError(err).Error("Unable to create phone-home statistics request") + log.Error().Err(err).Msg("Unable to create phone-home statistics request") return } request.Header.Set("User-Agent", "Dendrite/"+internal.VersionString()) _, err = p.client.Do(request) if err != nil { - logrus.WithError(err).Error("Unable to send phone-home statistics") + log.Error().Err(err).Msg("Unable to send phone-home statistics") return } } diff --git a/userapi/util/stats.go b/userapi/util/stats.go index 22ef12aad8..38a13b435f 100644 --- a/userapi/util/stats.go +++ b/userapi/util/stats.go @@ -21,14 +21,14 @@ import ( "syscall" "time" - "github.com/sirupsen/logrus" + log "github.com/rs/zerolog/log" ) func getMemoryStats(p *phoneHomeStats) error { oldUsage := p.prevData newUsage := syscall.Rusage{} if err := syscall.Getrusage(syscall.RUSAGE_SELF, &newUsage); err != nil { - logrus.WithError(err).Error("unable to get usage") + log.Error().Err(err).Msg("unable to get usage") return err } newData := timestampToRUUsage{timestamp: time.Now().Unix(), usage: newUsage}