Skip to content
Snippets Groups Projects
Commit 505d6f38 authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

Реализован logzap.Caller(...Option)

parents 271ed4ec 0f9c94b9
No related branches found
No related tags found
No related merge requests found
...@@ -35,7 +35,7 @@ func TestEntryEncoder_EncodeEntry(t *testing.T) { ...@@ -35,7 +35,7 @@ func TestEntryEncoder_EncodeEntry(t *testing.T) {
logzap.Component("Items.Service"), logzap.Component("Items.Service"),
logzap.Event("Items.Create"), logzap.Event("Items.Create"),
logzap.Object("/spaces/WPNN/envs/9VGP/cols/GxNv/items/W0fl"), logzap.Object("/spaces/WPNN/envs/9VGP/cols/GxNv/items/W0fl"),
logzap.Caller("/users/PHVz"), logzap.Caller(nil, logzap.WithObject("/users/PHVz")),
logzap.Attr("any"), logzap.Attr("any"),
logzap.Tags("tag1", "tag2", "tag3"), logzap.Tags("tag1", "tag2", "tag3"),
}, },
...@@ -70,7 +70,7 @@ func BenchmarkEntryEncoderSimple(b *testing.B) { ...@@ -70,7 +70,7 @@ func BenchmarkEntryEncoderSimple(b *testing.B) {
fields := []zapcore.Field{ fields := []zapcore.Field{
logzap.Event(items.EventCreate), logzap.Event(items.EventCreate),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)), logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"), logzap.Caller(nil, logzap.WithObject("/system")),
logzap.Tags("tag1", "tag2", "tag3"), logzap.Tags("tag1", "tag2", "tag3"),
} }
...@@ -84,7 +84,7 @@ func BenchmarkEntryEncoderUnknownFields(b *testing.B) { ...@@ -84,7 +84,7 @@ func BenchmarkEntryEncoderUnknownFields(b *testing.B) {
fields := []zapcore.Field{ fields := []zapcore.Field{
logzap.Event(items.EventCreate), logzap.Event(items.EventCreate),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)), logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"), logzap.Caller(nil, logzap.WithObject("/system")),
logzap.Tags("tag1", "tag2", "tag3"), logzap.Tags("tag1", "tag2", "tag3"),
} }
...@@ -102,7 +102,7 @@ func BenchmarkEntryEncoderV2Simple(b *testing.B) { ...@@ -102,7 +102,7 @@ func BenchmarkEntryEncoderV2Simple(b *testing.B) {
fields := []zapcore.Field{ fields := []zapcore.Field{
logzap.Event(items.EventCreate), logzap.Event(items.EventCreate),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)), logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"), logzap.Caller(nil, logzap.WithObject("/system")),
logzap.Tags("tag1", "tag2", "tag3"), logzap.Tags("tag1", "tag2", "tag3"),
} }
...@@ -116,7 +116,7 @@ func BenchmarkEntryEncoderV2UnknownFields(b *testing.B) { ...@@ -116,7 +116,7 @@ func BenchmarkEntryEncoderV2UnknownFields(b *testing.B) {
fields := []zapcore.Field{ fields := []zapcore.Field{
logzap.Event(items.EventCreate), logzap.Event(items.EventCreate),
logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)), logzap.Object(items.NewItem("WPNN", "9VGP", "GxNv", "W0fl", nil, nil)),
logzap.Caller("/system"), logzap.Caller(nil, logzap.WithObject("/system")),
logzap.Tags("tag1", "tag2", "tag3"), logzap.Tags("tag1", "tag2", "tag3"),
} }
......
...@@ -77,7 +77,7 @@ func TestExample(t *testing.T) { ...@@ -77,7 +77,7 @@ func TestExample(t *testing.T) {
logger.Info("Item created", logger.Info("Item created",
logzap.Event(items.EventCreate), logzap.Event(items.EventCreate),
logzap.Object(item), logzap.Object(item),
logzap.CallerFromContext(ctx, item.SpaceID), logzap.Caller(ctx, logzap.WithSpace(item.SpaceID)),
logzap.Tags("tag1", "tag2", "tag3"), logzap.Tags("tag1", "tag2", "tag3"),
) )
...@@ -85,7 +85,7 @@ func TestExample(t *testing.T) { ...@@ -85,7 +85,7 @@ func TestExample(t *testing.T) {
logger.Warn("Item updated", logger.Warn("Item updated",
logzap.Event(items.EventUpdate), logzap.Event(items.EventUpdate),
logzap.Object(item), logzap.Object(item),
logzap.CallerFromContext(ctx, item.SpaceID), logzap.Caller(ctx, logzap.WithSpace(item.SpaceID)),
logzap.Attr(map[string]map[string]any{"title": {"old": "old title", "new": "new title"}}), logzap.Attr(map[string]map[string]any{"title": {"old": "old title", "new": "new title"}}),
) )
} }
......
...@@ -30,7 +30,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections. ...@@ -30,7 +30,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections.
spaceID = collection.SpaceID spaceID = collection.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(collections.EventCollectionCreate), logzap.Event(collections.EventCollectionCreate),
) )
...@@ -46,7 +46,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections. ...@@ -46,7 +46,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, collection *collections.
func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId string, collectionId string) (err error) { func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId string, collectionId string) (err error) {
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
logzap.Event(collections.EventCollectionDelete), logzap.Event(collections.EventCollectionDelete),
logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)), logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)),
) )
...@@ -63,7 +63,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId st ...@@ -63,7 +63,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, spaceId string, envId st
func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, options ...*collections.GetOptions) (collection *collections.Collection, err error) { func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, options ...*collections.GetOptions) (collection *collections.Collection, err error) {
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
collection, err = m.next.Get(ctx, spaceId, envId, collectionId, options...) collection, err = m.next.Get(ctx, spaceId, envId, collectionId, options...)
...@@ -77,7 +77,7 @@ func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId strin ...@@ -77,7 +77,7 @@ func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId strin
func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId string, filter *collections.Filter) (collections []*collections.Collection, err error) { func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId string, filter *collections.Filter) (collections []*collections.Collection, err error) {
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
collections, err = m.next.List(ctx, spaceId, envId, filter) collections, err = m.next.List(ctx, spaceId, envId, filter)
...@@ -91,7 +91,7 @@ func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId stri ...@@ -91,7 +91,7 @@ func (m *loggingMiddleware) List(ctx context.Context, spaceId string, envId stri
func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId string, collectionId string, schema *schema.Schema) (err error) { func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId string, collectionId string, schema *schema.Schema) (err error) {
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
logzap.Event(collections.EventCollectionSetSchema), logzap.Event(collections.EventCollectionSetSchema),
logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)), logzap.Object(id.NewCollectionId(spaceId, envId, collectionId)),
) )
...@@ -108,7 +108,7 @@ func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId ...@@ -108,7 +108,7 @@ func (m *loggingMiddleware) SetSchema(ctx context.Context, spaceId string, envId
func (m *loggingMiddleware) SetState(ctx context.Context, spaceId string, envId string, collectionId string, state *collections.StateInfo) (err error) { func (m *loggingMiddleware) SetState(ctx context.Context, spaceId string, envId string, collectionId string, state *collections.StateInfo) (err error) {
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
err = m.next.SetState(ctx, spaceId, envId, collectionId, state) err = m.next.SetState(ctx, spaceId, envId, collectionId, state)
...@@ -127,7 +127,7 @@ func (m *loggingMiddleware) Update(ctx context.Context, coll *collections.Collec ...@@ -127,7 +127,7 @@ func (m *loggingMiddleware) Update(ctx context.Context, coll *collections.Collec
spaceID = coll.SpaceID spaceID = coll.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(collections.EventCollectionUpdate), logzap.Event(collections.EventCollectionUpdate),
logzap.Object(coll), logzap.Object(coll),
) )
......
...@@ -26,7 +26,7 @@ func LoggingMiddleware(logger *zap.Logger) Middleware { ...@@ -26,7 +26,7 @@ func LoggingMiddleware(logger *zap.Logger) Middleware {
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) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
result, err = m.next.Aggregate(ctx, spaceId, envId, collectionId, filter, options...) result, err = m.next.Aggregate(ctx, spaceId, envId, collectionId, filter, options...)
...@@ -40,7 +40,7 @@ func (m *loggingMiddleware) Aggregate(ctx context.Context, spaceId string, envId ...@@ -40,7 +40,7 @@ func (m *loggingMiddleware) Aggregate(ctx context.Context, spaceId string, envId
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) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
result, err = m.next.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...) result, err = m.next.AggregatePublished(ctx, spaceId, envId, collectionId, filter, options...)
...@@ -58,7 +58,7 @@ func (m *loggingMiddleware) Archive(ctx context.Context, item *items.Item, optio ...@@ -58,7 +58,7 @@ func (m *loggingMiddleware) Archive(ctx context.Context, item *items.Item, optio
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventArchive), logzap.Event(items.EventArchive),
logzap.Object(item), logzap.Object(item),
) )
...@@ -79,7 +79,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, item *items.Item, opts . ...@@ -79,7 +79,7 @@ func (m *loggingMiddleware) Create(ctx context.Context, item *items.Item, opts .
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventCreate), logzap.Event(items.EventCreate),
) )
...@@ -99,7 +99,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, option ...@@ -99,7 +99,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, option
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventDelete), logzap.Event(items.EventDelete),
logzap.Object(item), logzap.Object(item),
) )
...@@ -116,7 +116,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, option ...@@ -116,7 +116,7 @@ func (m *loggingMiddleware) Delete(ctx context.Context, item *items.Item, option
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) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
items, total, err = m.next.Find(ctx, spaceId, envId, collectionId, filter, options...) items, total, err = m.next.Find(ctx, spaceId, envId, collectionId, filter, options...)
...@@ -131,7 +131,7 @@ func (m *loggingMiddleware) Find(ctx context.Context, spaceId string, envId stri ...@@ -131,7 +131,7 @@ func (m *loggingMiddleware) Find(ctx context.Context, spaceId string, envId stri
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) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
items, total, err = m.next.FindArchived(ctx, spaceId, envId, collectionId, filter, options...) items, total, err = m.next.FindArchived(ctx, spaceId, envId, collectionId, filter, options...)
...@@ -145,7 +145,7 @@ func (m *loggingMiddleware) FindArchived(ctx context.Context, spaceId string, en ...@@ -145,7 +145,7 @@ func (m *loggingMiddleware) FindArchived(ctx context.Context, spaceId string, en
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) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
) )
items, total, err = m.next.FindPublished(ctx, spaceId, envId, collectionId, filter, options...) items, total, err = m.next.FindPublished(ctx, spaceId, envId, collectionId, filter, options...)
...@@ -159,7 +159,7 @@ func (m *loggingMiddleware) FindPublished(ctx context.Context, spaceId string, e ...@@ -159,7 +159,7 @@ func (m *loggingMiddleware) FindPublished(ctx context.Context, spaceId string, e
func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetOptions) (item *items.Item, err error) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)), logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
) )
...@@ -174,7 +174,7 @@ func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId strin ...@@ -174,7 +174,7 @@ func (m *loggingMiddleware) Get(ctx context.Context, spaceId string, envId strin
func (m *loggingMiddleware) GetPublished(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.GetPublishedOptions) (item *items.Item, err error) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)), logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
) )
...@@ -189,7 +189,7 @@ func (m *loggingMiddleware) GetPublished(ctx context.Context, spaceId string, en ...@@ -189,7 +189,7 @@ func (m *loggingMiddleware) GetPublished(ctx context.Context, spaceId string, en
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) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)), logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
) )
...@@ -208,7 +208,7 @@ func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, op ...@@ -208,7 +208,7 @@ func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, op
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Object(item), logzap.Object(item),
) )
...@@ -223,7 +223,7 @@ func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, op ...@@ -223,7 +223,7 @@ func (m *loggingMiddleware) Introspect(ctx context.Context, item *items.Item, op
func (m *loggingMiddleware) ListRevisions(ctx context.Context, spaceId string, envId string, collectionId string, itemId string, options ...*items.ListRevisionsOptions) (items []*items.Item, err error) { 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( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceId), logzap.Caller(ctx, logzap.WithSpace(spaceId)),
logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)), logzap.Object(id.NewItemId(spaceId, envId, collectionId, itemId)),
) )
...@@ -242,7 +242,7 @@ func (m *loggingMiddleware) Publish(ctx context.Context, item *items.Item, optio ...@@ -242,7 +242,7 @@ func (m *loggingMiddleware) Publish(ctx context.Context, item *items.Item, optio
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventPublish), logzap.Event(items.EventPublish),
logzap.Object(item), logzap.Object(item),
) )
...@@ -263,7 +263,7 @@ func (m *loggingMiddleware) Unarchive(ctx context.Context, item *items.Item, opt ...@@ -263,7 +263,7 @@ func (m *loggingMiddleware) Unarchive(ctx context.Context, item *items.Item, opt
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventUnarchive), logzap.Event(items.EventUnarchive),
logzap.Object(item), logzap.Object(item),
) )
...@@ -284,7 +284,7 @@ func (m *loggingMiddleware) Undelete(ctx context.Context, item *items.Item, opti ...@@ -284,7 +284,7 @@ func (m *loggingMiddleware) Undelete(ctx context.Context, item *items.Item, opti
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventUndelete), logzap.Event(items.EventUndelete),
logzap.Object(item), logzap.Object(item),
) )
...@@ -305,7 +305,7 @@ func (m *loggingMiddleware) Unpublish(ctx context.Context, item *items.Item, opt ...@@ -305,7 +305,7 @@ func (m *loggingMiddleware) Unpublish(ctx context.Context, item *items.Item, opt
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventUnpublish), logzap.Event(items.EventUnpublish),
logzap.Object(item), logzap.Object(item),
) )
...@@ -326,7 +326,7 @@ func (m *loggingMiddleware) Update(ctx context.Context, item *items.Item, option ...@@ -326,7 +326,7 @@ func (m *loggingMiddleware) Update(ctx context.Context, item *items.Item, option
spaceID = item.SpaceID spaceID = item.SpaceID
} }
logger := m.logger.With( logger := m.logger.With(
logzap.CallerFromContext(ctx, spaceID), logzap.Caller(ctx, logzap.WithSpace(spaceID)),
logzap.Event(items.EventUpdate), logzap.Event(items.EventUpdate),
logzap.Object(item), logzap.Object(item),
) )
......
...@@ -2,7 +2,6 @@ package zap ...@@ -2,7 +2,6 @@ package zap
import ( import (
"context" "context"
"git.perx.ru/perxis/perxis-go/id" "git.perx.ru/perxis/perxis-go/id"
_ "git.perx.ru/perxis/perxis-go/id/system" // регистрируем обработчики для системных объектов _ "git.perx.ru/perxis/perxis-go/id/system" // регистрируем обработчики для системных объектов
"git.perx.ru/perxis/perxis-go/pkg/auth" "git.perx.ru/perxis/perxis-go/pkg/auth"
...@@ -47,22 +46,42 @@ func Object(v any) zap.Field { ...@@ -47,22 +46,42 @@ func Object(v any) zap.Field {
return zap.Reflect("object", oid) return zap.Reflect("object", oid)
} }
// Caller возвращает поле и устанавливает передаваемый аргумент в качестве "вызывающего" в формате ObjectId. type callerConfig struct {
// Поддерживает типы в формате ObjectId: id.Descriptor, string, map[string]any, системные объекты. object any
func Caller(v any) zap.Field { spaceID string
oid, _ := id.NewObjectId(v) }
return zap.Reflect("caller", oid)
type CallerOption func(c *callerConfig)
func WithObject(object any) CallerOption {
return func(c *callerConfig) {
c.object = object
}
}
func WithSpace(spaceID string) CallerOption {
return func(c *callerConfig) {
c.spaceID = spaceID
}
} }
// CallerFromContext извлекает auth.Principal из контекста и устанавливает его в качестве "вызывающего" в формате Object. // Caller возвращает поле и устанавливает в зависимости от переданных опций "вызывающего" в формате ObjectId.
// Вторым параметром передается идентификатор пространства, который требуется, если вызывающий является auth.SpaceAccessor. func Caller(ctx context.Context, options ...CallerOption) zap.Field {
// Если вызывающий не связан с пространством, следует передать пустую строку. c := new(callerConfig)
func CallerFromContext(ctx context.Context, spaceID string) zap.Field { for _, o := range options {
o(c)
}
var oid *id.ObjectId
if c.object != nil {
oid, _ = id.NewObjectId(c.object)
} else if ctx != nil {
principal := auth.GetPrincipal(ctx) principal := auth.GetPrincipal(ctx)
if accessor, ok := principal.(auth.SpaceAccessor); ok && spaceID != "" { if accessor, ok := principal.(auth.SpaceAccessor); ok && c.spaceID != "" {
principal = accessor.Space(spaceID) principal = accessor.Space(c.spaceID)
}
oid, _ = id.NewObjectId(principal)
} }
return Caller(principal) return zap.Reflect("caller", oid)
} }
func Attr(attr any) zap.Field { func Attr(attr any) zap.Field {
......
...@@ -132,43 +132,19 @@ func TestCaller(t *testing.T) { ...@@ -132,43 +132,19 @@ func TestCaller(t *testing.T) {
oid := id.MustObjectId(user) oid := id.MustObjectId(user)
userId := id.NewUserId(user.ID) userId := id.NewUserId(user.ID)
tests := []struct {
name string
field zap.Field
want zap.Field
}{
{name: "system object", field: Caller(user), want: zap.Reflect("caller", oid)},
{name: "object id", field: Caller(userId), want: zap.Reflect("caller", oid)},
{name: "string", field: Caller(oid.String()), want: zap.Reflect("caller", oid)},
{name: "invalid", field: Caller(nil), want: zap.Reflect("caller", (*id.ObjectId)(nil))},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
wantObjectId, ok1 := tc.want.Interface.(*id.ObjectId)
fieldObjectId, ok2 := tc.field.Interface.(*id.ObjectId)
if ok1 && ok2 && wantObjectId != nil && fieldObjectId != nil {
assert.Equal(t, wantObjectId.String(), fieldObjectId.String())
} else {
assert.Equal(t, tc.want.Interface, tc.field.Interface)
}
})
}
}
func TestCallerFromContext(t *testing.T) {
ctx := auth.WithSystem(context.Background()) ctx := auth.WithSystem(context.Background())
oid := id.MustObjectId(auth.GetPrincipal(ctx))
tests := []struct { tests := []struct {
name string name string
field zap.Field field zap.Field
want zap.Field want zap.Field
}{ }{
{name: "ok", field: CallerFromContext(ctx, ""), want: zap.Reflect("caller", oid)}, {name: "system object", field: Caller(nil, WithObject(user)), want: zap.Reflect("caller", oid)},
{name: "invalid", field: CallerFromContext(context.TODO(), ""), want: zap.Reflect("caller", (*id.ObjectId)(nil))}, {name: "object id", field: Caller(nil, WithObject(userId)), want: zap.Reflect("caller", oid)},
{name: "string", field: Caller(nil, WithObject(oid.String())), want: zap.Reflect("caller", oid)},
{name: "invalid", field: Caller(nil), want: zap.Reflect("caller", (*id.ObjectId)(nil))},
{name: "context", field: Caller(ctx), want: zap.Reflect("caller", id.MustObjectId(auth.GetPrincipal(ctx)))},
{name: "invalid context", field: Caller(context.TODO()), want: zap.Reflect("caller", (*id.ObjectId)(nil))},
} }
for _, tc := range tests { for _, tc := range tests {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment