github.com/annchain/OG@v0.0.9/rpc/logger.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package rpc
    15  
    16  import (
    17  	"fmt"
    18  	math2 "github.com/annchain/OG/arefactor/common/math"
    19  	"github.com/gin-gonic/gin"
    20  	"github.com/sirupsen/logrus"
    21  	"sync/atomic"
    22  	"time"
    23  )
    24  
    25  var requestId uint32
    26  
    27  func getRequestId() uint32 {
    28  	if requestId > math2.MaxUint32-1000 {
    29  		requestId = 10
    30  	}
    31  	return atomic.AddUint32(&requestId, 1)
    32  }
    33  
    34  // defaultLogFormatter is the default log format function Logger middleware uses.
    35  var ginLogFormatter = func(param gin.LogFormatterParams) string {
    36  	if logrus.GetLevel() < logrus.TraceLevel {
    37  		return ""
    38  	}
    39  	var statusColor, methodColor, resetColor string
    40  	if param.IsOutputColor() {
    41  		statusColor = param.StatusCodeColor()
    42  		methodColor = param.MethodColor()
    43  		resetColor = param.ResetColor()
    44  	}
    45  
    46  	if param.Latency > time.Minute {
    47  		// Truncate in a golang < 1.8 safe way
    48  		param.Latency = param.Latency - param.Latency%time.Second
    49  	}
    50  
    51  	logEntry := fmt.Sprintf("GIN %v %s %3d %s %13v  %15s %s %-7s %s %s %s  id_%d",
    52  		param.TimeStamp.Format("2006/01/02 - 15:04:05"),
    53  		statusColor, param.StatusCode, resetColor,
    54  		param.Latency,
    55  		param.ClientIP,
    56  		methodColor, param.Method, resetColor,
    57  		param.Path,
    58  		param.ErrorMessage,
    59  		getRequestId(),
    60  	)
    61  	logrus.Tracef("gin log %v ", logEntry)
    62  	//return  logEntry
    63  	return ""
    64  }