all logs now have an op field

This commit is contained in:
Julien Neuhart
2019-07-07 19:54:44 +02:00
parent c8f7ea934c
commit 5f6ab2fc08
16 changed files with 240 additions and 85 deletions

View File

@@ -74,11 +74,11 @@ func (ctx *Context) WithResource(resourceDirPath string) error {
// LogRequestResult logs the result of a request.
// This method should only be used by a middleware!
func (ctx *Context) LogRequestResult(err error, isDebug bool) error {
const op = "context.LogRequestResult"
req := ctx.Request()
resp := ctx.Response()
stopTime := time.Now()
fields := map[string]interface{}{
"time_rfc3339": timeRFC3339(), // FIXME required?
"remote_ip": ctx.RealIP(),
"host": req.Host,
"uri": req.RequestURI,
@@ -93,21 +93,17 @@ func (ctx *Context) LogRequestResult(err error, isDebug bool) error {
"bytes_out": bytesOut(resp),
}
if err != nil {
ctx.logger.WithFields(fields).Error("request failed")
ctx.logger.WithFields(fields).ErrorfOp(op, "request failed")
return err
}
if isDebug {
ctx.logger.WithFields(fields).Debug("request handled")
ctx.logger.WithFields(fields).DebugfOp(op, "request handled")
return nil
}
ctx.logger.WithFields(fields).Info("request handled")
ctx.logger.WithFields(fields).InfofOp(op, "request handled")
return nil
}
func timeRFC3339() string {
return time.Now().Format(time.RFC3339)
}
func path(r *http.Request) string {
path := r.URL.Path
if path == "" {

View File

@@ -20,8 +20,9 @@ func Error() echo.MiddlewareFunc {
}
// we log the initial error before returning
// the HTTP error.
errOp := standarderror.Op(err)
logger := ctx.StandardLogger()
logger.Error(err.Error())
logger.ErrorOp(errOp, err)
// handle our custom HTTP error.
var httpErr error
errCode := standarderror.Code(err)

View File

@@ -318,7 +318,7 @@ func (r *Resource) float64(formField string, defaultValue float64) (float64, err
if err != nil {
return 0.0, &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("'%s' is not a float", formField),
Message: fmt.Sprintf("'%s' is not a float, got '%s'", formField, v),
Op: op,
}
}
@@ -338,7 +338,7 @@ func (r *Resource) bool(formField string, defaultValue bool) (bool, error) {
if err != nil {
return false, &standarderror.Error{
Code: standarderror.Invalid,
Message: fmt.Sprintf("'%s' is not a boolean", formField),
Message: fmt.Sprintf("'%s' is not a boolean, got '%s'", formField, v),
Op: op,
}
}