Skip to content
Snippets Groups Projects
Commit 01ef43b6 authored by ensiouel's avatar ensiouel
Browse files

для items добавлен middleware для отправки логов в log сервис

parent 250b7e3f
No related tags found
No related merge requests found
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"reflect" "reflect"
"strings"
"time" "time"
"git.perx.ru/perxis/perxis-go/pkg/data" "git.perx.ru/perxis/perxis-go/pkg/data"
...@@ -135,6 +136,16 @@ func (i *Item) Clone() *Item { ...@@ -135,6 +136,16 @@ func (i *Item) Clone() *Item {
return &itm return &itm
} }
func (i Item) ObjectID(kv ...string) string {
path := []string{
"spaces", i.SpaceID,
"envs", i.EnvID,
"items", i.ID,
}
path = append(path, kv...)
return strings.Join(path, "/")
}
func (i *Item) ToMap() map[string]interface{} { func (i *Item) ToMap() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"id": i.ID, "id": i.ID,
......
package middleware
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/schema"
"go.uber.org/zap"
)
type logService struct {
logger *zap.Logger
next items.Items
}
func LogServiceMiddleware(logger *zap.Logger) Middleware {
logger = logger.With(
zap.String("component", "Items.Service"),
)
return func(next items.Items) items.Items {
return &logService{
logger: logger,
next: next,
}
}
}
func (m *logService) Create(ctx context.Context, item *items.Item, opts ...*items.CreateOptions) (created *items.Item, err error) {
level := zap.InfoLevel
created, err = m.next.Create(ctx, item, opts...)
if err != nil {
level = zap.WarnLevel
}
if created != nil {
item = created
}
m.logger.Log(level, "create item",
zap.String("event", "Items.Create"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)), // TODO Решить, как заполнять это поле
)
return
}
func (m *logService) Introspect(ctx context.Context, item *items.Item, opts ...*items.IntrospectOptions) (itm *items.Item, sch *schema.Schema, err error) {
return m.next.Introspect(ctx, item, opts...)
}
func (m *logService) Get(ctx context.Context, spaceId, envId, collectionId, itemId string, options ...*items.GetOptions) (item *items.Item, err error) {
return m.next.Get(ctx, spaceId, envId, collectionId, itemId, options...)
}
func (m *logService) Find(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.FindOptions) (items []*items.Item, total int, err error) {
return m.next.Find(ctx, spaceId, envId, collectionId, filter, options...)
}
func (m *logService) Update(ctx context.Context, item *items.Item, options ...*items.UpdateOptions) (err error) {
level := zap.InfoLevel
err = m.next.Update(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "update item",
zap.String("event", "Items.Update"),
zap.String("object", item.ObjectID("revs", item.RevisionID)),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) Delete(ctx context.Context, item *items.Item, options ...*items.DeleteOptions) (err error) {
level := zap.InfoLevel
err = m.next.Delete(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "delete item",
zap.String("event", "Items.Delete"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) Undelete(ctx context.Context, item *items.Item, options ...*items.UndeleteOptions) (err error) {
level := zap.InfoLevel
err = m.next.Undelete(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "undelete item",
zap.String("event", "Items.Undelete"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) Publish(ctx context.Context, item *items.Item, options ...*items.PublishOptions) (err error) {
level := zap.InfoLevel
err = m.next.Publish(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "publish item",
zap.String("event", "Items.Publish"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) Unpublish(ctx context.Context, item *items.Item, options ...*items.UnpublishOptions) (err error) {
level := zap.InfoLevel
err = m.next.Unpublish(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "unpublish item",
zap.String("event", "Items.Unpublish"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) GetPublished(ctx context.Context, spaceId, envId, collectionId, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) {
return m.next.GetPublished(ctx, spaceId, envId, collectionId, itemId, options...)
}
func (m *logService) FindPublished(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.FindPublishedOptions) (items []*items.Item, total int, err error) {
return m.next.FindPublished(ctx, spaceId, envId, collectionId, filter, options...)
}
func (m *logService) GetRevision(ctx context.Context, spaceId, envId, collectionId, itemId, revisionId string, options ...*items.GetRevisionOptions) (item *items.Item, err error) {
return m.next.GetRevision(ctx, spaceId, envId, collectionId, itemId, revisionId, options...)
}
func (m *logService) ListRevisions(ctx context.Context, spaceId, envId, collectionId, itemId string, options ...*items.ListRevisionsOptions) (items []*items.Item, err error) {
return m.next.ListRevisions(ctx, spaceId, envId, collectionId, itemId, options...)
}
func (m *logService) Archive(ctx context.Context, item *items.Item, options ...*items.ArchiveOptions) (err error) {
level := zap.InfoLevel
err = m.next.Archive(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "archive item",
zap.String("event", "Items.Archive"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) FindArchived(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.FindArchivedOptions) (items []*items.Item, total int, err error) {
return m.next.FindArchived(ctx, spaceId, envId, collectionId, filter, options...)
}
func (m *logService) Unarchive(ctx context.Context, item *items.Item, options ...*items.UnarchiveOptions) (err error) {
level := zap.InfoLevel
err = m.next.Unarchive(ctx, item, options...)
if err != nil {
level = zap.WarnLevel
}
m.logger.Log(level, "unarchive item",
zap.String("event", "Items.Unarchive"),
zap.String("object", item.ObjectID()),
zap.String("caller", ctx.Value("caller").(string)),
)
return
}
func (m *logService) Aggregate(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.AggregateOptions) (result map[string]interface{}, err error) {
return m.next.Aggregate(ctx, spaceId, envId, collectionId, filter, options...)
}
func (m *logService) AggregatePublished(ctx context.Context, spaceId, envId, collectionId string, filter *items.Filter, options ...*items.AggregatePublishedOptions) (result map[string]interface{}, err error) {
return m.next.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment