Esempio n. 1
0
func (c *Context) Log(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
	c.Job = stream.NewJob(r.RoutePath())

	id, err := uuid.NewV4()
	if err == nil {
		c.Job.KeyValue("request-id", id.String())
	}

	path := r.URL.Path
	c.Job.EventKv("api.request", health.Kvs{"path": path})

	next(rw, r)

	code := rw.StatusCode()
	kvs := health.Kvs{
		"code": fmt.Sprint(code),
		"path": path,
	}

	// Map HTTP status code to category.
	var status health.CompletionStatus
	if c.Panic {
		status = health.Panic
	} else if code < 400 {
		status = health.Success
	} else if code == 422 {
		status = health.ValidationError
	} else if code < 500 {
		status = health.Junk // 404, 401
	} else {
		status = health.Error
	}
	c.Job.CompleteKv(status, kvs)
}
Esempio n. 2
0
func (c *apiContext) HealthMiddleware(rw web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
	c.Job = c.hd.stream.NewJob(r.RoutePath())

	path := r.URL.Path
	c.EventKv("starting_request", health.Kvs{"path": path})

	next(rw, r)

	code := rw.StatusCode()
	kvs := health.Kvs{
		"code": fmt.Sprint(code),
		"path": path,
	}

	// Map HTTP status code to category.
	var status health.CompletionStatus
	// if c.Panic {
	// 	status = health.Panic
	// } else
	if code < 400 {
		status = health.Success
	} else if code == 422 {
		status = health.ValidationError
	} else if code < 500 {
		status = health.Junk // 404, 401
	} else {
		status = health.Error
	}
	c.CompleteKv(status, kvs)
}
Esempio n. 3
0
func LogRequest(rw web.ResponseWriter, req *web.Request, startTime time.Time) {

	duration, durationUnits := ProcessRequestTime(startTime)

	if rw.StatusCode() > 499 {
		log.WithFields(log.Fields{
			"type": "request",
			//"req":      req,
			"status":   rw.StatusCode(),
			"duration": fmt.Sprintf("%d%s", duration, durationUnits),
		}).Errorf("%d %s %s", rw.StatusCode(), req.Method, req.URL.Path)
	} else {
		log.WithFields(log.Fields{
			"type": "request",
			//"req":      req,
			"status":   rw.StatusCode(),
			"duration": fmt.Sprintf("%d%s", duration, durationUnits),
		}).Infof("%d %s %s", rw.StatusCode(), req.Method, req.URL.Path)
	}
}
Esempio n. 4
0
File: api.go Progetto: ahjdzx/dis
// LogMiddleware is generic middleware that will log requests to Logger.
func LogMiddleware(rw web.ResponseWriter, req *web.Request, next web.NextMiddlewareFunc) {
	startTime := time.Now()

	next(rw, req)

	duration := time.Since(startTime).Nanoseconds()
	var durationUnits string
	switch {
	case duration > 2000000:
		durationUnits = "ms"
		duration /= 1000000
	case duration > 1000:
		durationUnits = "μs"
		duration /= 1000
	default:
		durationUnits = "ns"
	}

	log.Infof("[%d %s] %d '%s'", duration, durationUnits, rw.StatusCode(), req.URL.Path)
}