Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
eea44197
Commit
eea44197
authored
1 year ago
by
Semyon Krestyaninov
Browse files
Options
Downloads
Patches
Plain Diff
добавлен канальный zap core
parent
6f4b0a04
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
log/zap/channel_core.go
+43
-0
43 additions, 0 deletions
log/zap/channel_core.go
log/zap/channel_core_test.go
+44
-0
44 additions, 0 deletions
log/zap/channel_core_test.go
log/zap/field.go
+8
-0
8 additions, 0 deletions
log/zap/field.go
log/zap/filter_core.go
+28
-0
28 additions, 0 deletions
log/zap/filter_core.go
with
123 additions
and
0 deletions
log/zap/channel_core.go
0 → 100644
+
43
−
0
View file @
eea44197
package
zap
import
(
"strings"
"go.uber.org/zap/zapcore"
)
const
channelKey
=
"channel"
func
HasChannel
(
channel
string
)
FilterFunc
{
return
func
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
bool
{
for
_
,
f
:=
range
fields
{
if
f
.
Key
==
channelKey
&&
f
.
Type
==
zapcore
.
StringType
{
list
:=
strings
.
Split
(
f
.
String
,
","
)
for
_
,
v
:=
range
list
{
if
v
==
channel
{
return
true
}
}
}
}
return
false
}
}
// NewDefaultChannelCore аналогичен NewChannelCore, но также устанавливает переданный канал в качестве канала по умолчанию.
// Это означает, что если поле Channel в записи не указано, запись все равно будет передана в zapcore.Core.
func
NewDefaultChannelCore
(
core
zapcore
.
Core
,
channel
string
)
zapcore
.
Core
{
return
RegisterFilters
(
core
,
Or
(
HasChannel
(
channel
),
Not
(
HasKey
(
channelKey
)),
),
)
}
// NewChannelCore добавляет к переданному Core фильтрацию записей по каналам.
// Это означает, что если запись содержит поле Channel и значение соответствует переданному каналу, то запись будет передана в zapcore.Core.
func
NewChannelCore
(
core
zapcore
.
Core
,
channel
string
)
zapcore
.
Core
{
return
RegisterFilters
(
core
,
HasChannel
(
channel
))
}
This diff is collapsed.
Click to expand it.
log/zap/channel_core_test.go
0 → 100644
+
44
−
0
View file @
eea44197
package
zap
import
(
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func
TestNewChannelCore_WriteSingleChannel
(
t
*
testing
.
T
)
{
core
,
logs
:=
observer
.
New
(
zapcore
.
InfoLevel
)
core
=
NewChannelCore
(
core
,
"test"
)
core
.
Write
(
zapcore
.
Entry
{
Message
:
"msg"
},
[]
zapcore
.
Field
{
Channel
(
"test"
)})
core
.
Write
(
zapcore
.
Entry
{
Message
:
"msg"
},
[]
zapcore
.
Field
{
Channel
(
"empty"
)})
// запись не попадет в лог
require
.
Equal
(
t
,
1
,
logs
.
Len
())
}
func
TestNewChannelCore_WriteMultiplyChannels
(
t
*
testing
.
T
)
{
core
,
logs
:=
observer
.
New
(
zapcore
.
InfoLevel
)
core
=
zapcore
.
NewTee
(
NewChannelCore
(
core
,
"test1"
),
NewChannelCore
(
core
,
"test2"
),
)
core
.
Write
(
zapcore
.
Entry
{
Message
:
"msg"
},
[]
zapcore
.
Field
{
Channel
(
"test1"
,
"test2"
)})
// запись попадет сразу в 2 core
require
.
Equal
(
t
,
2
,
logs
.
Len
())
}
func
TestNewDefaultChannelCore
(
t
*
testing
.
T
)
{
core
,
logs
:=
observer
.
New
(
zapcore
.
InfoLevel
)
core
=
NewDefaultChannelCore
(
core
,
"test1"
)
core
.
Write
(
zapcore
.
Entry
{
Message
:
"msg"
},
[]
zapcore
.
Field
{
Channel
(
"test1"
)})
core
.
Write
(
zapcore
.
Entry
{
Message
:
"msg"
},
[]
zapcore
.
Field
{
Channel
(
"test2"
)})
// эта запись не попадет в лог
core
.
Write
(
zapcore
.
Entry
{
Message
:
"msg"
},
[]
zapcore
.
Field
{})
require
.
Equal
(
t
,
2
,
logs
.
Len
())
}
This diff is collapsed.
Click to expand it.
log/zap/field.go
+
8
−
0
View file @
eea44197
...
...
@@ -2,6 +2,7 @@ package zap
import
(
"context"
"strings"
"git.perx.ru/perxis/perxis-go/id"
_
"git.perx.ru/perxis/perxis-go/id/system"
// регистрируем обработчики для системных объектов
...
...
@@ -9,6 +10,13 @@ import (
"go.uber.org/zap"
)
func
Channel
(
channels
...
string
)
zap
.
Field
{
if
len
(
channels
)
==
0
{
return
zap
.
Skip
()
}
return
zap
.
String
(
channelKey
,
strings
.
Join
(
channels
,
","
))
}
func
Category
(
category
string
)
zap
.
Field
{
if
category
==
""
{
return
zap
.
Skip
()
...
...
This diff is collapsed.
Click to expand it.
log/zap/filter_core.go
+
28
−
0
View file @
eea44197
...
...
@@ -24,6 +24,34 @@ func NotHasField(field zapcore.Field) FilterFunc {
}
}
func
HasKey
(
key
string
)
FilterFunc
{
return
func
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
bool
{
for
_
,
f
:=
range
fields
{
if
f
.
Key
==
key
{
return
true
}
}
return
false
}
}
func
Or
(
filters
...
FilterFunc
)
FilterFunc
{
return
func
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
bool
{
for
_
,
f
:=
range
filters
{
if
f
(
entry
,
fields
)
{
return
true
}
}
return
false
}
}
func
Not
(
filter
FilterFunc
)
FilterFunc
{
return
func
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
bool
{
return
!
filter
(
entry
,
fields
)
}
}
type
filterCore
struct
{
zapcore
.
Core
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment