Documentation ¶
Overview ¶
Package zaptool provides useful wrappers and managers for uber-go/zap logging.
Index ¶
- Constants
- Variables
- func ErrorLevel(err error) zapcore.Level
- func LogLevelsInternalLevel(lvl interface{}) func(*LogLevels)
- func LoggingHTTPHandler(logger *zap.Logger, httpHandler http.Handler, opts ...loggingOptionsFunc) http.Handler
- func LoggingHTTPHandlerWrapper(logger *zap.Logger, opts ...loggingOptionsFunc) func(next http.Handler) http.Handler
- func LoggingOptionForwardedFor(state bool) loggingOptionsFunc
- func LoggingOptionLogLevel(level zapcore.Level) loggingOptionsFunc
- func LoggingOptionTimestamp(state bool) loggingOptionsFunc
- func LoggingOptionTiming(state bool) loggingOptionsFunc
- type LogLevels
- func (a *LogLevels) DeleteLevel(name string)
- func (a *LogLevels) IsLogger(name string) bool
- func (a *LogLevels) Iterator(f func(string, *zap.AtomicLevel) error) error
- func (a *LogLevels) Named(name string, opts ...interface{}) *zap.Logger
- func (a *LogLevels) NewLevel(name string) *zap.AtomicLevel
- func (a *LogLevels) SetLevel(name string, lvl interface{}) bool
- func (a *LogLevels) String() string
- type LogManager
- type SubLogLevels
- func (s *SubLogLevels) DeleteLevel(name string)
- func (s *SubLogLevels) IsLogger(name string) bool
- func (s *SubLogLevels) Iterator(f func(string, *zap.AtomicLevel) error) error
- func (s *SubLogLevels) Named(name string, opts ...interface{}) *zap.Logger
- func (s *SubLogLevels) NewLevel(name string) *zap.AtomicLevel
- func (s *SubLogLevels) SetLevel(name string, lvl interface{}) bool
- func (s *SubLogLevels) String() string
Examples ¶
Constants ¶
const ( HeaderUsername = "X-Logging-Username" HeaderNoop = "X-Logging-Noop" )
Variables ¶
var ErrUnimplemented = errors.New("unimplemented method")
ErrUnimplemented is returned when a method is unimplemented.
Functions ¶
func ErrorLevel ¶ added in v0.4.8
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 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
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 ¶
NewLogLevels returns a new LogLevels ready for use.
func (*LogLevels) DeleteLevel ¶ added in v0.4.2
DeleteLevel removes the entry from the list.
func (*LogLevels) IsLogger ¶ added in v0.4.2
IsLogger returns true if there is a logger that matches.
func (*LogLevels) Iterator ¶ added in v0.4.2
Iterator runs a callback function over the levels map item by item.
func (*LogLevels) Named ¶
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.
type LogManager ¶ added in v0.4.2
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