Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var LoggerCtxKey = ctxKey("logger")
LoggerCtxKey is the context key used to store and retrieve the Logger instance from a context.Context.
Functions ¶
This section is empty.
Types ¶
type Entry ¶
type Entry interface {
// Str adds a string key-value pair to the log entry.
Str(key, value string) Entry
// Dur adds a time.Duration key-value pair to the log entry.
Dur(key string, value time.Duration) Entry
// Int adds an int key-value pair to the log entry.
Int(key string, value int) Entry
// Bool adds a bool key-value pair to the log entry.
Bool(key string, value bool) Entry
// Err adds an error to the log entry.
Err(error) Entry
// Msg sends the log entry with the given message.
Msg(msg string)
}
Entry represents a single log event builder. It supports chaining of key-value pairs and message emission.
type Logger ¶
type Logger interface {
// Debug starts a log entry with Debug level.
Debug() Entry
// Info starts a log entry with Info level.
Info() Entry
// Warn starts a log entry with Warn level.
Warn() Entry
// Error starts a log entry with Error level.
Error() Entry
// Fatal starts a log entry with Fatal level.
Fatal() Entry
}
Logger defines a structured logging interface with methods for different log levels.
func FromContext ¶
FromContext attempts to retrieve a Logger from the provided context.Context. If no logger is found, it returns nil.
Example usage:
logger := log.FromContext(ctx)
if logger != nil {
logger.Info().Str("user", "alice").Msg("User logged in")
}
type NoOpLogger ¶
type NoOpLogger struct{}
NoOpLogger is a Logger implementation that performs no operations. It can be used as a default or placeholder logger to avoid nil checks.
Example usage:
var logger Logger = &NoOpLogger{}
logger.Info().Str("key", "value").Msg("This message is ignored")
func (*NoOpLogger) Debug ¶
func (l *NoOpLogger) Debug() Entry
Debug returns a no-op Entry for debug-level logs.
func (*NoOpLogger) Error ¶
func (l *NoOpLogger) Error() Entry
Error returns a no-op Entry for error-level logs.
func (*NoOpLogger) Fatal ¶
func (l *NoOpLogger) Fatal() Entry
Fatal returns a no-op Entry for fatal-level logs.
func (*NoOpLogger) Info ¶
func (l *NoOpLogger) Info() Entry
Info returns a no-op Entry for info-level logs.
func (*NoOpLogger) Warn ¶
func (l *NoOpLogger) Warn() Entry
Warn returns a no-op Entry for warn-level logs.
type ZeroLogger ¶
type ZeroLogger struct {
// contains filtered or unexported fields
}
ZeroLogger is an adapter that implements the Logger interface using the zerolog.Logger from the zerolog package.
Example usage:
import (
"github.com/rs/zerolog"
"os"
)
func main() {
zlogger := zerolog.New(os.Stdout).With().Timestamp().Logger()
logger := NewZeroLogger(zlogger)
logger.Info().Str("user", "alice").Msg("User logged in")
}
func NewZeroLogger ¶
func NewZeroLogger(z zerolog.Logger) *ZeroLogger
NewZeroLogger creates a new ZeroLogger wrapping a zerolog.Logger instance.
func (*ZeroLogger) Debug ¶
func (zl *ZeroLogger) Debug() Entry
Debug starts a debug-level log entry.
func (*ZeroLogger) Error ¶
func (zl *ZeroLogger) Error() Entry
Error starts an error-level log entry.
func (*ZeroLogger) Fatal ¶
func (zl *ZeroLogger) Fatal() Entry
Fatal starts a fatal-level log entry.