good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
J
jrpc
Manage
Activity
Members
Labels
Plan
Issues
6
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
4
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
open
jrpc
Commits
84a18f86
Commit
84a18f86
authored
2 years ago
by
a
Browse files
Options
Downloads
Patches
Plain Diff
better middleware
parent
cb54b1c9
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
middleware/log.go
+24
-7
24 additions, 7 deletions
middleware/log.go
with
24 additions
and
7 deletions
middleware/log.go
+
24
−
7
View file @
84a18f86
package
middleware
// Ported from Goji's middleware, source:
// jrpcs://github.com/zenazn/goji/tree/master/web/middleware
import
(
"context"
"time"
"gfx.cafe/open/jrpc"
"tuxpa.in/a/zlog"
"tuxpa.in/a/zlog/log"
)
// Key to use when setting the request ID.
type
ctxKeyLogger
int
// RequestIDKey is the key that holds the unique request ID in a request context.
const
LoggerKey
ctxKeyLogger
=
76
func
Logger
(
next
jrpc
.
Handler
)
jrpc
.
Handler
{
fn
:=
func
(
w
jrpc
.
ResponseWriter
,
r
*
jrpc
.
Request
)
{
start
:=
time
.
Now
()
next
.
ServeRPC
(
w
,
r
)
log
.
Trace
()
.
Stringer
(
"time"
,
time
.
Since
(
start
))
.
l
:=
log
.
Trace
()
.
Str
(
"id"
,
GetReqID
(
r
.
Context
()))
.
Str
(
"remote"
,
r
.
Remote
())
.
Str
(
"method"
,
r
.
Method
)
.
Str
(
"params"
,
string
(
r
.
Msg
()
.
Params
))
.
Msg
(
"RPC Request"
)
Str
(
"params"
,
string
(
r
.
Msg
()
.
Params
))
next
.
ServeRPC
(
w
,
r
.
WithContext
(
context
.
WithValue
(
r
.
Context
(),
LoggerKey
,
l
)))
l
=
l
.
Stringer
(
"dur"
,
time
.
Since
(
start
))
l
.
Msg
(
"RPC Request"
)
}
return
jrpc
.
HandlerFunc
(
fn
)
}
func
GetLogger
(
ctx
context
.
Context
)
*
zlog
.
Event
{
if
ctx
==
nil
{
return
log
.
Debug
()
}
if
lgr
,
ok
:=
ctx
.
Value
(
LoggerKey
)
.
(
*
zlog
.
Event
);
ok
{
return
lgr
}
return
log
.
Debug
()
}
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