fiberlog

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2023 License: MIT Imports: 8 Imported by: 2

README

fiberlog

godoc

HTTP request/response logger for Fiber using zerolog.

Install
go get -u github.com/gofiber/fiber/v2
go get -u github.com/msaf1980/fiberlog
Usage
package main

import (
  "github.com/gofiber/fiber/v2"
  "github.com/msaf1980/fiberlog"
)

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

  // Default
  app.Use(fiberlog.New())

  // Custom Config
  app.Use(fiberlog.New(fiberlog.Config{
    Logger: &zerolog.New(os.Stdout),
    Next: func(ctx *fiber.Ctx) bool {
      // skip if we hit /private
      return ctx.Path() == "/private"
    },
  }))

  app.Listen(3000)
}

If we need log addional fields, it's possible

package main

import (
  "github.com/gofiber/fiber/v2"
  "github.com/msaf1980/fiberlog"
)

func main() {
  logger := zerolog.New(os.Stdout)
  app := fiber.New()

  // Default
  app.Use(fiberlog.New())

  // basic auth
  app.Use(basicauth.New(basicauth.Config{
    Users: map[string]string{
      "test":  "password",
      "admin": "123456",
    },
  }))

  // Custom Config
  app.Use(fiberlog.New(fiberlog.Config{
    Logger: &logger,
    Next: func(ctx *fiber.Ctx) bool {
      // skip if we hit /private
      return ctx.Path() == "/private"
    },
    // TagReqHeader:  []string{},
    TagRespHeader:   []string{"content-type"},
    LogUsername:     "username",
    LogUserAgent:    true,
    LogForwardedFor: true,
    // store it in handler like c.Context().SetUserValue("test", "test_value")
    Tags:            []string{"test"},
  }))

  logger.Fatal().Err(app.Listen(":3000"))
}
Example

Run app server:

$ go run example/main.go

Test request:

$ curl http://localhost:3000/ok
$ curl -u test:password http://localhost:3000/basic
$ curl http://localhost:3000/warn
$ curl http://localhost:3000/err

screen

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

New is a zerolog middleware that allows you to pass a Config.

app := fiber.New()

// Without config
app.Use(New())

// With config
app.Use(New(Config{Logger: &zerolog.New(os.Stdout)}))

Types

type Config

type Config struct {
	// Next defines a function to skip this middleware.
	Next func(ctx *fiber.Ctx) bool

	// Logger is a *zerolog.Logger that writes the logs.
	//
	// Default: log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
	Logger *zerolog.Logger

	TimeLayout      string
	LogUsername     string // log from context username parameter (by default set to 'username'
	LogUserAgent    bool   // log user agent
	LogForwardedFor bool   // log X-Forwarded-For (if behing a balancer) or repote IP
	LogHost         bool   // log incommig host (in request)
	TagReqHeader    []string
	TagRespHeader   []string
	Tags            []string // log from context user parameter with keys from tags slice
}

Config defines the config for logger middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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