Select Git revision
access_logging_middleware.go
logging_middleware.go 9.39 KiB
package middleware
import (
"context"
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/schema"
logzap "git.perx.ru/perxis/perxis-go/zap"
"go.uber.org/zap"
)
type loggingMiddleware struct {
logger *zap.Logger
next items.Items
}
func LoggingMiddleware(logger *zap.Logger) Middleware {
return func(next items.Items) items.Items {
return &loggingMiddleware{
next: next,
logger: logger.With(logzap.Component("items")),
}
}
}
func (m *loggingMiddleware) Aggregate(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregateOptions) (result map[string]interface{}, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
)
result, err = m.next.Aggregate(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
logger.Error("Failed to aggregate", zap.Error(err))
return
}
return result, err
}
func (m *loggingMiddleware) AggregatePublished(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
)
result, err = m.next.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
logger.Error("Failed to aggregate_published", zap.Error(err))
return
}
return result, err
}
func (m *loggingMiddleware) Archive(ctx context.Context, item *items.Item, options ...*items.ArchiveOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemArchive),
logzap.Object(item),
)
err = m.next.Archive(ctx, item, options...)
if err != nil {
logger.Error("Failed to archive", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully archived", logzap.Channels(logzap.Userlog))
return err
}
func (m *loggingMiddleware) Create(ctx context.Context, item *items.Item, opts ...*items.CreateOptions) (created *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemCreate),
)
created, err = m.next.Create(ctx, item, opts...)
if err != nil {
logger.Error("Failed to create", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog), logzap.Object(item))
return
}
logger.Info("Successfully created", logzap.Channels(logzap.Userlog), logzap.Object(created))
return created, err
}
func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, options ...*items.DeleteOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemDelete),
logzap.Object(item),
)
err = m.next.Delete(ctx, item, options...)
if err != nil {
logger.Error("Failed to delete", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully deleted", logzap.Channels(logzap.Userlog))
return err
}
func (m *loggingMiddleware) Find(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindOptions) (items []*items.Item, total int, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
)
items, total, err = m.next.Find(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
logger.Error("Failed to find", zap.Error(err))
return
}
return items, total, err
}
func (m *loggingMiddleware) FindArchived(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindArchivedOptions) (items []*items.Item, total int, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
)
items, total, err = m.next.FindArchived(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
logger.Error("Failed to find_archived", zap.Error(err))
return
}
return items, total, err
}
func (m *loggingMiddleware) FindPublished(ctx context.Context, spaceId string, envId string, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
)
items, total, err = m.next.FindPublished(ctx, spaceId, envId, collectionId, filter, options...)
if err != nil {
logger.Error("Failed to findpublished", zap.Error(err))
return
}
return items, total, err
}
func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetOptions) (item *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
item, err = m.next.Get(ctx, spaceId, envId, collectionId, itemId, options...)
if err != nil {
logger.Error("Failed to get", zap.Error(err))
return
}
return item, err
}
func (m *loggingMiddleware) GetPublished(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
item, err = m.next.GetPublished(ctx, spaceId, envId, collectionId, itemId, options...)
if err != nil {
logger.Error("Failed to get published", zap.Error(err))
return
}
return item, err
}
func (m *loggingMiddleware) GetRevision(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, revisionId string, options ...*items.GetRevisionOptions) (item *items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
item, err = m.next.GetRevision(ctx, spaceId, envId, collectionId, itemId, revisionId, options...)
if err != nil {
logger.Error("Failed to get revision", zap.Error(err))
return
}
return item, err
}
func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, opts ...*items.IntrospectOptions) (itm *items.Item, sch *schema.Schema, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Object(item),
)
itm, sch, err = m.next.Introspect(ctx, item, opts...)
if err != nil {
logger.Error("Failed to introspect", zap.Error(err))
return
}
return itm, sch, err
}
func (m *loggingMiddleware) ListRevisions(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.ListRevisionsOptions) (items []*items.Item, err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
)
items, err = m.next.ListRevisions(ctx, spaceId, envId, collectionId, itemId, options...)
if err != nil {
logger.Error("Failed to list revisions", zap.Error(err))
return
}
return items, err
}
func (m *loggingMiddleware) Publish(ctx context.Context, item *items.Item, options ...*items.PublishOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemPublish),
logzap.Object(item),
)
err = m.next.Publish(ctx, item, options...)
if err != nil {
logger.Error("Failed to publish", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully published", logzap.Channels(logzap.Userlog))
return err
}
func (m *loggingMiddleware) Unarchive(ctx context.Context, item *items.Item, options ...*items.UnarchiveOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemUnarchive),
logzap.Object(item),
)
err = m.next.Unarchive(ctx, item, options...)
if err != nil {
logger.Error("Failed to unarchive", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully unarchived", logzap.Channels(logzap.Userlog))
return err
}
func (m *loggingMiddleware) Undelete(ctx context.Context, item *items.Item, options ...*items.UndeleteOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemUndelete),
logzap.Object(item),
)
err = m.next.Undelete(ctx, item, options...)
if err != nil {
logger.Error("Failed to undelete", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully undeleted", logzap.Channels(logzap.Userlog))
return err
}
func (m *loggingMiddleware) Unpublish(ctx context.Context, item *items.Item, options ...*items.UnpublishOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemUnpublish),
logzap.Object(item),
)
err = m.next.Unpublish(ctx, item, options...)
if err != nil {
logger.Error("Failed to unpublish", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully unpublished", logzap.Channels(logzap.Userlog))
return err
}
func (m *loggingMiddleware) Update(ctx context.Context, item *items.Item, options ...*items.UpdateOptions) (err error) {
logger := m.logger.With(
logzap.CallerFromContext(ctx),
logzap.Event(items.EventItemUpdate),
logzap.Object(item),
)
err = m.next.Update(ctx, item, options...)
if err != nil {
logger.Error("Failed to update", zap.Error(err), logzap.Channels(logzap.Userlog, logzap.Syslog))
return
}
logger.Info("Successfully updated", logzap.Channels(logzap.Userlog))
return err
}