log

package
v0.1.83 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 12 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Ldate         = log.Ldate         // Include date in log output (e.g., 2009/01/23).
	Ltime         = log.Ltime         // Include time in log output (e.g., 01:23:23).
	Lmicroseconds = log.Lmicroseconds // Include microsecond resolution in time (requires Ltime).
	Llongfile     = log.Llongfile     // Include full file name and line number (e.g., /a/b/c/d.go:23).
	Lshortfile    = log.Lshortfile    // Include final file name element and line number (e.g., d.go:23), overrides Llongfile.
	LUTC          = log.LUTC          // Use UTC for date/time if Ldate or Ltime is set.
	Lmsgprefix    = log.Lmsgprefix    // Move prefix to before the message.
	LstdFlags     = log.LstdFlags     // Default flags: Ldate | Ltime.
)

Constants for log flags, mirrored from the standard log package.

Variables

This section is empty.

Functions

func Debug

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

Debug logs a message at Debug level using the default Logger.

func DebugContext

func DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs a message at Debug level with context using the default Logger.

func Enabled

func Enabled(ctx context.Context, level slog.Level) bool

Enabled checks if the specified log level is enabled for the default Logger.

func Error

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

Error logs a message at Error level using the default Logger.

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs a message at Error level with context using the default Logger.

func Fatal

func Fatal(v ...any)

Fatal logs a message and exits using the default Logger's Fatal method.

func Fatalf

func Fatalf(format string, v ...any)

Fatalf logs a formatted message and exits using the default Logger's Fatalf method.

func Fatalln

func Fatalln(v ...any)

Fatalln logs a message with a newline and exits using the default Logger's Fatalln method.

func File

func File() string

File returns the current log file path of the default Logger.

func Flags

func Flags() int

Flags returns the current log flags of the default Logger.

func Info

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

Info logs a message at Info level using the default Logger.

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs a message at Info level with context using the default Logger.

func Level

func Level() slog.Level

Level returns the log level of the default Logger.

func ListenRotateSignal

func ListenRotateSignal(ctx context.Context, r Rotatable, sig ...os.Signal)

ListenRotateSignal listens for the specified signals and triggers rotation on the Rotatable object. It stops listening when the context is canceled.

func Log

func Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log logs a message at the specified level with context using the default Logger.

func LogAttrs

func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs logs a message at the specified level with attributes using the default Logger.

func Output

func Output(calldepth int, s string) error

Output logs a message with the specified call depth using the default Logger's Output method.

func Panic

func Panic(v ...any)

Panic logs a message and panics using the default Logger's Panic method.

func Panicf

func Panicf(format string, v ...any)

Panicf logs a formatted message and panics using the default Logger's Panicf method.

func Panicln

func Panicln(v ...any)

Panicln logs a message with a newline and panics using the default Logger's Panicln method.

func Prefix

func Prefix() string

Prefix returns the current log prefix of the default Logger.

func Print

func Print(v ...any)

Print logs a message using the default Logger's Print method.

func Printf

func Printf(format string, v ...any)

Printf logs a formatted message using the default Logger's Printf method.

func Println

func Println(v ...any)

Println logs a message with a newline using the default Logger's Println method.

func Rotate

func Rotate()

Rotate reopens the log file and rotates the extra writer for the default Logger if applicable.

func SetDefault added in v0.1.81

func SetDefault(l *Logger)

SetDefault sets the default Logger instance.

func SetExtra

func SetExtra(extra io.Writer)

SetExtra sets an additional output destination for the default Logger, keeping the existing file.

func SetFile

func SetFile(file string)

SetFile sets the log file path for the default Logger, keeping the existing extra writer.

func SetFlags

func SetFlags(flag int)

SetFlags sets the log flags for the default Logger.

func SetHandler

func SetHandler(h slog.Handler)

SetHandler sets the slog handler for the default Logger. Note: The new handler may not respect the existing log level (obtained via Level), potentially disabling level control. Ensure the provided handler is configured with the desired log level if needed.

func SetLevel

func SetLevel(level slog.Level)

SetLevel sets the log level for the default Logger.

func SetOutput

func SetOutput(file string, extra io.Writer)

SetOutput sets the output destination for the default Logger. The file parameter specifies the log file path; if empty, no file output is used. The extra parameter allows an additional output destination (e.g., stderr).

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets the log prefix for the default Logger.

func SlogHandler added in v0.1.82

func SlogHandler() slog.Handler

SlogHandler returns the slog handler of the default Logger.

func Warn

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

Warn logs a message at Warn level using the default Logger.

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs a message at Warn level with context using the default Logger.

func Writer

func Writer() io.Writer

Writer returns the current output writer of the default Logger.

Types

type Logger

type Logger struct {
	*log.Logger // Underlying standard logger.
	// contains filtered or unexported fields
}

Logger implements a custom logger that combines the standard log.Logger with slog.Logger, providing flexible output destinations and log level control.

func Default

func Default() *Logger

Default returns the current default Logger instance.

func New

func New(file, prefix string, flag int) *Logger

New creates a new Logger instance with the specified file path, prefix, and flags. If file is empty, logs are discarded. Panics if the file cannot be opened.

func With

func With(args ...any) *Logger

With returns a new Logger with the specified attributes, leaving the default Logger unchanged.

func WithGroup

func WithGroup(name string) *Logger

WithGroup returns a new Logger with the specified group name, leaving the default Logger unchanged.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs a message at Debug level with the given arguments.

func (*Logger) DebugContext

func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs a message at Debug level with the given context and arguments.

func (*Logger) Enabled

func (l *Logger) Enabled(ctx context.Context, level slog.Level) bool

Enabled checks if the specified log level is enabled for the logger.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs a message at Error level with the given arguments.

func (*Logger) ErrorContext

func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs a message at Error level with the given context and arguments.

func (*Logger) File

func (l *Logger) File() string

File returns the current log file path, or an empty string if no file is set.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs a message at Info level with the given arguments.

func (*Logger) InfoContext

func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs a message at Info level with the given context and arguments.

func (*Logger) Level

func (l *Logger) Level() slog.Level

Level returns the current log level.

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log logs a message at the specified level with the given context and arguments.

func (*Logger) LogAttrs

func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)

LogAttrs logs a message at the specified level with the given context and attributes.

func (*Logger) Rotate

func (l *Logger) Rotate()

Rotate reopens the log file and rotates the extra writer if it implements Rotatable.

func (*Logger) SetExtra

func (l *Logger) SetExtra(extra io.Writer)

SetExtra sets an additional output destination (e.g., stderr), keeping the existing file.

func (*Logger) SetFile

func (l *Logger) SetFile(file string)

SetFile sets the log file to the specified path, keeping the existing extra writer.

func (*Logger) SetHandler

func (l *Logger) SetHandler(h slog.Handler)

SetHandler sets the slog handler for structured logging. Note: The new handler may not respect the existing log level (l.level), potentially disabling level control. Ensure the provided handler is configured with the desired log level if needed.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level slog.Level)

SetLevel sets the log level for structured logging.

func (*Logger) SetOutput

func (l *Logger) SetOutput(file string, extra io.Writer) error

SetOutput sets the log output to the specified file path and extra writer. Returns an error if the file cannot be opened.

func (*Logger) SlogHandler added in v0.1.82

func (l *Logger) SlogHandler() slog.Handler

SlogHandler returns the current slog handler.

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs a message at Warn level with the given arguments.

func (*Logger) WarnContext

func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs a message at Warn level with the given context and arguments.

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

With returns a new Logger with the specified attributes, leaving the original unchanged.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup returns a new Logger with the specified group name, leaving the original unchanged.

func (*Logger) Write

func (l *Logger) Write(b []byte) (int, error)

Write writes bytes to the logger's output destination, implementing io.Writer.

type Rotatable

type Rotatable interface {
	Rotate()
}

Rotatable defines an interface for objects that support log rotation.

Jump to

Keyboard shortcuts

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