fiber

package module
v0.0.0-...-69ed6e6 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 3 Imported by: 0

README

log/fiber

Go Reference Go Report Card

A fiber logger handler implementation.

Usage

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/gofiber/fiber/v2"
	"github.com/phuslu/log"
	fiberlog "github.com/phuslu/log/fiber"
)

func main() {
	if log.IsTerminal(os.Stderr.Fd()) {
		log.DefaultLogger = log.Logger{
			TimeFormat: "15:04:05",
			Caller:     1,
			Writer: &log.ConsoleWriter{
				ColorOutput:    true,
				QuoteString:    true,
				EndWithMessage: true,
			},
		}
	}

	app := fiber.New()

	// Add a logger middleware, which:
	//   - Logs all requests, like a combined access and error log.
	//   - Logs to stdout.
	app.Use(fiberlog.New(&log.DefaultLogger, nil))

	// Custom logger
	app.Use(fiberlog.New(&log.Logger{
			Context: log.NewContext(nil).Str("logger", "access").Value(),
			Writer:  &log.FileWriter{
				Filename: "access.log",
				MaxSize:  1024 * 1024 * 1024,
			},
		}, func(c *fiber.Ctx) bool {
			if string(c.Path()) == "/backdoor" {
				return true
			}
			return false
		}))

	app.Get("/ping", func(c *fiber.Ctx) error {
		return c.SendString("pong " + fmt.Sprint(time.Now().Unix()))
	})

	app.Get("/backdoor", func(c *fiber.Ctx) error {
		return c.SendString("a backdoor, go away")
	})

	log.Fatal().Err(app.Listen(":3000")).Msg("")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(logger *log.Logger, skip func(c *fiber.Ctx) bool) fiber.Handler

Types

This section is empty.

Jump to

Keyboard shortcuts

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