Documentation
¶
Overview ¶
Example ¶
package main import ( "context" "github.com/edwingeng/slog" ) func main() { logger, err := slog.NewDevelopmentConfig().Build() if err != nil { panic(err) } logger.Debug("str1") logger.Infof("str2: %d", 200) logger.Warnw("str3", "foo", 300) type HandlerContext struct { context.Context slog.Logger } ctx := &HandlerContext{ Context: context.TODO(), Logger: logger.NewLoggerWith("handler", "UpdateUserName"), } ctx.Error("invalid user name") }
Output:
Index ¶
- Constants
- type Config
- type LogEntry
- type Logger
- type MessageFinder
- func (mf *MessageFinder) Find(str string) []int
- func (mf *MessageFinder) FindRegexp(pat string) []int
- func (mf *MessageFinder) FindRegexpSequence(seq []string) ([]int, bool)
- func (mf *MessageFinder) FindSequence(seq []string) ([]int, bool)
- func (mf *MessageFinder) FindString(str string) []int
- func (mf *MessageFinder) FindStringSequence(seq []string) ([]int, bool)
- type Scavenger
- func (sc *Scavenger) Debug(args ...any)
- func (sc *Scavenger) Debugf(format string, args ...any)
- func (sc *Scavenger) Debugw(msg string, keyVals ...any)
- func (sc *Scavenger) Dump() string
- func (sc *Scavenger) Entries() []LogEntry
- func (sc *Scavenger) Error(args ...any)
- func (sc *Scavenger) Errorf(format string, args ...any)
- func (sc *Scavenger) Errorw(msg string, keyVals ...any)
- func (sc *Scavenger) Exists(str string) bool
- func (sc *Scavenger) Filter(fn func(level, msg string) bool) *Scavenger
- func (sc *Scavenger) Finder() *MessageFinder
- func (sc *Scavenger) FlushLogger() error
- func (sc *Scavenger) Info(args ...any)
- func (sc *Scavenger) Infof(format string, args ...any)
- func (sc *Scavenger) Infow(msg string, keyVals ...any)
- func (sc *Scavenger) Len() int
- func (sc *Scavenger) LogEntry(index int) LogEntry
- func (sc *Scavenger) LogLevelEnabled(level int) bool
- func (sc *Scavenger) NewLoggerWith(keyVals ...any) Logger
- func (sc *Scavenger) Reset()
- func (sc *Scavenger) SequenceExists(seq []string) bool
- func (sc *Scavenger) Warn(args ...any)
- func (sc *Scavenger) Warnf(format string, args ...any)
- func (sc *Scavenger) Warnw(msg string, keyVals ...any)
- type ZapLogger
- func (zl *ZapLogger) Debug(args ...any)
- func (zl *ZapLogger) Debugf(format string, args ...any)
- func (zl *ZapLogger) Debugw(msg string, keyVals ...any)
- func (zl *ZapLogger) Error(args ...any)
- func (zl *ZapLogger) Errorf(format string, args ...any)
- func (zl *ZapLogger) Errorw(msg string, keyVals ...any)
- func (zl *ZapLogger) FlushLogger() error
- func (zl *ZapLogger) Info(args ...any)
- func (zl *ZapLogger) Infof(format string, args ...any)
- func (zl *ZapLogger) Infow(msg string, keyVals ...any)
- func (zl *ZapLogger) LogLevelEnabled(level int) bool
- func (zl *ZapLogger) NewLoggerWith(keyVals ...any) Logger
- func (zl *ZapLogger) Warn(args ...any)
- func (zl *ZapLogger) Warnf(format string, args ...any)
- func (zl *ZapLogger) Warnw(msg string, keyVals ...any)
- func (zl *ZapLogger) Zap() *zap.Logger
Examples ¶
Constants ¶
View Source
const ( LevelDebug = "DEBUG" LevelInfo = "INFO" LevelWarn = "WARN" LevelError = "ERROR" )
View Source
const ( ZapDebugLevel = int(zapcore.DebugLevel) ZapInfoLevel = int(zapcore.InfoLevel) ZapWarnLevel = int(zapcore.WarnLevel) ZapErrorLevel = int(zapcore.ErrorLevel) )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
func NewDevelopmentConfig ¶
func NewDevelopmentConfig() *Config
func NewProductionConfig ¶
func NewProductionConfig() *Config
type Logger ¶
type Logger interface { // NewLoggerWith adds a variadic number of fields to the logging context. It accepts a // mix of zap.Field objects and loosely-typed key-value pairs. When // processing pairs, the first element of the pair is used as the field key // and the second as the field value. The keys in key-value pairs should be strings. NewLoggerWith(keyVals ...any) Logger // LogLevelEnabled checks if the given log level is enabled. LogLevelEnabled(level int) bool // Debug uses fmt.Sprint to construct and log a message. Debug(args ...any) // Info uses fmt.Sprint to construct and log a message. Info(args ...any) // Warn uses fmt.Sprint to construct and log a message. Warn(args ...any) // Error uses fmt.Sprint to construct and log a message. Error(args ...any) // Debugf uses fmt.Sprintf to log a templated message. Debugf(format string, args ...any) // Infof uses fmt.Sprintf to log a templated message. Infof(format string, args ...any) // Warnf uses fmt.Sprintf to log a templated message. Warnf(format string, args ...any) // Errorf uses fmt.Sprintf to log a templated message. Errorf(format string, args ...any) // Debugw logs a message with some additional context. The variadic key-value // pairs are treated as they are in NewLoggerWith. Debugw(msg string, keyVals ...any) // Infow logs a message with some additional context. The variadic key-value // pairs are treated as they are in NewLoggerWith. Infow(msg string, keyVals ...any) // Warnw logs a message with some additional context. The variadic key-value // pairs are treated as they are in NewLoggerWith. Warnw(msg string, keyVals ...any) // Errorw logs a message with some additional context. The variadic key-value // pairs are treated as they are in NewLoggerWith. Errorw(msg string, keyVals ...any) // FlushLogger flushes any buffered log entries. FlushLogger() error }
Logger is an interface which is highly compatible with zap.SugaredLogger.
func NewDevourer ¶
func NewDevourer() Logger
NewDevourer creates a new Logger that devours all log messages and outputs nothing.
type MessageFinder ¶
type MessageFinder Scavenger
func (*MessageFinder) Find ¶
func (mf *MessageFinder) Find(str string) []int
func (*MessageFinder) FindRegexp ¶
func (mf *MessageFinder) FindRegexp(pat string) []int
func (*MessageFinder) FindRegexpSequence ¶
func (mf *MessageFinder) FindRegexpSequence(seq []string) ([]int, bool)
func (*MessageFinder) FindSequence ¶
func (mf *MessageFinder) FindSequence(seq []string) ([]int, bool)
func (*MessageFinder) FindString ¶
func (mf *MessageFinder) FindString(str string) []int
func (*MessageFinder) FindStringSequence ¶
func (mf *MessageFinder) FindStringSequence(seq []string) ([]int, bool)
type Scavenger ¶
type Scavenger struct {
// contains filtered or unexported fields
}
Scavenger collects all log messages for later queries.
func (*Scavenger) Filter ¶
Filter creates a new Scavenger that contains only the log messages satisfying the predicate fn.
func (*Scavenger) Finder ¶
func (sc *Scavenger) Finder() *MessageFinder
Finder returns a MessageFinder.
func (*Scavenger) FlushLogger ¶
func (*Scavenger) LogLevelEnabled ¶
func (*Scavenger) NewLoggerWith ¶
func (*Scavenger) SequenceExists ¶
type ZapLogger ¶
type ZapLogger struct {
// contains filtered or unexported fields
}
ZapLogger is a wrapper of zap.SugaredLogger.
func NewZapLogger ¶
func NewZapLogger(zsl *zap.SugaredLogger) *ZapLogger
NewZapLogger creates a new ZapLogger.
func (*ZapLogger) FlushLogger ¶
func (*ZapLogger) LogLevelEnabled ¶
func (*ZapLogger) NewLoggerWith ¶
Click to show internal directories.
Click to hide internal directories.