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
37d31351
Commit
37d31351
authored
1 year ago
by
Semyon Krestyaninov
Browse files
Options
Downloads
Patches
Plain Diff
feature: add filter_core.go
parent
083aaf1f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
log/zap/filter_core.go
+51
-0
51 additions, 0 deletions
log/zap/filter_core.go
log/zap/filter_core_test.go
+34
-0
34 additions, 0 deletions
log/zap/filter_core_test.go
with
85 additions
and
0 deletions
log/zap/filter_core.go
0 → 100644
+
51
−
0
View file @
37d31351
package
zap
import
(
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type
FilterFunc
func
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
bool
type
filterCore
struct
{
zapcore
.
Core
filters
[]
FilterFunc
fields
[]
zap
.
Field
}
func
NewFilterCore
(
core
zapcore
.
Core
,
filters
...
FilterFunc
)
zapcore
.
Core
{
return
&
filterCore
{
Core
:
core
,
filters
:
filters
,
}
}
func
(
core
*
filterCore
)
With
(
fields
[]
zapcore
.
Field
)
zapcore
.
Core
{
return
&
filterCore
{
Core
:
core
.
Core
.
With
(
fields
),
filters
:
core
.
filters
,
fields
:
append
(
core
.
fields
,
fields
...
),
}
}
func
(
core
*
filterCore
)
Check
(
entry
zapcore
.
Entry
,
checkedEntry
*
zapcore
.
CheckedEntry
)
*
zapcore
.
CheckedEntry
{
if
core
.
Core
.
Enabled
(
entry
.
Level
)
{
return
checkedEntry
.
AddCore
(
entry
,
core
)
}
return
checkedEntry
}
func
(
core
*
filterCore
)
Write
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
error
{
if
len
(
core
.
filters
)
>
0
{
fields
=
append
(
core
.
fields
,
fields
...
)
}
for
_
,
filter
:=
range
core
.
filters
{
if
!
filter
(
entry
,
fields
)
{
return
nil
}
}
return
core
.
Core
.
Write
(
entry
,
fields
)
}
This diff is collapsed.
Click to expand it.
log/zap/filter_core_test.go
0 → 100644
+
34
−
0
View file @
37d31351
package
zap
import
(
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func
TestFilterCore_Write
(
t
*
testing
.
T
)
{
core
,
logs
:=
observer
.
New
(
zapcore
.
InfoLevel
)
core
=
NewFilterCore
(
core
,
func
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
bool
{
enc
:=
zapcore
.
NewMapObjectEncoder
()
for
_
,
field
:=
range
fields
{
field
.
AddTo
(
enc
)
}
check
,
_
:=
enc
.
Fields
[
"check"
]
.
(
bool
)
return
check
})
logger
:=
zap
.
New
(
core
)
{
logger
:=
logger
.
With
(
zap
.
Bool
(
"check"
,
true
))
logger
.
Info
(
"check true 1"
)
logger
.
Info
(
"check true 2"
)
}
logger
.
Info
(
"check false 1"
)
logger
.
Info
(
"check false 2"
)
require
.
Equal
(
t
,
2
,
logs
.
Len
())
}
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