xlog

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package log provides a simple way to write logs in Go applications.

It has two types of loggers:

  • JSONLogger: Writes logs in JSON format. Good for servers.
  • TextLogger: Writes logs as text with colors. Good for local work.

Usage

Create a logger:

// For servers (JSON)
logger := log.NewJSONLogger(os.Stdout, log.LogLevelInfoLevel)

// For local work (Text)
logger := log.NewTextLogger(os.Stdout, log.LogLevelDebugLevel)

Write logs:

logger.Info("Hello world")
logger.Infof("User %s joined", "Alice")
logger.WithField("id", 123).Info("User login")

Control details:

logger.SetLevel(log.LogLevelDebugLevel)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONLogger

type JSONLogger struct {
	// contains filtered or unexported fields
}

JSONLogger implements the Logger interface and outputs logs in JSON format.

func NewJSONLogger

func NewJSONLogger(out io.Writer, level LogLevel) JSONLogger

NewJSONLogger creates a new JSONLogger writing to the provided io.Writer at the specified level. If out is nil, it defaults to os.Stdout.

func (JSONLogger) CheckLevel

func (l JSONLogger) CheckLevel(level LogLevel) bool

func (JSONLogger) Debug

func (l JSONLogger) Debug(msg string)

func (JSONLogger) DebugContext

func (l JSONLogger) DebugContext(ctx context.Context, msg string)

func (JSONLogger) DebugContextf

func (l JSONLogger) DebugContextf(ctx context.Context, format string, args ...any)

func (JSONLogger) Debugf

func (l JSONLogger) Debugf(format string, args ...any)

func (JSONLogger) Error

func (l JSONLogger) Error(msg string)

func (JSONLogger) ErrorContext

func (l JSONLogger) ErrorContext(ctx context.Context, msg string)

func (JSONLogger) ErrorContextf

func (l JSONLogger) ErrorContextf(ctx context.Context, format string, args ...any)

func (JSONLogger) Errorf

func (l JSONLogger) Errorf(format string, args ...any)

func (JSONLogger) Fatal

func (l JSONLogger) Fatal(msg string)

func (JSONLogger) FatalContext

func (l JSONLogger) FatalContext(ctx context.Context, msg string)

func (JSONLogger) FatalContextf

func (l JSONLogger) FatalContextf(ctx context.Context, format string, args ...any)

func (JSONLogger) Fatalf

func (l JSONLogger) Fatalf(format string, args ...any)

func (JSONLogger) GetLevel

func (l JSONLogger) GetLevel() LogLevel

func (JSONLogger) Info

func (l JSONLogger) Info(msg string)

func (JSONLogger) InfoContext

func (l JSONLogger) InfoContext(ctx context.Context, msg string)

func (JSONLogger) InfoContextf

func (l JSONLogger) InfoContextf(ctx context.Context, format string, args ...any)

func (JSONLogger) Infof

func (l JSONLogger) Infof(format string, args ...any)

func (JSONLogger) SetLevel

func (l JSONLogger) SetLevel(level LogLevel)

func (JSONLogger) Warn

func (l JSONLogger) Warn(msg string)

func (JSONLogger) WarnContext

func (l JSONLogger) WarnContext(ctx context.Context, msg string)

func (JSONLogger) WarnContextf

func (l JSONLogger) WarnContextf(ctx context.Context, format string, args ...any)

func (JSONLogger) Warnf

func (l JSONLogger) Warnf(format string, args ...any)

func (JSONLogger) WithField

func (l JSONLogger) WithField(key string, value any) Logger

func (JSONLogger) WithFields

func (l JSONLogger) WithFields(fields map[string]any) Logger

type LogLevel

type LogLevel int32

LogLevel defines the severity level of a log message.

const (
	LogLevelDebugLevel LogLevel = iota
	LogLevelInfoLevel
	LogLevelWarnLevel
	LogLevelErrorLevel
	LogLevelFatalLevel
)

func (LogLevel) String

func (l LogLevel) String() string

type Logger

type Logger interface {
	// Info logs a message at Info level.
	Info(msg string)
	// Infof logs a formatted message at Info level.
	Infof(format string, args ...any)
	// InfoContext logs a message at Info level with context.
	InfoContext(ctx context.Context, msg string)
	// InfoContextf logs a formatted message at Info level with context.
	InfoContextf(ctx context.Context, format string, args ...any)

	// Debug logs a message at Debug level.
	Debug(msg string)
	// Debugf logs a formatted message at Debug level.
	Debugf(format string, args ...any)
	// DebugContext logs a message at Debug level with context.
	DebugContext(ctx context.Context, msg string)
	// DebugContextf logs a formatted message at Debug level with context.
	DebugContextf(ctx context.Context, format string, args ...any)

	// Warn logs a message at Warn level.
	Warn(msg string)
	// Warnf logs a formatted message at Warn level.
	Warnf(format string, args ...any)
	// WarnContext logs a message at Warn level with context.
	WarnContext(ctx context.Context, msg string)
	// WarnContextf logs a formatted message at Warn level with context.
	WarnContextf(ctx context.Context, format string, args ...any)

	// Error logs a message at Error level.
	Error(msg string)
	// Errorf logs a formatted message at Error level.
	Errorf(format string, args ...any)
	// ErrorContext logs a message at Error level with context.
	ErrorContext(ctx context.Context, msg string)
	// ErrorContextf logs a formatted message at Error level with context.
	ErrorContextf(ctx context.Context, format string, args ...any)

	// Fatal logs a message at Fatal level and then exits the application with status 1.
	Fatal(msg string)
	// Fatalf logs a formatted message at Fatal level and then exits.
	Fatalf(format string, args ...any)
	// FatalContext logs a message at Fatal level with context and then exits.
	FatalContext(ctx context.Context, msg string)
	// FatalContextf logs a formatted message at Fatal level with context and then exits.
	FatalContextf(ctx context.Context, format string, args ...any)

	// WithField adds a single field to the logger context.
	// It returns a new Logger instance with the field added.
	WithField(key string, value any) Logger
	// WithFields adds multiple fields to the logger context.
	// It returns a new Logger instance with the fields added.
	WithFields(fields map[string]any) Logger

	// SetLevel sets the minimum logging level.
	SetLevel(level LogLevel)
	// GetLevel returns the current logging level.
	GetLevel() LogLevel
}

Logger defines the universal logging interface for the stdx library. It supports structured logging, leveled logging, and context-aware logging.

type TextLogger

type TextLogger struct {
	// contains filtered or unexported fields
}

TextLogger implements the Logger interface and outputs logs in a human-readable text format. It supports ANSI colors for different log levels.

func NewTextLogger

func NewTextLogger(out io.Writer, level LogLevel) TextLogger

NewTextLogger creates a new TextLogger writing to the provided io.Writer at the specified level. If out is nil, it defaults to os.Stdout.

func (TextLogger) CheckLevel

func (l TextLogger) CheckLevel(level LogLevel) bool

func (TextLogger) Debug

func (l TextLogger) Debug(msg string)

func (TextLogger) DebugContext

func (l TextLogger) DebugContext(ctx context.Context, msg string)

func (TextLogger) DebugContextf

func (l TextLogger) DebugContextf(ctx context.Context, format string, args ...any)

func (TextLogger) Debugf

func (l TextLogger) Debugf(format string, args ...any)

func (TextLogger) Error

func (l TextLogger) Error(msg string)

func (TextLogger) ErrorContext

func (l TextLogger) ErrorContext(ctx context.Context, msg string)

func (TextLogger) ErrorContextf

func (l TextLogger) ErrorContextf(ctx context.Context, format string, args ...any)

func (TextLogger) Errorf

func (l TextLogger) Errorf(format string, args ...any)

func (TextLogger) Fatal

func (l TextLogger) Fatal(msg string)

func (TextLogger) FatalContext

func (l TextLogger) FatalContext(ctx context.Context, msg string)

func (TextLogger) FatalContextf

func (l TextLogger) FatalContextf(ctx context.Context, format string, args ...any)

func (TextLogger) Fatalf

func (l TextLogger) Fatalf(format string, args ...any)

func (TextLogger) GetLevel

func (l TextLogger) GetLevel() LogLevel

func (TextLogger) Info

func (l TextLogger) Info(msg string)

func (TextLogger) InfoContext

func (l TextLogger) InfoContext(ctx context.Context, msg string)

func (TextLogger) InfoContextf

func (l TextLogger) InfoContextf(ctx context.Context, format string, args ...any)

func (TextLogger) Infof

func (l TextLogger) Infof(format string, args ...any)

func (TextLogger) SetLevel

func (l TextLogger) SetLevel(level LogLevel)

func (TextLogger) Warn

func (l TextLogger) Warn(msg string)

func (TextLogger) WarnContext

func (l TextLogger) WarnContext(ctx context.Context, msg string)

func (TextLogger) WarnContextf

func (l TextLogger) WarnContextf(ctx context.Context, format string, args ...any)

func (TextLogger) Warnf

func (l TextLogger) Warnf(format string, args ...any)

func (TextLogger) WithField

func (l TextLogger) WithField(key string, value any) Logger

func (TextLogger) WithFields

func (l TextLogger) WithFields(fields map[string]any) Logger

Jump to

Keyboard shortcuts

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