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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
378c7013
Commit
378c7013
authored
1 year ago
by
Semyon Krestyaninov
Browse files
Options
Downloads
Patches
Plain Diff
Revert "remove field file"
This reverts commit
39fa53b8
.
parent
39fa53b8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/log/zap/field.go
+79
-0
79 additions, 0 deletions
pkg/log/zap/field.go
with
79 additions
and
0 deletions
pkg/log/zap/field.go
0 → 100644
+
79
−
0
View file @
378c7013
package
zap
import
(
"context"
"fmt"
"git.perx.ru/perxis/perxis-go/id"
"git.perx.ru/perxis/perxis-go/pkg/auth"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const
(
unknownObject
=
"unknown"
unknownCaller
=
"unknown"
)
func
Category
(
category
string
)
zapcore
.
Field
{
return
zap
.
String
(
"category"
,
category
)
}
func
Component
(
component
string
)
zapcore
.
Field
{
return
zap
.
String
(
"component"
,
component
)
}
func
Event
(
event
string
)
zapcore
.
Field
{
return
zap
.
String
(
"event"
,
event
)
}
// Object возвращает поле и устанавливает передаваемый аргумент в качестве идентификатора объекта в формате ObjectID.
// Поддерживаемые типы: string, fmt.Stringer.
// Если передан аргумент другого типа, будет произведена попытка привести переданное значение к ObjectID.
func
Object
(
v
any
)
zapcore
.
Field
{
var
object
=
unknownObject
switch
value
:=
v
.
(
type
)
{
case
string
:
object
=
value
case
fmt
.
Stringer
:
object
=
value
.
String
()
default
:
oid
,
err
:=
id
.
FromObject
(
v
)
if
err
==
nil
{
object
=
oid
.
String
()
}
}
return
zap
.
String
(
"object"
,
object
)
}
// Caller возвращает поле и устанавливает передаваемый аргумент в качестве "вызывающего" в формате ObjectID.
// Поддерживаемые типы: string, fmt.Stringer.
// Если передан аргумент другого типа, будет произведена попытка привести переданное значение к ObjectID.
func
Caller
(
v
any
)
zapcore
.
Field
{
var
caller
=
unknownCaller
switch
value
:=
v
.
(
type
)
{
case
string
:
caller
=
value
case
fmt
.
Stringer
:
caller
=
value
.
String
()
default
:
oid
,
err
:=
id
.
FromObject
(
v
)
if
err
==
nil
{
caller
=
oid
.
String
()
}
}
return
zap
.
String
(
"caller"
,
caller
)
}
// CallerFromContext извлекает auth.Principal из контекста и устанавливает его в качестве "вызывающего" в формате ObjectID.
func
CallerFromContext
(
ctx
context
.
Context
)
zapcore
.
Field
{
return
Caller
(
auth
.
GetPrincipal
(
ctx
))
}
func
Attr
(
attr
any
)
zapcore
.
Field
{
return
zap
.
Any
(
"attr"
,
attr
)
}
func
Tags
(
tags
...
string
)
zapcore
.
Field
{
return
zap
.
Strings
(
"tags"
,
tags
)
}
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