gin_logrus

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 4 Imported by: 0

README

Gin-Logrus

gin-logrus is a gin middleware implemented using logrus

Installation

go get github.com/RuiFG/gin-logrus

Simple Example

func main() {
	engine := gin.New()
	engine.Use(gin_logrus.Logger())
	engine.GET("", func(context *gin.Context) {
		context.Status(200)
	})
	_ = engine.Run("127.0.0.1:8080")
}

Customize

You can also define the implementation of LogTransformer yourself

func main() {
	logger := logrus.New()
	engine := gin.New()
	engine.Use(gin_logrus.LoggerWithConfig(gin_logrus.LoggerConfig{
		Logger: logger, 
		Formatter: func(logger *logrus.Logger, params gin_logrus.FieldsParams) {
		//noting to do
	}}))
	engine.GET("", func(context *gin.Context) {
		context.Status(200)
	})
	_ = engine.Run("127.0.0.1:8080")
}

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger() gin.HandlerFunc

func LoggerWithConfig

func LoggerWithConfig(config LoggerConfig) gin.HandlerFunc

Types

type FieldsParams

type FieldsParams struct {
	Request *http.Request

	// TimeStamp shows the time after the server returns a response.
	TimeStamp time.Time
	// StatusCode is HTTP response code.
	StatusCode int
	// Latency is how much time the server cost to process a certain request.
	Latency time.Duration
	// ClientIP equals Context's ClientIP method.
	ClientIP string
	// Method is the HTTP method given to the request.
	Method string
	// Path is a path the client requests.
	Path string
	// ErrorMessage is set if error has occurred in processing the request.
	ErrorMessage string
	// BodySize is the size of the Response Body
	BodySize int
	// Keys are the keys set on the request's context.
	Keys map[string]interface{}

	Option OptionalFieldsParams
}

FieldsParams is the logrus Fields params·

type LogTransformer

type LogTransformer func(logger *logrus.Logger, params FieldsParams)

type LoggerConfig

type LoggerConfig struct {
	Logger    *logrus.Logger
	Formatter LogTransformer
	SkipPaths []string
	Option    OptionalFieldsParams
}

LoggerConfig defines the config for Logger middleware.

type OptionalFieldsParams

type OptionalFieldsParams struct {
	//Optional fields
	Host      bool
	Referer   bool
	UserAgent bool
	Header    bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL