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
d5f28920
Commit
d5f28920
authored
1 year ago
by
Semyon Krestyaninov
Browse files
Options
Downloads
Patches
Plain Diff
refactor
parent
ebecdb4c
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
logs/zap/core.go
+16
-51
16 additions, 51 deletions
logs/zap/core.go
logs/zap/core_test.go
+0
-64
0 additions, 64 deletions
logs/zap/core_test.go
logs/zap/entry_encoder.go
+71
-0
71 additions, 0 deletions
logs/zap/entry_encoder.go
logs/zap/entry_encoder_test.go
+1
-0
1 addition, 0 deletions
logs/zap/entry_encoder_test.go
with
88 additions
and
115 deletions
logs/zap/core.go
+
16
−
51
View file @
d5f28920
package
zap
import
(
"fmt"
oid
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/logs"
"git.perx.ru/perxis/perxis-go/pkg/id"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
...
...
@@ -20,22 +15,27 @@ type WriteSyncer interface {
type
Core
struct
{
zapcore
.
LevelEnabler
w
riteSyncer
WriteSyncer
fields
[]
zap
.
Field
w
s
WriteSyncer
enc
Encoder
}
func
NewCore
(
writeSyncer
WriteSyncer
)
*
Core
{
return
&
Core
{
LevelEnabler
:
zapcore
.
InfoLevel
,
writeSyncer
:
writeSyncer
,
ws
:
writeSyncer
,
enc
:
NewEntryEncoder
(),
}
}
func
(
core
*
Core
)
With
(
fields
[]
zapcore
.
Field
)
zapcore
.
Core
{
clone
:=
core
.
enc
.
Clone
()
for
i
:=
range
fields
{
fields
[
i
]
.
AddTo
(
clone
)
}
return
&
Core
{
LevelEnabler
:
core
.
LevelEnabler
,
w
riteSyncer
:
core
.
writeSyncer
,
fields
:
append
(
core
.
fields
,
fields
...
)
,
w
s
:
core
.
ws
,
enc
:
clone
,
}
}
...
...
@@ -47,48 +47,13 @@ func (core *Core) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry)
}
func
(
core
*
Core
)
Write
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
error
{
return
core
.
writeSyncer
.
Write
(
core
.
getEntry
(
entry
,
fields
))
encodeEntry
,
err
:=
core
.
enc
.
EncodeEntry
(
entry
,
fields
)
if
err
!=
nil
{
return
err
}
return
core
.
ws
.
Write
(
encodeEntry
)
}
func
(
core
*
Core
)
Sync
()
error
{
return
core
.
writeSyncer
.
Sync
()
}
func
(
core
*
Core
)
getEntry
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
*
logs
.
Entry
{
if
len
(
core
.
fields
)
>
0
{
fields
=
append
(
fields
,
core
.
fields
...
)
}
enc
:=
zapcore
.
NewMapObjectEncoder
()
for
_
,
field
:=
range
fields
{
field
.
AddTo
(
enc
)
}
ent
:=
&
logs
.
Entry
{
ID
:
id
.
GenerateNewID
(),
Timestamp
:
entry
.
Time
,
LogLevel
:
logs
.
Level
(
entry
.
Level
),
Message
:
entry
.
Message
,
}
ent
.
Category
,
_
=
enc
.
Fields
[
"category"
]
.
(
string
)
ent
.
Component
,
_
=
enc
.
Fields
[
"component"
]
.
(
string
)
ent
.
Event
,
_
=
enc
.
Fields
[
"event"
]
.
(
string
)
ent
.
ObjectID
,
_
=
enc
.
Fields
[
"object"
]
.
(
*
oid
.
ObjectId
)
ent
.
CallerID
,
_
=
enc
.
Fields
[
"caller"
]
.
(
*
oid
.
ObjectId
)
ent
.
Attr
=
enc
.
Fields
[
"attr"
]
if
err
,
_
:=
enc
.
Fields
[
"error"
]
.
(
error
);
err
!=
nil
{
ent
.
Message
=
fmt
.
Sprintf
(
"%s. Error: %s"
,
ent
.
Message
,
err
.
Error
())
}
if
tags
,
ok
:=
enc
.
Fields
[
"tags"
]
.
([]
any
);
ok
{
for
_
,
item
:=
range
tags
{
if
tag
,
ok
:=
item
.
(
string
);
ok
{
ent
.
Tags
=
append
(
ent
.
Tags
,
tag
)
}
}
}
return
ent
return
core
.
ws
.
Sync
()
}
This diff is collapsed.
Click to expand it.
logs/zap/core_test.go
+
0
−
64
View file @
d5f28920
package
zap
import
(
"testing"
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/logs"
logzap
"git.perx.ru/perxis/perxis-go/zap"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func
TestCore_getEntry
(
t
*
testing
.
T
)
{
core
:=
NewCore
(
nil
)
tests
:=
[]
struct
{
name
string
input
struct
{
entry
zapcore
.
Entry
fields
[]
zapcore
.
Field
}
want
*
logs
.
Entry
}{
{
name
:
"simple"
,
input
:
struct
{
entry
zapcore
.
Entry
fields
[]
zapcore
.
Field
}{
entry
:
zapcore
.
Entry
{
Level
:
zapcore
.
InfoLevel
,
Message
:
"создан элемент коллекции"
},
fields
:
[]
zapcore
.
Field
{
zap
.
String
(
"key"
,
"val"
),
// будет проигнорировано
logzap
.
Category
(
"create"
),
logzap
.
Component
(
"Items.Service"
),
logzap
.
Event
(
"Items.Create"
),
logzap
.
Object
(
"/spaces/WPNN/envs/9VGP/cols/GxNv/items/W0fl"
),
logzap
.
Caller
(
"/users/PHVz"
),
logzap
.
Attr
(
"any"
),
logzap
.
Tags
(
"tag1"
,
"tag2"
,
"tag3"
),
},
},
want
:
&
logs
.
Entry
{
LogLevel
:
logs
.
Level
(
zapcore
.
InfoLevel
),
Message
:
"создан элемент коллекции"
,
Category
:
"create"
,
Component
:
"Items.Service"
,
Event
:
"Items.Create"
,
ObjectID
:
id
.
MustObjectId
(
"/spaces/WPNN/envs/9VGP/cols/GxNv/items/W0fl"
),
CallerID
:
id
.
MustObjectId
(
"/users/PHVz"
),
Attr
:
"any"
,
Tags
:
[]
string
{
"tag1"
,
"tag2"
,
"tag3"
},
},
},
}
for
_
,
tc
:=
range
tests
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
got
:=
core
.
getEntry
(
tc
.
input
.
entry
,
tc
.
input
.
fields
)
got
.
ID
=
tc
.
want
.
ID
// игнорируем ID
got
.
Timestamp
=
tc
.
want
.
Timestamp
// игнорируем Timestamp
require
.
Equal
(
t
,
tc
.
want
,
got
)
})
}
}
This diff is collapsed.
Click to expand it.
logs/zap/entry_encoder.go
0 → 100644
+
71
−
0
View file @
d5f28920
package
zap
import
(
"fmt"
"maps"
oid
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/logs"
"git.perx.ru/perxis/perxis-go/pkg/id"
"go.uber.org/zap/zapcore"
)
type
Encoder
interface
{
zapcore
.
ObjectEncoder
Clone
()
Encoder
EncodeEntry
(
zapcore
.
Entry
,
[]
zapcore
.
Field
)
(
*
logs
.
Entry
,
error
)
}
type
entryEncoder
struct
{
*
zapcore
.
MapObjectEncoder
}
func
NewEntryEncoder
()
Encoder
{
return
&
entryEncoder
{
MapObjectEncoder
:
zapcore
.
NewMapObjectEncoder
()}
}
func
(
enc
*
entryEncoder
)
Clone
()
Encoder
{
return
enc
.
clone
()
}
func
(
enc
*
entryEncoder
)
clone
()
*
entryEncoder
{
objEnc
:=
zapcore
.
NewMapObjectEncoder
()
maps
.
Copy
(
objEnc
.
Fields
,
enc
.
MapObjectEncoder
.
Fields
)
return
&
entryEncoder
{
MapObjectEncoder
:
objEnc
}
}
func
(
enc
*
entryEncoder
)
EncodeEntry
(
entry
zapcore
.
Entry
,
fields
[]
zapcore
.
Field
)
(
*
logs
.
Entry
,
error
)
{
clone
:=
enc
.
clone
()
for
i
:=
range
fields
{
fields
[
i
]
.
AddTo
(
clone
)
}
ent
:=
&
logs
.
Entry
{
ID
:
id
.
GenerateNewID
(),
Timestamp
:
entry
.
Time
,
LogLevel
:
logs
.
Level
(
entry
.
Level
),
Message
:
entry
.
Message
,
}
ent
.
Category
,
_
=
clone
.
Fields
[
"category"
]
.
(
string
)
ent
.
Component
,
_
=
clone
.
Fields
[
"component"
]
.
(
string
)
ent
.
Event
,
_
=
clone
.
Fields
[
"event"
]
.
(
string
)
ent
.
ObjectID
,
_
=
clone
.
Fields
[
"object"
]
.
(
*
oid
.
ObjectId
)
ent
.
CallerID
,
_
=
clone
.
Fields
[
"caller"
]
.
(
*
oid
.
ObjectId
)
ent
.
Attr
=
clone
.
Fields
[
"attr"
]
if
err
,
_
:=
clone
.
Fields
[
"error"
]
.
(
error
);
err
!=
nil
{
ent
.
Message
=
fmt
.
Sprintf
(
"%s. Error: %s"
,
ent
.
Message
,
err
.
Error
())
}
if
tags
,
ok
:=
clone
.
Fields
[
"tags"
]
.
([]
any
);
ok
{
for
i
:=
range
tags
{
if
tag
,
ok
:=
tags
[
i
]
.
(
string
);
ok
{
ent
.
Tags
=
append
(
ent
.
Tags
,
tag
)
}
}
}
return
ent
,
nil
}
This diff is collapsed.
Click to expand it.
logs/zap/entry_encoder_test.go
0 → 100644
+
1
−
0
View file @
d5f28920
package
zap
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