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
930bb7a5
Commit
930bb7a5
authored
1 year ago
by
ensiouel
Browse files
Options
Downloads
Patches
Plain Diff
add custom fields
parent
e0b4b8f3
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
+56
-0
56 additions, 0 deletions
pkg/log/zap/field.go
with
56 additions
and
0 deletions
pkg/log/zap/field.go
0 → 100644
+
56
−
0
View file @
930bb7a5
package
zap
import
(
"context"
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const
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
)
}
func
Object
(
v
any
)
zapcore
.
Field
{
switch
object
:=
v
.
(
type
)
{
case
string
:
return
zap
.
String
(
"object"
,
object
)
case
fmt
.
Stringer
:
return
zap
.
String
(
"object"
,
object
.
String
())
default
:
return
zap
.
Any
(
"object"
,
object
)
}
}
type
callerKey
struct
{}
func
ContextWithCaller
(
parent
context
.
Context
,
caller
string
)
context
.
Context
{
return
context
.
WithValue
(
parent
,
callerKey
{},
caller
)
}
func
CallerFromContext
(
ctx
context
.
Context
)
zapcore
.
Field
{
caller
,
ok
:=
ctx
.
Value
(
callerKey
{})
.
(
string
)
if
!
ok
{
caller
=
unknownCaller
}
return
zap
.
String
(
"caller"
,
caller
)
}
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