Documentation
¶
Overview ¶
Package logger provides a structured logging interface for Go applications. It uses the standard library's log/slog package as the default implementation, with support for structured fields and multiple log levels.
The package defines Logger and FieldLogger interfaces that are used throughout Buffalo apps and other systems. The default implementation writes to stdout in a human-readable but parseable format.
Basic usage:
log := logger.New(logger.InfoLevel)
log.Info("Server starting")
log.WithField("port", 8080).Info("Server started")
For users who prefer the logrus backend, import the logrus subpackage:
import "github.com/gobuffalo/logger/v2/logrus" log := logrus.New(logrus.InfoLevel)
Index ¶
- func Debug(args ...any)
- func Debugf(msg string, args ...any)
- func Error(args ...any)
- func Errorf(msg string, args ...any)
- func Fatal(args ...any)
- func Fatalf(msg string, args ...any)
- func Info(args ...any)
- func Infof(msg string, args ...any)
- func Panic(args ...any)
- func Printf(msg string, args ...any)
- func SetDefault(l FieldLogger)
- func Version() string
- func Warn(args ...any)
- func Warnf(msg string, args ...any)
- type FieldLogger
- type Level
- type Logger
- type Outable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶ added in v2.0.1
func Debug(args ...any)
Debug logs a debug message using the default logger.
func Error ¶ added in v2.0.1
func Error(args ...any)
Error logs an error message using the default logger.
func Fatal ¶ added in v2.0.1
func Fatal(args ...any)
Fatal logs a fatal message using the default logger and exits the program.
func Fatalf ¶ added in v2.0.1
Fatalf logs a formatted fatal message using the default logger and exits the program.
func Info ¶ added in v2.0.1
func Info(args ...any)
Info logs an info message using the default logger.
func Panic ¶ added in v2.0.1
func Panic(args ...any)
Panic logs a panic message using the default logger and panics.
func SetDefault ¶ added in v2.0.1
func SetDefault(l FieldLogger)
SetDefault sets the default logger instance. Subsequent calls to Default() and package-level logging functions will use this logger.
func Version ¶
func Version() string
Version returns the version of the logger. It attempts to get the version from git metadata in the following order:
- Git tag (if available)
- Git commit hash
- Git branch name
- "unknown" (fallback)
Types ¶
type FieldLogger ¶
type FieldLogger interface {
Logger
WithField(string, any) FieldLogger
WithFields(map[string]any) FieldLogger
}
func Default ¶ added in v2.0.1
func Default() FieldLogger
Default returns the default FieldLogger instance. The default logger is initialized with InfoLevel on first call.
func New ¶
func New(lvl Level) FieldLogger
New based on the specified log level, defaults to "debug". This logger will log to the STDOUT in a human readable, but parseable form.
Example: time="2016-12-01T21:02:07-05:00" level=info duration=225.283µs human_size="106 B" method=GET path="/" render=199.79µs request_id=2265736089 size=106 status=200
func NewLogger ¶
func NewLogger(level string) FieldLogger
NewLogger based on the specified log level, defaults to "debug". See `New` for more details.
func WithField ¶ added in v2.0.1
func WithField(key string, value any) FieldLogger
WithField adds a field to the default logger and returns a new FieldLogger.
func WithFields ¶ added in v2.0.1
func WithFields(fields map[string]any) FieldLogger
WithFields adds multiple fields to the default logger and returns a new FieldLogger.
type Level ¶
type Level uint32
Level represents the logging level
const ( // PanicLevel level, highest level of severity. Logs and then calls panic with the // message passed to Debug, Info, ... PanicLevel Level = iota // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the // logging level is set to Panic. FatalLevel // ErrorLevel level. Logs. Used for errors that should definitely be noted. // Commonly used for hooks to send errors to an error tracking service. ErrorLevel // WarnLevel level. Non-critical entries that deserve eyes. WarnLevel // InfoLevel level. General operational entries about what's going on inside the // application. InfoLevel // DebugLevel level. Usually only enabled when debugging. Very verbose logging. DebugLevel )
These are the different logging levels.
func ParseLevel ¶
ParseLevel parses a string level into a Level.