Giter VIP home page Giter VIP logo

fiberlogrus's Introduction

Logrus logger middleware Mentioned in Awesome Fiber

Logger middleware for Fiber that logs HTTP request/response details.

Use your configured logrus logger instance or global logrus instance to handle logging in a structured way.

Table of Contents

Getting started

$ go get github.com/mikhail-bigun/fiberlogrus

Signatures

func New(config ...Config) fiber.Handler

Examples

Import required packages

import (
	"github.com/gofiber/fiber/v2"
	"github.com/sirupsen/logrus"
	"github.com/mikhail-bigun/fiberlogrus"
)

Default config

Using with a default config, it will call global logrus instance to log the requests

app := fiber.New()

app.Use(fiberlogrus.New())
// ConfigDefault is the default config
var ConfigDefault Config = Config{
	Logger: nil,
	Tags: []string{
		TagStatus,
		TagLatency,
		TagMethod,
		TagPath,
	},
}

Use logger instance and configure tags

logger := logrus.New()
// you can also provide logger with a desired formatter
// logger.SetFormatter(&logrus.JSONFormatter{})

app.Use(
	fiberlogrus.New(
		fiberlogrus.Config{
			Logger: logger,
			Tags: []string{
				// add method field
				fiberlogrus.TagMethod,
				// add status field
				fiberlogrus.TagStatus,
				// add value from locals
				AttachKeyTag(TagLocals, "requestid"),
				// add certain header
				AttachKeyTag(TagReqHeader, "custom-header"),
			},
		},
	),
)

All supported common tags example

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/sirupsen/logrus"
	"github.com/mikhail-bigun/fiberlogrus"
)

func main() {
	app := fiber.New()

	logger := logrus.New()
	logger.SetFormatter(&logrus.JSONFormatter{PrettyPrint: true})

	app.Use(
		fiberlogrus.New(
			fiberlogrus.Config{
				Logger: logger,
				Tags: fiberlogrus.CommonTags,
			}))
	
	app.Get("/", func(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) })
	logger.Fatal(f.Listen(":8080"))
}

Supported tags

Common

// Common Tags
const (
	// request referer
	TagReferer = "referer"
	// request protocol
	TagProtocol = "protocol"
	// request port
	TagPort = "port"
	// request ip
	TagIP = "ip"
	// request ips
	TagIPs = "ips"
	// request host
	TagHost = "host"
	// request path
	TagPath = "path"
	// request url
	TagURL = "url"
	// request user-agent
	TagUA = "ua"
	// request body
	TagBody = "body"
	// request body bytes length
	TagBytesReceived = "bytesReceived"
	// response bytes length
	TagBytesSent = "bytesSent"
	// request route
	TagRoute = "route"
	// response body
	TagResBody = "resBody"
	// request headers
	TagReqHeaders = "reqHeaders"
	// request query parameters
	TagQueryStringParams = "queryParams"
	// response status
	TagStatus = "status"
	// request method
	TagMethod = "method"
	// fiber process id
	TagPid = "pid"
	// request latency
	TagLatency = "latency"
)

Key

// Key Tags
const (
	// request specified header
	TagReqHeader = "reqHeader"
	// response specified header
	TagRespHeader = "respHeader"
	// request specified query
	TagQuery = "query"
	// request specified form value
	TagForm = "form"
	// request specified cookie value
	TagCookie = "cookie"
	// request specified locals value
	TagLocals = "locals"
)

fiberlogrus's People

Contributors

ginakira avatar lheinlen avatar mikhail-bigun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

fiberlogrus's Issues

0.1.3 tag missing v prefix

The tag for version 0.1.3 is missing the v prefix which will, I believe, prevent go mod from finding it automatically.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.