logzap

package module
v0.0.0-...-274ab79 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: MIT Imports: 16 Imported by: 0

README

logzap

A extension for uber/zap

Purpose

The purpose of this package is make logger has sub module level verbose control. In addition, lowest configuration and out of box. I used Sugar logger of zap for all sub module, however it means lost some excellent performance to some degree.

Usage

logger := logzap.New(logzap.Config{
    Level: zapcore.WarnLevel,
    Modules: logzap.ModulesLevel{
        "foo":  zapcore.DebugLevel,
    },
})

fooLogger := logger.Get("foo") // debug level verbose
booLogger := logger.Get("boo") // warning level verbose

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidModuleLevel = fmt.Errorf("invalid module level")
	ErrUnsupportedFields  = fmt.Errorf("unsupported fields")
)
View Source
var (
	ErrUnsupportedCoreConstructor = errors.New("unsupported core constructor")
	ErrCoreNotFound               = errors.New("core not found")
)
View Source
var MapStructureLevelDecodeHook = []mapstructure.DecodeHookFunc{
	levelDecodeHookFunc,
	mapStringDecodeHookFunc,
}

Functions

func Reload

func Reload(lv zapcore.Level, modules ModulesLevel)

Reload reload global instance with given log level.

func Sync

func Sync() error

Sync flushes any buffered log entries.

Types

type Config

type Config struct {
	Level   zapcore.Level `yaml:"level"`
	Modules ModulesLevel  `yaml:"modules,omitempty"`
	Cores   Cores         `yaml:"cores,omitempty"`
}

func (Config) RegisterFlagsWithPrefix

func (t Config) RegisterFlagsWithPrefix(prefix string, f *pflag.FlagSet)

type CoreConstructor

type CoreConstructor func(ctx context.Context, registry prometheus.Registerer, url string) (zapcore.Core, error)

type Cores

type Cores map[string]string

func (Cores) Build

func (t Cores) Build(
	ctx context.Context,
	registry prometheus.Registerer,
) (core []zapcore.Core, err error)

func (Cores) BuildByName

func (t Cores) BuildByName(
	ctx context.Context,
	registry prometheus.Registerer,
	name string,
) (core zapcore.Core, err error)

func (Cores) MustBuild

func (t Cores) MustBuild(
	ctx context.Context,
	registry prometheus.Registerer,
) []zapcore.Core

type Logger

type Logger interface {
	// Error logs a message at ErrorLevel if err is not nil.
	Trace(err error, fields ...zap.Field)
	// TraceError logs a message at ErrorLevel if err is not nil.
	TraceError(err error, fields ...zap.Field) error
	// TraceContext logs a message at ErrorLevel if err is not nil and ctx is not done.
	TraceContext(ctx context.Context, err error, fields ...zap.Field) error

	// Error logs a message at ErrorLevel. The message includes any fields passed
	Error(msg string, fields ...zap.Field)
	// Warn logs a message at WarnLevel. The message includes any fields passed
	Warn(msg string, fields ...zap.Field)
	// Info logs a message at InfoLevel. The message includes any fields passed
	Info(msg string, fields ...zap.Field)
	// Debug logs a message at DebugLevel. The message includes any fields passed
	Debug(msg string, fields ...zap.Field)

	// Errorf uses fmt.Sprintf to log a templated message at ErrorLevel.
	Errorf(format string, args ...interface{})
	// Warnf uses fmt.Sprintf to log a templated message at WarnLevel.
	Warnf(format string, args ...interface{})
	// Infof uses fmt.Sprintf to log a templated message at InfoLevel.
	Infof(format string, args ...interface{})
	// Debugf uses fmt.Sprintf to log a templated message at DebugLevel.
	Debugf(format string, args ...interface{})

	// Increase increase log level
	Increase(lv zapcore.Level) Logger
	// Level return
	zapcore.LevelEnabler
	// L return zap.Logger
	L() *zap.Logger
}

func Get

func Get(name string, opts ...zap.Option) Logger

Get return a logger with given name and options from global instance.

type Logzap

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

func Default

func Default() *Logzap

Default return a instance which with DebugLevel

func New

func New(
	ctx context.Context,
	registry prometheus.Registerer,
	c Config,
) *Logzap

New return a instance with given configuration. Note that it will panic if any error occurred.

func NewE

func NewE(
	ctx context.Context,
	registry prometheus.Registerer,
	c Config,
) (t *Logzap, err error)

NewE return a instance and error with given configuration.

func Nop

func Nop() *Logzap

Nop return a instance which does nothing

func (*Logzap) Get

func (t *Logzap) Get(name string, opts ...zap.Option) Logger

func (*Logzap) Reload

func (t *Logzap) Reload(lv zapcore.Level, modules ModulesLevel)

only reconfiguration the global level and submodule level

func (*Logzap) Sync

func (t *Logzap) Sync() (err error)

func (*Logzap) Use

func (t *Logzap) Use(core zapcore.Core)

type ModulesLevel

type ModulesLevel map[string]zapcore.Level

func (ModulesLevel) Get

func (t ModulesLevel) Get(s string) (l zapcore.Level)

Directories

Path Synopsis
core

Jump to

Keyboard shortcuts

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