mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
http logger add panic.
This commit is contained in:
parent
6026ab8a27
commit
c2d2557704
@ -17,11 +17,10 @@ type logFormatter struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
|
func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
|
||||||
e := logEntry{logger: l.logger}
|
req := map[string]interface{}{}
|
||||||
e.req.Time = time.Now()
|
|
||||||
|
|
||||||
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
|
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
|
||||||
e.req.Id = reqID
|
req["id"] = reqID
|
||||||
}
|
}
|
||||||
|
|
||||||
scheme := "http"
|
scheme := "http"
|
||||||
@ -29,37 +28,38 @@ func (l *logFormatter) NewLogEntry(r *http.Request) middleware.LogEntry {
|
|||||||
scheme = "https"
|
scheme = "https"
|
||||||
}
|
}
|
||||||
|
|
||||||
e.req.Scheme = scheme
|
req["scheme"] = scheme
|
||||||
e.req.Proto = r.Proto
|
req["proto"] = r.Proto
|
||||||
e.req.Method = r.Method
|
req["method"] = r.Method
|
||||||
e.req.Remote = r.RemoteAddr
|
req["remote"] = r.RemoteAddr
|
||||||
e.req.Agent = r.UserAgent()
|
req["agent"] = r.UserAgent()
|
||||||
e.req.Uri = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
|
req["uri"] = fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)
|
||||||
return &e
|
|
||||||
|
return &logEntry{
|
||||||
|
logger: l.logger.With().Interface("req", req).Logger(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type logEntry struct {
|
type logEntry struct {
|
||||||
req struct {
|
|
||||||
Time time.Time
|
|
||||||
Id string
|
|
||||||
Scheme string
|
|
||||||
Proto string
|
|
||||||
Method string
|
|
||||||
Remote string
|
|
||||||
Agent string
|
|
||||||
Uri string
|
|
||||||
}
|
|
||||||
res struct {
|
|
||||||
Time time.Time
|
|
||||||
Code int
|
|
||||||
Bytes int
|
|
||||||
}
|
|
||||||
err error
|
|
||||||
session *types.Session
|
|
||||||
logger zerolog.Logger
|
logger zerolog.Logger
|
||||||
|
err error
|
||||||
|
panic *logPanic
|
||||||
|
session *types.Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *logEntry) SetError(err error) {
|
type logPanic struct {
|
||||||
|
message string
|
||||||
|
stack string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *logEntry) Panic(v interface{}, stack []byte) {
|
||||||
|
e.panic = &logPanic{
|
||||||
|
message: fmt.Sprintf("%+v", v),
|
||||||
|
stack: string(stack),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *logEntry) Error(err error) {
|
||||||
e.err = err
|
e.err = err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,24 +68,33 @@ func (e *logEntry) SetSession(session types.Session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
|
||||||
e.res.Time = time.Now()
|
res := map[string]interface{}{}
|
||||||
e.res.Code = status
|
res["time"] = time.Now().UTC().Format(time.RFC1123)
|
||||||
e.res.Bytes = bytes
|
res["status"] = status
|
||||||
|
res["bytes"] = bytes
|
||||||
|
res["elapsed"] = float64(elapsed.Nanoseconds()) / 1000000.0
|
||||||
|
|
||||||
logger := e.logger.With().
|
logger := e.logger.With().Interface("res", res).Logger()
|
||||||
Float64("elapsed", float64(elapsed.Nanoseconds())/1000000.0).
|
|
||||||
Interface("req", e.req).
|
|
||||||
Interface("res", e.res).
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
|
// add session ID to logs (if exists)
|
||||||
if e.session != nil {
|
if e.session != nil {
|
||||||
logger = logger.With().Str("session_id", (*e.session).ID()).Logger()
|
logger = logger.With().Str("session_id", (*e.session).ID()).Logger()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle panic error message
|
||||||
|
if e.panic != nil {
|
||||||
|
logger.WithLevel(zerolog.PanicLevel).
|
||||||
|
Err(e.err).
|
||||||
|
Str("stack", e.panic.stack).
|
||||||
|
Msgf("request failed (%d): %s", status, e.panic.message)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle panic error message
|
||||||
if e.err != nil {
|
if e.err != nil {
|
||||||
httpErr, ok := e.err.(*utils.HTTPError)
|
httpErr, ok := e.err.(*utils.HTTPError)
|
||||||
if !ok {
|
if !ok {
|
||||||
logger.Err(e.err).Msgf("request failed (%d)", e.res.Code)
|
logger.Err(e.err).Msgf("request failed (%d)", status)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,11 +102,11 @@ func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Dur
|
|||||||
httpErr.Message = http.StatusText(httpErr.Code)
|
httpErr.Message = http.StatusText(httpErr.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
var logEvent *zerolog.Event
|
var logLevel zerolog.Level
|
||||||
if httpErr.Code < 500 {
|
if httpErr.Code < 500 {
|
||||||
logEvent = logger.Warn()
|
logLevel = zerolog.WarnLevel
|
||||||
} else {
|
} else {
|
||||||
logEvent = logger.Error()
|
logLevel = zerolog.ErrorLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
message := httpErr.Message
|
message := httpErr.Message
|
||||||
@ -105,16 +114,9 @@ func (e *logEntry) Write(status, bytes int, header http.Header, elapsed time.Dur
|
|||||||
message = httpErr.InternalMsg
|
message = httpErr.InternalMsg
|
||||||
}
|
}
|
||||||
|
|
||||||
logEvent.Err(httpErr.InternalErr).Msgf("request failed (%d): %s", e.res.Code, message)
|
logger.WithLevel(logLevel).Err(httpErr.InternalErr).Msgf("request failed (%d): %s", status, message)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug().Msgf("request complete (%d)", e.res.Code)
|
logger.Debug().Msgf("request complete (%d)", status)
|
||||||
}
|
|
||||||
|
|
||||||
func (e *logEntry) Panic(v interface{}, stack []byte) {
|
|
||||||
e.logger.WithLevel(zerolog.PanicLevel).
|
|
||||||
Interface("req", e.req).
|
|
||||||
Str("stack", string(stack)).
|
|
||||||
Msgf("request panic (500): %+v", v)
|
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ func routeHandler(fn types.RouterHandler) http.HandlerFunc {
|
|||||||
logEntry, _ := r.Context().Value(middleware.LogEntryCtxKey).(*logEntry)
|
logEntry, _ := r.Context().Value(middleware.LogEntryCtxKey).(*logEntry)
|
||||||
|
|
||||||
if err := fn(w, r); err != nil {
|
if err := fn(w, r); err != nil {
|
||||||
logEntry.SetError(err)
|
logEntry.Error(err)
|
||||||
errorHandler(err, w, r)
|
errorHandler(err, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ func middlewareHandler(fn types.MiddlewareHandler) func(http.Handler) http.Handl
|
|||||||
|
|
||||||
ctx, err := fn(w, r)
|
ctx, err := fn(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logEntry.SetError(err)
|
logEntry.Error(err)
|
||||||
errorHandler(err, w, r)
|
errorHandler(err, w, r)
|
||||||
|
|
||||||
// set session if exits
|
// set session if exits
|
||||||
|
Loading…
Reference in New Issue
Block a user