echotozero

package module
v0.0.0-...-4517c8b Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 9 Imported by: 2

README

Echo to Zerolog

Reference Linter Build status

A simple adapter for using Zerolog with the Echo web framework.

Project status

Complete. Latest commit timestamp might be old - that's okay.

Usage

Steal a complete example from this file or try go run ./demo for a quick start.

import (
	"github.com/kofalt/echotozero"
	"github.com/labstack/echo/v4"
	"github.com/rs/zerolog"
)

func main() {
	// Web framework
	e := echo.New()
	e.HideBanner = true

	// Create or use any zerolog logger
	zl := zerolog.New()
	adapter := echotozero.New(zl)

	// Log echo messages + http queries
	e.Logger = adapter
	e.Use(echotozero.Middleware(adapter))
}

You can also steal middleware.go and mess with which request details are logged.

Notes

This repo is an updated fork of Lecho with a few improvements:

  • Simplifed surface area
  • Easier to read middleware
  • Removed context fiddling
  • Added a log level to echo's messages when it lacks one
  • Punt more features directly to zerolog
  • Updated for more recent golang, actions, etc

There's also a large golang logging discussion going on that might influence both libraries in the future.
This will hold the two together for now.

Known issue

The very first message echo prints is without a level:

7:51AM ??? ⇨ http server started on [::]:8080

Zerolog supports this just fine, but it would be nice to have everything leveled. This message happens because the echo interface demands Output() io.Writer and, for some reason, uses that to print its startup message. Could probably be fixed by messing with echo.

Documentation

Index

Constants

This section is empty.

Variables

Functions

func Middleware

func Middleware(logger *Logger) echo.MiddlewareFunc

MiddleWare logs successful requests to debug, failed req to error, and never skips.

func MiddlewareWithOptions

func MiddlewareWithOptions(logger *Logger, successLvl, failLvl zerolog.Level, skipper middleware.Skipper) echo.MiddlewareFunc

Types

type JSON

type JSON = echoLib.JSON // map[string]any

type Logger

type Logger struct {
	ZLog zerolog.Logger

	// Level used when Echo does not provide one
	DefaultLevel zerolog.Level
}

Logger wraps `zerolog.Logger` and implements `echo.Logger`

func New

func New(log zerolog.Logger) *Logger

func (*Logger) Debug

func (l *Logger) Debug(i ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, i ...interface{})

func (*Logger) Debugj

func (l *Logger) Debugj(j JSON)

func (*Logger) Error

func (l *Logger) Error(i ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, i ...interface{})

func (*Logger) Errorj

func (l *Logger) Errorj(j JSON)

func (*Logger) Fatal

func (l *Logger) Fatal(i ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, i ...interface{})

func (*Logger) Fatalj

func (l *Logger) Fatalj(j JSON)

func (*Logger) Info

func (l *Logger) Info(i ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, i ...interface{})

func (*Logger) Infoj

func (l *Logger) Infoj(j JSON)

func (*Logger) Level

func (l *Logger) Level() echoLevel

func (*Logger) Output

func (l *Logger) Output() io.Writer

func (*Logger) Panic

func (l *Logger) Panic(i ...interface{})

func (*Logger) Panicf

func (l *Logger) Panicf(format string, i ...interface{})

func (*Logger) Panicj

func (l *Logger) Panicj(j JSON)

func (*Logger) Prefix

func (l *Logger) Prefix() string

func (*Logger) Print

func (l *Logger) Print(i ...interface{})

func (*Logger) Printf

func (l *Logger) Printf(format string, i ...interface{})

func (*Logger) Printj

func (l *Logger) Printj(j JSON)

func (*Logger) SetHeader

func (l *Logger) SetHeader(h string)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level echoLevel)

func (*Logger) SetOutput

func (l *Logger) SetOutput(newOut io.Writer)

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(newPrefix string)

func (*Logger) Warn

func (l *Logger) Warn(i ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, i ...interface{})

func (*Logger) Warnj

func (l *Logger) Warnj(j JSON)

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) context.Context

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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