zwrap

package module
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 5 Imported by: 1

README

zwrap

GoDoc Go Report Card

zwrap is a simple compatibility wrapper around zerolog that allows for a package to use many different logging libraries (including stdlib's log package) without having to import them directly.

Usage

package main

import (
	"os"
	"log"

	"git.tcp.direct/kayos/zwrap"
	"github.com/rs/zerolog"
)

type aLogger interface {
	Println(...interface{})
}

type needsLogger struct {
	logger aLogger
}

func (n *needsLogger) SetLogger(logger aLogger) {
	n.logger = logger
}

func (n *needsLogger) DoSomething() {
	n.logger.Println("Hello, world!")
}

func main() {
	// Create a new zerolog.Logger
	logger := zerolog.New(os.Stdout).With().Timestamp().Logger()
	
	// Demonstrate that we can use the stdlib logger
	myThing := &needsLogger{}
	myThing.SetLogger(log.New(os.Stdout, "", log.LstdFlags))
	myThing.DoSomething()

	// Demonstrate that we can use zerolog when wrapped

	/* Before, does not compile:
	myThing.SetLogger(logger)
	myThing.DoSomething()
	*/
	
	// The zwrap solution, wrap the logger:
	zl := zwrap.Wrap(logger)
	myThing.SetLogger(zl)
	myThing.DoSomething()
}

Documentation

Overview

Package zwrap provides a wrapper for zerolog.Logger that implements the standard library's log.Logger methods, as well as other common logging methods as an attempt to provide compatibility with other logging libraries.

Index

Examples

Constants

This section is empty.

Variables

View Source
var FormattedLevels = map[zerolog.Level]string{
	zerolog.TraceLevel: "TRC",
	zerolog.DebugLevel: "DBG",
	zerolog.InfoLevel:  "INF",
	zerolog.WarnLevel:  "WRN",
	zerolog.ErrorLevel: "ERR",
	zerolog.FatalLevel: "FTL",
	zerolog.PanicLevel: "PNC",
}

FormattedLevels are used by ConsoleWriter's consoleDefaultFormatLevel for a short level name.

View Source
var LevelColors = map[zerolog.Level]int{
	zerolog.TraceLevel: colorMagenta,
	zerolog.DebugLevel: colorYellow,
	zerolog.InfoLevel:  colorGreen,
	zerolog.WarnLevel:  colorRed,
	zerolog.ErrorLevel: colorRed,
	zerolog.FatalLevel: colorRed,
	zerolog.PanicLevel: colorRed,
}

Functions

func Colorize added in v0.4.4

func Colorize(s interface{}, c int, disabled bool) string

Colorize returns the string s wrapped in ANSI code c, unless disabled is true or c is 0.

func LogLevelFmt added in v0.4.4

func LogLevelFmt(noColor bool) zerolog.Formatter

Types

type GRPCCompatLogger added in v0.5.0

type GRPCCompatLogger interface {
	Info(args ...any)
	Infoln(args ...any)
	Infof(format string, args ...any)
	Warning(args ...any)
	Warningln(args ...any)
	Warningf(format string, args ...any)
	Error(args ...any)
	Errorln(args ...any)
	Errorf(format string, args ...any)
	Fatal(args ...any)
	Fatalln(args ...any)
	Fatalf(format string, args ...any)
	V(l int) bool
}

type Level added in v0.6.0

type Level interface {
	int | uint | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64
}

type Logger

type Logger struct {
	*zerolog.Logger
	// contains filtered or unexported fields
}

func Wrap

func Wrap(l zerolog.Logger) *Logger
Example
// Create a new zerolog.Logger
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()

// Demonstrate that we can use the stdlib logger
myThing := &needsLogger{}
myThing.SetLogger(log.New(os.Stderr, "stdlog: ", log.LstdFlags))
myThing.DoSomething()

// Demonstrate that we can use zerolog when wrapped

/* Before, does not compile:
myThing.SetLogger(logger)
myThing.DoSomething()
*/

// The zwrap solution, wrap the logger:
zl := Wrap(logger)
myThing.SetLogger(zl)
myThing.DoSomething()
Output:

func (*Logger) Debug

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

func (*Logger) Debugf

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

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

func (*Logger) Error

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

func (*Logger) Errorf

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

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

func (*Logger) Fatal

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

func (*Logger) Fatalf

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

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

func (*Logger) ForceLevel added in v0.6.0

func (l *Logger) ForceLevel(level any)

func (*Logger) Info

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

func (*Logger) Infof

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

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

func (*Logger) Logf added in v0.4.3

func (l *Logger) Logf(format string, v ...interface{})

func (*Logger) NoFatals added in v0.6.0

func (l *Logger) NoFatals(b bool)

func (*Logger) NoPanics added in v0.6.0

func (l *Logger) NoPanics(b bool)

func (*Logger) Noticef added in v0.3.0

func (l *Logger) Noticef(format string, v ...interface{})

func (*Logger) Output

func (l *Logger) Output(calldepth int, s string) error

func (*Logger) Panic

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

func (*Logger) Panicf

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

func (*Logger) Panicln

func (l *Logger) Panicln(v ...interface{})

func (*Logger) Prefix

func (l *Logger) Prefix() string

func (*Logger) Print

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

func (*Logger) Printf

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

func (*Logger) Println

func (l *Logger) Println(v ...interface{})

func (*Logger) SetLevel added in v0.4.2

func (l *Logger) SetLevel(level any)

SetLevel is compatibility for ghettovoice/gosip/log.Logger

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

func (*Logger) SetPrintLevel

func (l *Logger) SetPrintLevel(level zerolog.Level)

func (*Logger) Trace

func (l *Logger) Trace(v ...interface{})

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

func (*Logger) Traceln

func (l *Logger) Traceln(v ...interface{})

func (*Logger) V added in v0.5.0

func (l *Logger) V(level int) bool

func (*Logger) Verbosef added in v0.3.0

func (l *Logger) Verbosef(format string, v ...interface{})

func (*Logger) Warn

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

func (*Logger) Warnf

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

func (*Logger) Warning added in v0.5.0

func (l *Logger) Warning(args ...any)

func (*Logger) Warningf added in v0.3.0

func (l *Logger) Warningf(format string, v ...interface{})

func (*Logger) Warningln added in v0.5.0

func (l *Logger) Warningln(args ...any)

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

func (*Logger) WithFields added in v0.4.2

func (l *Logger) WithFields(fields map[string]interface{}) *Logger

func (*Logger) WithForceLevel added in v0.6.0

func (l *Logger) WithForceLevel(level any) *Logger

func (*Logger) WithNoFatals added in v0.6.0

func (l *Logger) WithNoFatals() *Logger

func (*Logger) WithNoPanics added in v0.6.0

func (l *Logger) WithNoPanics() *Logger

func (*Logger) WithPrefix added in v0.4.2

func (l *Logger) WithPrefix(prefix string) *Logger

func (*Logger) Write added in v0.4.0

func (l *Logger) Write(p []byte) (n int, err error)

func (*Logger) ZLogger added in v0.6.3

func (l *Logger) ZLogger() *zerolog.Logger

type StdCompatLogger

type StdCompatLogger interface {
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})
	Prefix() string
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
	SetPrefix(prefix string)
	Output(calldepth int, s string) error
}

StdCompatLogger is an interface that provides compatibility with the standard library's log.Logger.

type ZWrapLogger added in v0.6.2

type ZWrapLogger interface {
	StdCompatLogger
	GRPCCompatLogger
}

Jump to

Keyboard shortcuts

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