logz

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 18 Imported by: 0

README

gopherz

ezpkg.io/logz

PkgGoDev GitHub License version

Package logz provides common interfaces and utilities for working with other log packages, including log/slog and zap. It's useful for package library to provide a common interface for logging, so that the user can choose the logging package they want to use.

Installation

go get -u ezpkg.io/logz@v0.1.2

Examples

package mypackage

import "log/slog"
import "os"
import "testing"
import "go.uber.org/zap"

import "ezpkg.io/errorz"
import "ezpkg.io/logz"

type SuperPower struct {
	logger logz.Logger
}

func NewSuperPower(logger logz.Logger) (*SuperPower, error) {
	sp := &SuperPower{logger: logger}
	sp.logger.Debugw("A new SuperPower is created! Better be prepared! 🔥")
	return sp, nil
}

func TestSuperPowerUsingSlog(t *testing.T) {
	opt := &slog.HandlerOptions{Level: slog.LevelDebug}
	handler := slog.NewTextHandler(os.Stderr, opt)
	stdLogger := slog.New(handler)

	sp, err := NewSuperPower(logz.FromLoggerI(stdLogger))
	assertSuperPower(sp, err)
}
func TestSuperPowerUsingZap(t *testing.T) {
	zapLogger := errorz.Must(zap.NewDevelopment()).Sugar()

	sp, err := NewSuperPower(logz.FromLoggerx(zapLogger))
	assertSuperPower(sp, err)
}

About ezpkg.io

As I work on various Go projects, I often find myself creating utility functions, extending existing packages, or developing packages to solve specific problems. Moving from one project to another, I usually have to copy or rewrite these solutions. So I created this repository to have all these utilities and packages in one place. Hopefully, you'll find them useful as well.

For more information, see the main repository.

Author

Oliver Nguyen  github

Documentation

Overview

Package logz provides common interfaces and utilities for working with other log packages, including log/slog and zap.

Index

Constants

View Source
const (
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

Variables

This section is empty.

Functions

func FormatLevelColor added in v0.1.2

func FormatLevelColor(opt *FormatLevelColorOptions) func([]byte, Level) []byte

func SetDefaultEnableLevel added in v0.1.2

func SetDefaultEnableLevel(level Level)

Types

type Attr added in v0.1.2

type Attr = slog.Attr

type CtxEnabler added in v0.1.2

type CtxEnabler interface {
	Enabled(ctx context.Context, level Level) bool
}

type CtxEnablerFunc added in v0.1.2

type CtxEnablerFunc func(ctx context.Context, level Level) bool

func (CtxEnablerFunc) Enabled added in v0.1.2

func (fn CtxEnablerFunc) Enabled(ctx context.Context, level Level) bool

type Enabler added in v0.1.2

type Enabler interface {
	Enabled(level Level) bool
}

type EnablerFunc added in v0.1.2

type EnablerFunc func(level Level) bool

func (EnablerFunc) Enabled added in v0.1.2

func (fn EnablerFunc) Enabled(level Level) bool

type FormatLevelColorOptions added in v0.1.2

type FormatLevelColorOptions struct {
	DebugColor string // colorz.Magenta.Code()
	InfoColor  string // colorz.Cyan.Code()
	WarnColor  string // colorz.Yellow.Code()
	ErrorColor string // colorz.Red.Code()
}

type Handler added in v0.1.2

type Handler = slog.Handler

func NewTextHandler added in v0.1.2

func NewTextHandler(w io.Writer, opt *TextHandlerOptions) Handler

type Level added in v0.1.2

type Level = slog.Level

func GetDefaultEnableLevel added in v0.1.2

func GetDefaultEnableLevel() Level

type LevelVar added in v0.1.2

type LevelVar = slog.LevelVar

func DefaultLevelVar added in v0.1.2

func DefaultLevelVar() *LevelVar

type Leveler added in v0.1.2

type Leveler = slog.Leveler

type Logger

type Logger interface {
	Debugw(msg string, keyValues ...any)
	Infow(msg string, keyValues ...any)
	Warnw(msg string, keyValues ...any)
	Errorw(msg string, keyValues ...any)

	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)

	Enabled(ctx context.Context, level Level) bool
	With(keyValues ...any) Logger
}

func DefaultLogger added in v0.1.2

func DefaultLogger(w io.Writer) Logger

func FromLoggerI

func FromLoggerI(logger LoggerI) Logger

func FromLoggerP

func FromLoggerP(logger LoggerP) Logger

func FromLoggerf

func FromLoggerf(logger Loggerf) Logger

func FromLoggerw

func FromLoggerw(logger Loggerw) Logger

func FromLoggerx

func FromLoggerx(logger Loggerx) Logger

func New added in v0.1.2

func New(h Handler) Logger

type LoggerI

type LoggerI interface {
	Debug(msg string, keyValues ...any)
	Info(msg string, keyValues ...any)
	Warn(msg string, keyValues ...any)
	Error(msg string, keyValues ...any)
}

LoggerI is implemented by stdlib slog.Logger.

type LoggerP

type LoggerP interface {
	Printf(format string, args ...any)
}

LoggerP is implemented by stdlib log.Logger.

type Loggerf

type Loggerf interface {
	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Warnf(format string, args ...any)
	Errorf(format string, args ...any)
}

type Loggerw

type Loggerw interface {
	Debugw(msg string, keyValues ...any)
	Infow(msg string, keyValues ...any)
	Warnw(msg string, keyValues ...any)
	Errorw(msg string, keyValues ...any)
}

Loggerw is implemented by zap.SugaredLogger.

type Loggerx

type Loggerx interface {
	Loggerw
	Loggerf
}

type Option added in v0.1.2

type Option struct {
	// contains filtered or unexported fields
}

func WithCtxEnabler added in v0.1.2

func WithCtxEnabler(enablerFunc CtxEnablerFunc) Option

func WithEnabler added in v0.1.2

func WithEnabler(enabler EnablerFunc) Option

func WithLevel added in v0.1.2

func WithLevel(level Level) Option

func WithLeveler added in v0.1.2

func WithLeveler(level Leveler) Option

func (Option) FromLoggerI added in v0.1.2

func (o Option) FromLoggerI(logger LoggerI) Logger

func (Option) FromLoggerP added in v0.1.2

func (o Option) FromLoggerP(logger LoggerP) Logger

func (Option) FromLoggerf added in v0.1.2

func (o Option) FromLoggerf(logger Loggerf) Logger

func (Option) FromLoggerw added in v0.1.2

func (o Option) FromLoggerw(logger Loggerw) Logger

func (Option) FromLoggerx added in v0.1.2

func (o Option) FromLoggerx(logger Loggerx) Logger

func (Option) WithCtxEnabler added in v0.1.2

func (o Option) WithCtxEnabler(enabler CtxEnablerFunc) Option

func (Option) WithEnabler added in v0.1.2

func (o Option) WithEnabler(enabler EnablerFunc) Option

func (Option) WithLevel added in v0.1.2

func (o Option) WithLevel(level Level) Option

func (Option) WithLeveler added in v0.1.2

func (o Option) WithLeveler(level Leveler) Option

type TextHandlerOptions added in v0.1.2

type TextHandlerOptions struct {
	AddSource bool // TODO
	Level     Leveler

	FormatTime  func(b []byte, time time.Time) []byte
	FormatLevel func(b []byte, level Level) []byte
}

func (TextHandlerOptions) ToSlogOptions added in v0.1.2

func (o TextHandlerOptions) ToSlogOptions() slog.HandlerOptions

Jump to

Keyboard shortcuts

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