logger

package
v0.0.0-...-592e5ed Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package logger provides simple terminal output logging capabilities.

Package logger provides structured logging capabilities with JSON formatting and context support.

Index

Constants

View Source
const (
	SimpleLevelDebug = "DEBUG"
	SimpleLevelInfo  = "INFO"
	SimpleLevelWarn  = "WARN"
	SimpleLevelError = "ERROR"
)

Log level constants for simple logger (string format).

Variables

This section is empty.

Functions

func Debug

func Debug(msg string, args ...interface{})

Debug logs a debug message using the global logger.

func Error

func Error(msg string, args ...interface{})

Error logs an error message using the global logger.

func ErrorWithStack

func ErrorWithStack(err error, msg string, args ...interface{})

ErrorWithStack logs an error message with stack trace using the global logger.

func Info

func Info(msg string, args ...interface{})

Info logs an info message using the global logger.

func IsDebugEnabled

func IsDebugEnabled() bool

IsDebugEnabled returns whether global debug logging is enabled.

func IsVerboseEnabled

func IsVerboseEnabled() bool

IsVerboseEnabled returns whether global verbose logging is enabled.

func SetGlobalLogger

func SetGlobalLogger(logger *StructuredLogger)

SetGlobalLogger sets a global logger instance.

func SetGlobalLoggingFlags

func SetGlobalLoggingFlags(verbose, debug, quiet bool)

SetGlobalLoggingFlags sets global logging flags that override config settings.

func SetGlobalSimpleLogger

func SetGlobalSimpleLogger(logger *SimpleLogger)

SetGlobalSimpleLogger sets a global simple logger instance.

func SimpleDebug

func SimpleDebug(msg string, args ...interface{})

SimpleDebug logs a debug message using the global logger.

func SimpleError

func SimpleError(msg string, args ...interface{})

SimpleError logs an error message using the global logger.

func SimpleErrorWithStack

func SimpleErrorWithStack(err error, msg string, args ...interface{})

SimpleErrorWithStack logs an error with stack trace using the global logger.

func SimpleInfo

func SimpleInfo(msg string, args ...interface{})

SimpleInfo logs an info message using the global logger.

func SimpleWarn

func SimpleWarn(msg string, args ...interface{})

SimpleWarn logs a warning message using the global logger.

func Warn

func Warn(msg string, args ...interface{})

Warn logs a warning message using the global logger.

Types

type CallerInfo

type CallerInfo struct {
	File     string `json:"file"`
	Line     int    `json:"line"`
	Function string `json:"function"`
}

CallerInfo represents caller information.

type CommonLogger

type CommonLogger interface {
	Debug(msg string, args ...interface{})
	Info(msg string, args ...interface{})
	Warn(msg string, args ...interface{})
	Error(msg string, args ...interface{})
	ErrorWithStack(err error, msg string, args ...interface{})
}

CommonLogger defines the common interface for both structured and simple loggers.

type ConsoleHandler

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

ConsoleHandler provides human-readable console output.

func NewConsoleHandler

func NewConsoleHandler(w io.Writer, opts *slog.HandlerOptions) *ConsoleHandler

NewConsoleHandler creates a new console handler with human-readable output.

func (*ConsoleHandler) Enabled

func (ch *ConsoleHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled returns whether the handler is enabled for the given level.

func (*ConsoleHandler) Handle

func (ch *ConsoleHandler) Handle(_ context.Context, record slog.Record) error

Handle processes a log record and outputs it in human-readable format.

func (*ConsoleHandler) WithAttrs

func (ch *ConsoleHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new handler with the given attributes.

func (*ConsoleHandler) WithGroup

func (ch *ConsoleHandler) WithGroup(name string) slog.Handler

WithGroup returns a new handler with the given group.

type ErrorInfo

type ErrorInfo struct {
	Type       string `json:"type"`
	Message    string `json:"message"`
	StackTrace string `json:"stackTrace,omitempty"`
	Code       string `json:"code,omitempty"`
}

ErrorInfo represents error information.

type LogEntry

type LogEntry struct {
	Timestamp   time.Time              `json:"timestamp"`
	Level       string                 `json:"level"`
	Message     string                 `json:"message"`
	Component   string                 `json:"component"`
	SessionID   string                 `json:"sessionId"`
	Context     map[string]interface{} `json:"context,omitempty"`
	Caller      *CallerInfo            `json:"caller,omitempty"`
	Error       *ErrorInfo             `json:"error,omitempty"`
	Performance *PerformanceInfo       `json:"performance,omitempty"`
}

LogEntry represents a structured log entry.

type LogLevel

type LogLevel string

LogLevel represents logging levels.

const (
	// LevelDebug represents debug log level.
	LevelDebug LogLevel = "debug"
	// LevelInfo represents info log level.
	LevelInfo LogLevel = "info"
	// LevelWarn represents warning log level.
	LevelWarn LogLevel = "warn"
	// LevelError represents error log level.
	LevelError LogLevel = "error"
)

type MultiHandler

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

MultiHandler implements slog.Handler to write to multiple handlers.

func NewMultiHandler

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

NewMultiHandler creates a handler that writes to multiple handlers.

func (*MultiHandler) Enabled

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

Enabled returns true if any handler is enabled.

func (*MultiHandler) Handle

func (mh *MultiHandler) Handle(ctx context.Context, record slog.Record) error

Handle writes the record to all handlers.

func (*MultiHandler) WithAttrs

func (mh *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new MultiHandler with attributes added to all handlers.

func (*MultiHandler) WithGroup

func (mh *MultiHandler) WithGroup(name string) slog.Handler

WithGroup returns a new MultiHandler with a group added to all handlers.

type PerformanceInfo

type PerformanceInfo struct {
	Duration    time.Duration          `json:"duration"`
	MemoryUsage int64                  `json:"memoryUsage"`
	Operation   string                 `json:"operation"`
	Metrics     map[string]interface{} `json:"metrics,omitempty"`
}

PerformanceInfo represents performance metrics.

type SimpleLogger

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

SimpleLogger provides straightforward terminal output for better readability.

func GetGlobalSimpleLogger

func GetGlobalSimpleLogger() *SimpleLogger

GetGlobalSimpleLogger returns the global simple logger instance.

func NewSimpleLogger

func NewSimpleLogger(component string) *SimpleLogger

NewSimpleLogger creates a new simple terminal logger.

func (*SimpleLogger) Debug

func (l *SimpleLogger) Debug(msg string, args ...interface{})

Debug prints a debug message.

func (*SimpleLogger) Error

func (l *SimpleLogger) Error(msg string, args ...interface{})

Error prints an error message.

func (*SimpleLogger) ErrorWithStack

func (l *SimpleLogger) ErrorWithStack(err error, msg string, args ...interface{})

ErrorWithStack prints an error message with error details.

func (*SimpleLogger) Info

func (l *SimpleLogger) Info(msg string, args ...interface{})

Info prints an info message.

func (*SimpleLogger) LogPerformance

func (l *SimpleLogger) LogPerformance(operation string, duration time.Duration, metrics map[string]interface{})

LogPerformance prints performance information.

func (*SimpleLogger) LoggerMiddleware

func (l *SimpleLogger) LoggerMiddleware(next func() error) error

LoggerMiddleware provides logging middleware functionality.

func (*SimpleLogger) Warn

func (l *SimpleLogger) Warn(msg string, args ...interface{})

Warn prints a warning message.

func (*SimpleLogger) WithContext

func (l *SimpleLogger) WithContext(key string, value interface{}) *SimpleLogger

WithContext adds context to the logger.

func (*SimpleLogger) WithSession

func (l *SimpleLogger) WithSession(sessionID string) *SimpleLogger

WithSession sets a session ID.

type StructuredLogger

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

StructuredLogger provides advanced logging capabilities.

func GetGlobalLogger

func GetGlobalLogger() *StructuredLogger

GetGlobalLogger returns the global logger instance.

func NewConsoleOnlyLogger

func NewConsoleOnlyLogger(component string, level LogLevel) *StructuredLogger

NewConsoleOnlyLogger creates a logger that only outputs to console.

func NewDualLogger

func NewDualLogger(component string, level LogLevel) (*StructuredLogger, error)

NewDualLogger creates a logger that outputs to both console (human-readable) and file (JSON).

func NewStructuredLogger

func NewStructuredLogger(component string, level LogLevel) *StructuredLogger

NewStructuredLogger creates a new structured logger with dual output.

func (*StructuredLogger) Debug

func (l *StructuredLogger) Debug(msg string, args ...interface{})

Debug logs a debug message.

func (*StructuredLogger) Error

func (l *StructuredLogger) Error(msg string, args ...interface{})

Error logs an error message.

func (*StructuredLogger) ErrorWithStack

func (l *StructuredLogger) ErrorWithStack(err error, msg string, args ...interface{})

ErrorWithStack logs an error with stack trace.

func (*StructuredLogger) Info

func (l *StructuredLogger) Info(msg string, args ...interface{})

Info logs an info message.

func (*StructuredLogger) LogPerformance

func (l *StructuredLogger) LogPerformance(operation string, duration time.Duration, metrics map[string]interface{})

LogPerformance logs performance metrics.

func (*StructuredLogger) LoggerMiddleware

func (l *StructuredLogger) LoggerMiddleware(next func() error) error

LoggerMiddleware provides logging middleware functionality.

func (*StructuredLogger) Warn

func (l *StructuredLogger) Warn(msg string, args ...interface{})

Warn logs a warning message.

func (*StructuredLogger) WithContext

func (l *StructuredLogger) WithContext(key string, value interface{}) *StructuredLogger

WithContext adds context to the logger.

func (*StructuredLogger) WithSession

func (l *StructuredLogger) WithSession(sessionID string) *StructuredLogger

WithSession sets a session ID.

Jump to

Keyboard shortcuts

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