logger

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 7 Imported by: 0

README

GoDoc Build status Go Report Card

Logger

The logger.Logger interface is used throughout Buffalo apps, and other systems, to log a whole manner of things.

Installation

$ go get -u github.com/gobuffalo/logger

Documentation

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

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 Debugf added in v2.0.1

func Debugf(msg string, args ...any)

Debugf logs a formatted 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 Errorf added in v2.0.1

func Errorf(msg string, args ...any)

Errorf logs a formatted 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

func Fatalf(msg string, args ...any)

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 Infof added in v2.0.1

func Infof(msg string, args ...any)

Infof logs a formatted 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 Printf added in v2.0.1

func Printf(msg string, args ...any)

Printf logs a formatted message using the default logger.

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:

  1. Git tag (if available)
  2. Git commit hash
  3. Git branch name
  4. "unknown" (fallback)

func Warn added in v2.0.1

func Warn(args ...any)

Warn logs a warning message using the default logger.

func Warnf added in v2.0.1

func Warnf(msg string, args ...any)

Warnf logs a formatted warning message using the default logger.

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

func ParseLevel(level string) (Level, error)

ParseLevel parses a string level into a Level.

func (Level) String

func (level Level) String() string

String returns the string representation of the logging level.

type Logger

type Logger interface {
	Debugf(string, ...any)
	Infof(string, ...any)
	Printf(string, ...any)
	Warnf(string, ...any)
	Errorf(string, ...any)
	Fatalf(string, ...any)
	Debug(...any)
	Info(...any)
	Warn(...any)
	Error(...any)
	Fatal(...any)
	Panic(...any)
}

type Outable

type Outable interface {
	SetOutput(out io.Writer)
}

Outable interface for loggers that allow setting the output writer

Directories

Path Synopsis
Package logrus provides a logger implementation backed by sirupsen/logrus.
Package logrus provides a logger implementation backed by sirupsen/logrus.

Jump to

Keyboard shortcuts

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