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
b4f66a55
Commit
b4f66a55
authored
2 years ago
by
a
Browse files
Options
Downloads
Patches
Plain Diff
first reles
parent
88b3d8bd
Loading
Loading
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
middleware/logger.go
+0
-171
0 additions, 171 deletions
middleware/logger.go
with
0 additions
and
171 deletions
middleware/logger.go
deleted
100644 → 0
+
0
−
171
View file @
88b3d8bd
package
middleware
// from
// https://github.com/go-chi/chi
import
(
"bytes"
"context"
"log"
"net/http"
"os"
"runtime"
"time"
"gfx.cafe/open/jrpc"
)
var
(
// LogEntryCtxKey is the context.Context key to store the request log entry.
LogEntryCtxKey
=
&
contextKey
{
"LogEntry"
}
// DefaultLogger is called by the Logger middleware handler to log each request.
// Its made a package-level variable so that it can be reconfigured for custom
// logging configurations.
DefaultLogger
func
(
next
jrpc
.
Handler
)
jrpc
.
Handler
)
// Logger is a middleware that logs the start and end of each request, along
// with some useful data about what was requested, what the response status was,
// and how long it took to return. When standard output is a TTY, Logger will
// print in color, otherwise it will print in black and white. Logger prints a
// request ID if one is provided.
//
// IMPORTANT NOTE: Logger should go before any other middleware that may change
// the response, such as middleware.Recoverer. Example:
// r := chi.NewRouter()
// r.Use(middleware.Logger) // <--<< Logger should come before Recoverer
// r.Use(middleware.Recoverer)
// r.Get("/", handler)
func
Logger
(
next
jrpc
.
Handler
)
jrpc
.
Handler
{
return
DefaultLogger
(
next
)
}
// RequestLogger returns a logger handler using a custom LogFormatter.
func
RequestLogger
(
f
LogFormatter
)
func
(
next
jrpc
.
Handler
)
jrpc
.
Handler
{
return
func
(
next
jrpc
.
Handler
)
jrpc
.
Handler
{
fn
:=
func
(
w
jrpc
.
ResponseWriter
,
r
*
jrpc
.
Request
)
{
entry
:=
f
.
NewLogEntry
(
r
)
ww
:=
NewWrapResponseWriter
(
w
,
r
.
ProtoMajor
)
t1
:=
time
.
Now
()
defer
func
()
{
entry
.
Write
(
ww
.
Status
(),
ww
.
BytesWritten
(),
ww
.
Header
(),
time
.
Since
(
t1
),
nil
)
}()
next
.
ServeRPC
(
ww
,
WithLogEntry
(
r
,
entry
))
}
return
jrpc
.
HandlerFunc
(
fn
)
}
}
// LogFormatter initiates the beginning of a new LogEntry per request.
// See DefaultLogFormatter for an example implementation.
type
LogFormatter
interface
{
NewLogEntry
(
r
*
jrpc
.
Request
)
LogEntry
}
// LogEntry records the final log when a request completes.
// See defaultLogEntry for an example implementation.
type
LogEntry
interface
{
Write
(
status
,
bytes
int
,
elapsed
time
.
Duration
,
extra
interface
{})
Panic
(
v
interface
{},
stack
[]
byte
)
}
// GetLogEntry returns the in-context LogEntry for a request.
func
GetLogEntry
(
r
*
jrpc
.
Request
)
LogEntry
{
entry
,
_
:=
r
.
Context
()
.
Value
(
LogEntryCtxKey
)
.
(
LogEntry
)
return
entry
}
// WithLogEntry sets the in-context LogEntry for a request.
func
WithLogEntry
(
r
*
jrpc
.
Request
,
entry
LogEntry
)
*
jrpc
.
Request
{
r
=
r
.
WithContext
(
context
.
WithValue
(
r
.
Context
(),
LogEntryCtxKey
,
entry
))
return
r
}
// LoggerInterface accepts printing to stdlib logger or compatible logger.
type
LoggerInterface
interface
{
Print
(
v
...
interface
{})
}
// DefaultLogFormatter is a simple logger that implements a LogFormatter.
type
DefaultLogFormatter
struct
{
Logger
LoggerInterface
NoColor
bool
}
// NewLogEntry creates a new LogEntry for the request.
func
(
l
*
DefaultLogFormatter
)
NewLogEntry
(
r
*
jrpc
.
Request
)
LogEntry
{
useColor
:=
!
l
.
NoColor
entry
:=
&
defaultLogEntry
{
DefaultLogFormatter
:
l
,
request
:
r
,
buf
:
&
bytes
.
Buffer
{},
useColor
:
useColor
,
}
reqID
:=
GetReqID
(
r
.
Context
())
if
reqID
!=
""
{
cW
(
entry
.
buf
,
useColor
,
nYellow
,
"[%s] "
,
reqID
)
}
cW
(
entry
.
buf
,
useColor
,
nCyan
,
"
\"
"
)
cW
(
entry
.
buf
,
useColor
,
bMagenta
,
"%s "
,
r
.
Method
)
scheme
:=
"jrpc"
cW
(
entry
.
buf
,
useColor
,
nCyan
,
"%s://%s%s %s
\"
"
,
scheme
,
r
.
Method
(),
r
.
Params
(),
r
.
Msg
()
.
ID
)
entry
.
buf
.
WriteString
(
"from "
)
// TODO: remote addr/ real ip
// entry.buf.WriteString(r.RemoteAddr)
entry
.
buf
.
WriteString
(
" - "
)
return
entry
}
type
defaultLogEntry
struct
{
*
DefaultLogFormatter
request
*
jrpc
.
Request
buf
*
bytes
.
Buffer
useColor
bool
}
func
(
l
*
defaultLogEntry
)
Write
(
status
,
bytes
int
,
elapsed
time
.
Duration
,
extra
interface
{})
{
switch
{
case
status
<
200
:
cW
(
l
.
buf
,
l
.
useColor
,
bBlue
,
"%03d"
,
status
)
case
status
<
300
:
cW
(
l
.
buf
,
l
.
useColor
,
bGreen
,
"%03d"
,
status
)
case
status
<
400
:
cW
(
l
.
buf
,
l
.
useColor
,
bCyan
,
"%03d"
,
status
)
case
status
<
500
:
cW
(
l
.
buf
,
l
.
useColor
,
bYellow
,
"%03d"
,
status
)
default
:
cW
(
l
.
buf
,
l
.
useColor
,
bRed
,
"%03d"
,
status
)
}
cW
(
l
.
buf
,
l
.
useColor
,
bBlue
,
" %dB"
,
bytes
)
l
.
buf
.
WriteString
(
" in "
)
if
elapsed
<
500
*
time
.
Millisecond
{
cW
(
l
.
buf
,
l
.
useColor
,
nGreen
,
"%s"
,
elapsed
)
}
else
if
elapsed
<
5
*
time
.
Second
{
cW
(
l
.
buf
,
l
.
useColor
,
nYellow
,
"%s"
,
elapsed
)
}
else
{
cW
(
l
.
buf
,
l
.
useColor
,
nRed
,
"%s"
,
elapsed
)
}
l
.
Logger
.
Print
(
l
.
buf
.
String
())
}
func
(
l
*
defaultLogEntry
)
Panic
(
v
interface
{},
stack
[]
byte
)
{
PrintPrettyStack
(
v
)
}
func
init
()
{
color
:=
true
if
runtime
.
GOOS
==
"windows"
{
color
=
false
}
DefaultLogger
=
RequestLogger
(
&
DefaultLogFormatter
{
Logger
:
log
.
New
(
os
.
Stdout
,
""
,
log
.
LstdFlags
),
NoColor
:
!
color
})
}
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