Skip to content
Snippets Groups Projects
Commit 96f00cbe authored by Alena Petraki's avatar Alena Petraki :nail_care_tone1:
Browse files

Вынесена базовая логика работы с zap в пакет zap

parent 839125d9
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@ import (
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/log"
zap2 "git.perx.ru/perxis/perxis-go/zap"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
......@@ -30,13 +31,13 @@ func TestCore_getEntry(t *testing.T) {
entry: zapcore.Entry{Level: zapcore.InfoLevel, Message: "создан элемент коллекции"},
fields: []zapcore.Field{
zap.String("key", "val"), // будет проигнорировано
Category("create"),
Component("Items.Service"),
Event("Items.Create"),
Object("/spaces/WPNN/envs/9VGP/cols/GxNv/items/W0fl"),
Caller("/users/PHVz"),
Attr("any"),
Tags("tag1", "tag2", "tag3"),
zap2.Category("create"),
zap2.Component("Items.Service"),
zap2.Event("Items.Create"),
zap2.Object("/spaces/WPNN/envs/9VGP/cols/GxNv/items/W0fl"),
zap2.Caller("/users/PHVz"),
zap2.Attr("any"),
zap2.Tags("tag1", "tag2", "tag3"),
},
},
want: &log.Entry{
......
......@@ -13,6 +13,7 @@ import (
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/users"
usersmocks "git.perx.ru/perxis/perxis-go/pkg/users/mocks"
zap2 "git.perx.ru/perxis/perxis-go/zap"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
......@@ -69,23 +70,23 @@ func TestExample(t *testing.T) {
// Пример отправки логов для сервиса Items
{
logger := logger.With(Component("Items"))
logger := logger.With(zap2.Component("Items"))
ctx := auth.WithPrincipal(context.Background(), factory.User("74d90aaf"))
// Отправка лога при создании item
logger.Info("Successfully created",
Event(items.EventCreateItem),
Object(item),
CallerFromContext(ctx),
Tags("tag1", "tag2", "tag3"),
zap2.Event(items.EventCreateItem),
zap2.Object(item),
zap2.CallerFromContext(ctx),
zap2.Tags("tag1", "tag2", "tag3"),
)
// Отправка лога при обновлении item
logger.Warn("Successfully updated",
Event(items.EventUpdateItem),
Object(item),
CallerFromContext(ctx),
Attr(map[string]map[string]any{"title": {"old": "old title", "new": "new title"}}),
zap2.Event(items.EventUpdateItem),
zap2.Object(item),
zap2.CallerFromContext(ctx),
zap2.Attr(map[string]map[string]any{"title": {"old": "old title", "new": "new title"}}),
)
}
......
......@@ -31,7 +31,7 @@ func NewDefaultChannelCore(core zapcore.Core, channel string) zapcore.Core {
core,
Or(
HasChannel(channel),
Not(HasKey(channelKey)),
Not(ContainsKey(channelKey)),
),
)
}
......
File moved
File moved
File moved
......@@ -7,7 +7,7 @@ import (
type FilterFunc func(entry zapcore.Entry, fields []zapcore.Field) bool
func HasField(field zapcore.Field) FilterFunc {
func ContainsField(field zapcore.Field) FilterFunc {
return func(_ zapcore.Entry, fields []zapcore.Field) bool {
for _, f := range fields {
if f.Equals(field) {
......@@ -18,13 +18,7 @@ func HasField(field zapcore.Field) FilterFunc {
}
}
func NotHasField(field zapcore.Field) FilterFunc {
return func(entry zapcore.Entry, fields []zapcore.Field) bool {
return !HasField(field)(entry, fields)
}
}
func HasKey(key string) FilterFunc {
func ContainsKey(key string) FilterFunc {
return func(entry zapcore.Entry, fields []zapcore.Field) bool {
for _, f := range fields {
if f.Key == key {
......@@ -70,7 +64,7 @@ type filterCore struct {
//
// l, _ := zap.NewDevelopment()
// core := l.Core().With([]zapcore.Field{zap.Int("a", 5)})
// core = RegisterFilters(core, HasField(zap.Int("a", 5)))
// core = RegisterFilters(core, ContainsField(zap.Int("a", 5)))
//
// logger := zap.New(core)
// logger.Info("Test log") // НЕ будет записан
......
......@@ -11,7 +11,7 @@ import (
func TestFilterCore_Write(t *testing.T) {
core, logs := observer.New(zapcore.InfoLevel)
core = RegisterFilters(core, HasField(zap.Bool("check", true)))
core = RegisterFilters(core, ContainsField(zap.Bool("check", true)))
err := core.With([]zapcore.Field{zap.Bool("check", true)}).Write(zapcore.Entry{Message: "msg"}, nil)
require.NoError(t, err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment