zaptool

package module
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2024 License: MIT Imports: 12 Imported by: 1

README

go-zaptool

CI GoDoc GitHub issues GitHub forks GitHub stars GitHub license

uber-go/zap wrappers and tools.

Install

go get -u github.com/na4ma4/go-zaptool

Tools

LogLevels
logger, _ := cfg.ZapConfig().Build()
ll := zaptool.NewLogLevels(logger)

processOne := server.NewProcess(ll.Named("Server.Process"))

// somewhere else.

ll.SetLevel("Server.Process", "debug")

// and triggered somewhere else again.

ll.SetLevel("Server.Process", "info")
HTTP Logging Handler
logger, _ := zap.NewProduction()
defer logger.Sync() // flushes buffer, if any
r := mux.NewRouter()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("This is a catch-all route"))
})

loggedRouter := zaptool.LoggingHTTPHandler(logger, r)
http.ListenAndServe(":1123", loggedRouter)

Documentation

Overview

Package zaptool provides useful wrappers and managers for uber-go/zap logging.

Index

Examples

Constants

View Source
const (
	HeaderUsername = "X-Logging-Username"
	HeaderNoop     = "X-Logging-Noop"
)

Variables

View Source
var ErrUnimplemented = errors.New("unimplemented method")

ErrUnimplemented is returned when a method is unimplemented.

Functions

func ErrorLevel added in v0.4.8

func ErrorLevel(err error) zapcore.Level

ErrorLevel returns ErrorLevel if err is non-nil; otherwise, it returns InfoLevel.

func LogLevelsInternalLevel added in v0.4.4

func LogLevelsInternalLevel(lvl interface{}) func(*LogLevels)

func LoggingHTTPHandler

func LoggingHTTPHandler(logger *zap.Logger, httpHandler http.Handler, opts ...loggingOptionsFunc) http.Handler

LoggingHTTPHandler return a http.Handler that wraps h and logs requests to out using a *zap.Logger.

Example
logger := zap.NewExample()
defer func() { _ = logger.Sync() }()

loggedRouter := zaptool.LoggingHTTPHandler(
	logger,
	http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
		// do nothing
	}),
	zaptool.LoggingOptionTimestamp(false),
	zaptool.LoggingOptionTiming(false),
)

ts := httptest.NewServer(loggedRouter)
defer ts.Close()

res, err := http.Get(ts.URL)
if err != nil {
	log.Fatal(err)
}

greeting, err := io.ReadAll(res.Body)
defer res.Body.Close()

if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s", greeting)
Output:

{"level":"info","msg":"Request","http":{"host":"127.0.0.1","username":"-","method":"GET","uri":"/","proto":"HTTP/1.1","status":200,"size":0,"referer":"","user-agent":"Go-http-client/1.1"}}

func LoggingHTTPHandlerWrapper added in v0.4.6

func LoggingHTTPHandlerWrapper(logger *zap.Logger, opts ...loggingOptionsFunc) func(next http.Handler) http.Handler

func LoggingOptionForwardedFor added in v0.4.1

func LoggingOptionForwardedFor(state bool) loggingOptionsFunc

LoggingOptionForwardedFor defines if the logging should contain a `http.forwarded_for` field.

func LoggingOptionLogLevel added in v0.4.7

func LoggingOptionLogLevel(level zapcore.Level) loggingOptionsFunc

LoggingOptionLogLevel defines the log level that http messages should output to, defaults to Info.

func LoggingOptionTimestamp

func LoggingOptionTimestamp(state bool) loggingOptionsFunc

LoggingOptionTimestamp defines if the logging should contain a `http.timestamp` field.

func LoggingOptionTiming

func LoggingOptionTiming(state bool) loggingOptionsFunc

LoggingOptionTiming defines if the logging should contain a `http.request_time` field.

Types

type LogLevels

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

LogLevels provides a wrapper for multiple *zap.Logger levels, the individual loggers are not kept, but levels are kept indexed by name.

func NewLogLevels

func NewLogLevels(coreLogger *zap.Logger, opts ...interface{}) *LogLevels

NewLogLevels returns a new LogLevels ready for use.

func (*LogLevels) DeleteLevel added in v0.4.2

func (a *LogLevels) DeleteLevel(name string)

DeleteLevel removes the entry from the list.

func (*LogLevels) IsLogger added in v0.4.2

func (a *LogLevels) IsLogger(name string) bool

IsLogger returns true if there is a logger that matches.

func (*LogLevels) Iterator added in v0.4.2

func (a *LogLevels) Iterator(f func(string, *zap.AtomicLevel) error) error

Iterator runs a callback function over the levels map item by item.

func (*LogLevels) Named

func (a *LogLevels) Named(name string, opts ...interface{}) *zap.Logger

Named returns a named *zap.Logger if any additional parameters are specified it will try to determine if they represent a log level (by string, zapcore.Level or *zap.AtomicLevel).

func (*LogLevels) NewLevel

func (a *LogLevels) NewLevel(name string) *zap.AtomicLevel

NewLevel returns a zap.AtomicLevel reference to the stored named level.

func (*LogLevels) SetLevel

func (a *LogLevels) SetLevel(name string, lvl interface{}) bool

SetLevel attempts to set the level supplied, it will attempt to typecast the value against string, zapcore.Level and *zap.AtomicLevel.

func (*LogLevels) String

func (a *LogLevels) String() string

String returns a string representation of the currently stored loggers and their levels.

type LogManager added in v0.4.2

type LogManager interface {
	NewLevel(name string) *zap.AtomicLevel
	Named(name string, opts ...interface{}) *zap.Logger
	Iterator(f func(string, *zap.AtomicLevel) error) error
	IsLogger(name string) bool
	SetLevel(name string, lvl interface{}) bool
	DeleteLevel(name string)
	String() string
}

type SubLogLevels added in v0.4.2

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

func NewSubLogLevels added in v0.4.2

func NewSubLogLevels(name string, logmgr LogManager) *SubLogLevels

func (*SubLogLevels) DeleteLevel added in v0.4.2

func (s *SubLogLevels) DeleteLevel(name string)

func (*SubLogLevels) IsLogger added in v0.4.2

func (s *SubLogLevels) IsLogger(name string) bool

func (*SubLogLevels) Iterator added in v0.4.2

func (s *SubLogLevels) Iterator(f func(string, *zap.AtomicLevel) error) error

func (*SubLogLevels) Named added in v0.4.2

func (s *SubLogLevels) Named(name string, opts ...interface{}) *zap.Logger

func (*SubLogLevels) NewLevel added in v0.4.2

func (s *SubLogLevels) NewLevel(name string) *zap.AtomicLevel

func (*SubLogLevels) SetLevel added in v0.4.2

func (s *SubLogLevels) SetLevel(name string, lvl interface{}) bool

func (*SubLogLevels) String added in v0.4.2

func (s *SubLogLevels) String() string

Jump to

Keyboard shortcuts

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