std / log

log

import "std:log"

Provides configurable logging with multiple severity levels, output destinations, and formatting options. Includes both a default logger and the ability to create custom Logger instances.

View source on Codeberg →

Types

#
type Level int

Represents log message severity.

#
type Logger struct { /* unexported fields */ }

A configurable logger instance with output stream, minimum level, and prefix.

Level Constants

#
const (
    LevelDebug Level = iota
    LevelInfo
    LevelWarn
    LevelError
    LevelFatal
)

Log level constants ordered by severity. LevelDebug is the lowest severity and LevelFatal is the highest.

Logger Constructor

#
func NewLogger(output io.Writer, level Level) Logger

Creates a new logger with the specified output stream and minimum level.

Logger Configuration

#
func SetOutput(logger *Logger, output io.Writer)

Sets the output stream for the logger.

#
func SetLevel(logger *Logger, level Level)

Sets the minimum log level for the logger.

#
func SetPrefix(logger *Logger, prefix string)

Sets the prefix for log messages.

Logger Instance - Simple Logging

#
func LogDebug(logger *Logger, args ...any)

Logs a debug message.

#
func LogInfo(logger *Logger, args ...any)

Logs an info message.

#
func LogWarn(logger *Logger, args ...any)

Logs a warning message.

#
func LogError(logger *Logger, args ...any)

Logs an error message.

#
func LogFatal(logger *Logger, args ...any)

Logs a fatal message and exits.

Logger Instance - Printf-style

#
func LogDebugf(logger *Logger, format string, args ...any)

Logs a formatted debug message.

#
func LogInfof(logger *Logger, format string, args ...any)

Logs a formatted info message.

#
func LogWarnf(logger *Logger, format string, args ...any)

Logs a formatted warning message.

#
func LogErrorf(logger *Logger, format string, args ...any)

Logs a formatted error message.

#
func LogFatalf(logger *Logger, format string, args ...any)

Logs a formatted fatal message and exits.

Default Logger Configuration

#
func SetDefaultOutput(output io.Writer)

Sets the output stream for the default logger.

#
func SetDefaultLevel(level Level)

Sets the minimum log level for the default logger.

#
func SetDefaultPrefix(prefix string)

Sets the prefix for the default logger.

Default Logger - Simple Logging

#
func Debug(args ...any)

Logs a debug message to the default logger.

#
func Info(args ...any)

Logs an info message to the default logger.

#
func Warn(args ...any)

Logs a warning message to the default logger.

#
func Error(args ...any)

Logs an error message to the default logger.

#
func Fatal(args ...any)

Logs a fatal message to the default logger and exits.

Default Logger - Printf-style

#
func Debugf(format string, args ...any)

Logs a formatted debug message to the default logger.

#
func Infof(format string, args ...any)

Logs a formatted info message to the default logger.

#
func Warnf(format string, args ...any)

Logs a formatted warning message to the default logger.

#
func Errorf(format string, args ...any)

Logs a formatted error message to the default logger.

#
func Fatalf(format string, args ...any)

Logs a formatted fatal message to the default logger and exits.

Utility

#
func ParseLevel(s string) Level

Parses a log level from a string. Returns LevelInfo if the string is not recognized.

#
func LevelFromEnv(envVar string, defaultLevel Level) Level

Reads a log level from an environment variable. Returns defaultLevel if the variable is not set or not recognized.