irisslog

package module
v0.0.0-...-46bbd1a Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 4 Imported by: 0

README

irisslog

Alternative logging through slog. A clone of https://github.com/iris-contrib/middleware 's zap.

This middleware will log all http requests with slog.

Current support go 1.20. So slog is "golang.org/x/exp/slog"

Usage

Start using it

Download and install it:

go get github.com/fnsne/irisslog@master

Import it in your code:

import "github.com/fnsne/irisslog"

todos

  • irisslog.New()
  • irisslog.NewWithRecover()
  • example
  • skip logging
  • custom fields

Example

See the example.

package main

import (
	"fmt"
	"github.com/fnsne/irisslog"
	"github.com/kataras/iris/v12"
	"golang.org/x/exp/slog"
	"os"
	"time"
)

func main() {

	logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
		AddSource:   true,
		Level:       slog.LevelDebug,
		ReplaceAttr: nil,
	}))

	slog.SetDefault(logger)

	app := iris.New()
	app.Use(irisslog.New(logger))

	// Example ping request.
	app.Get("/ping", func(ctx iris.Context) {
		//can get logger from context that has trace id in it.
		handlerLogger := irisslog.GetLogger(ctx)
		handlerLogger.Info("info ping")
		ctx.Text("pong " + fmt.Sprint(time.Now().Unix()))
	})

	// Example when panic happen.
	app.Get("/panic", func(ctx iris.Context) {
		panic("An unexpected error happen!")
	})

	// Listen and Server in 0.0.0.0:8080
	app.Listen(":8081")

}

Documentation

Overview

Package iriszap provides log handling using zap package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLogger

func GetLogger(ctx iris.Context) *slog.Logger

func New

func New(logger *slog.Logger) func(ctx iris.Context)

Types

This section is empty.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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