// GinLogger is the logrus logger middleware
GinLogger is the logrus logger middleware
// GinLogger is the logrus logger middleware
func GinLogger() gin.HandlerFunc {
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "unknow"
	}
	return func(c *gin.Context) {
		path := c.Request.URL.Path
		start := time.Now()
		c.Next()
		stop := time.Since(start)
		latency := int(math.Ceil(float64(stop.Nanoseconds()) / 1000000.0))
		statusCode := c.Writer.Status()
		clientIP := c.ClientIP()
		method := c.Request.Method
		clientUserAgent := c.Request.UserAgent()
		referer := c.Request.Referer()
		dataLength := c.Writer.Size()
		statusColor := colorForStatus(statusCode)
		methodColor := colorForMethod(method)
		if dataLength < 0 {
			dataLength = 0
		}
		if viper.GetString("runmode") == "debug" {
			msg := fmt.Sprintf("[GIN] %s |%s  %s %3d| %s  %s %-7s %s | size: %d | %s | (%dms)\n",
				clientIP,
				statusColor, reset, statusCode,
				methodColor, reset, method,
				path,
				dataLength,
				clientUserAgent,
				latency)
			if len(c.Errors) > 0 {
				common.Logger.Error(msg, c.Errors.ByType(gin.ErrorTypePrivate).String())
			} else {
				if statusCode > 499 {
					common.Logger.Error(msg)
				} else if statusCode > 399 {
					common.Logger.Warn(msg)
				} else {
					common.Logger.Info(msg)
				}
			}
			return
		}
		entry := common.Logger.WithFields(logrus.Fields{
			"hostname":   hostname,
			"statusCode": statusCode,
			"latency":    latency, 
			"clientIP":   clientIP,
			"method":     c.Request.Method,
			"path":       path,
			"referer":    referer,
			"dataLength": dataLength,
			"userAgent":  clientUserAgent,
		})
		if len(c.Errors) > 0 {
			entry.Errorln(c.Errors.ByType(gin.ErrorTypePrivate).String())
		} else {
			if statusCode > 499 {
				entry.Error("\n")
			} else if statusCode > 399 {
				entry.Warn("\n")
			} else {
				entry.Info("\n")
			}
		}
	}
}
func GinLogger ( ) gin . HandlerFunc { hostname , err := os . Hostname ( ) if err != nil { hostname = " " } return func ( c * gin . Context ) { path := c . Request . URL . Path start := time . Now ( ) c . Next ( ) stop := time . Since ( start ) latency := int ( math . Ceil ( float64 ( stop . Nanoseconds ( ) ) / 1000000.0 ) ) statusCode := c . Writer . Status ( ) clientIP := c . ClientIP ( ) method := c . Request . Method clientUserAgent := c . Request . UserAgent ( ) referer := c . Request . Referer ( ) dataLength := c . Writer . Size ( ) statusColor := colorForStatus ( statusCode ) methodColor := colorForMethod ( method ) if dataLength < 0 { dataLength = 0 } if viper . GetString ( " " ) == " " { msg := fmt . Sprintf ( " \n " , clientIP , statusColor , reset , statusCode , methodColor , reset , method , path , dataLength , clientUserAgent , latency ) if len ( c . Errors ) > 0 { common . Logger . Error ( msg , c . Errors . ByType ( gin . ErrorTypePrivate ) . String ( ) ) } else { if statusCode > 499 { common . Logger . Error ( msg ) } else if statusCode > 399 { common . Logger . Warn ( msg ) } else { common . Logger . Info ( msg ) } } return } entry := common . Logger . WithFields ( logrus . Fields { " " : hostname , " " : statusCode , " " : latency , " " : clientIP , " " : c . Request . Method , " " : path , " " : referer , " " : dataLength , " " : clientUserAgent , } ) if len ( c . Errors ) > 0 { entry . Errorln ( c . Errors . ByType ( gin . ErrorTypePrivate ) . String ( ) ) } else { if statusCode > 499 { entry . Error ( " \n " ) } else if statusCode > 399 { entry . Warn ( " \n " ) } else { entry . Info ( " \n " ) } } } }
GinLogger is the logrus logger middleware
GinLogger is the logrus logger middleware
// other handler can change c.Path so: // time to process