Example #1
0
// AccessLog is access log middleware
//
// Example:
// Jan _2 15:04:05.000000000: "GET /api/v1/user HTTP/1.1" [200] 1024
// 	Content-Type: application/json
// 	Access-Token: token
//  {"json_key":"json_value"}
func AccessLog(logger traceLogger, concernedHeaders ...string) gin.HandlerFunc {
	return func(c *gin.Context) {
		logid := getLogid(c)

		c.Next()

		req := c.Request
		logFormat := `%s: "%s %s %s" [%d] %d`
		args := []interface{}{
			logid,
			req.Method, req.URL, req.Proto,
			c.Writer.Status(), c.Writer.Size(),
		}

		s := stringer.New()
		s.FormatPrefix = "\t"
		existHeaders := false
		for _, key := range concernedHeaders {
			if header := req.Header.Get(key); header != "" {
				existHeaders = true
				s.Add(key, header)
			}
		}

		if existHeaders {
			logFormat += "\n%s"
			args = append(args, s.String())
		}

		// log request body if method is POST PUT
		switch req.Method {
		case "POST", "PUT":
			body, _ := ioutil.ReadAll(req.Body)
			logFormat += "\n%s"
			args = append(args, body)
		}

		logger.Tracef(logFormat, args...)
	}
}
Example #2
0
func (a Authtoken) String() string {
	s := stringer.New()
	s.Add("username", a.Username)
	s.Add("email", a.Email)
	return s.String()
}