logger

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package logger provides structured logging for the xw application.

This package implements a simple but effective logging system with multiple log levels and optional debug mode. It provides:

  • Multiple log levels (DEBUG, INFO, WARN, ERROR, FATAL)
  • Structured output with timestamps and caller information
  • Global and instance-based loggers
  • Thread-safe operations
  • Color-coded console output (when appropriate)

Example usage:

logger.Info("Server starting on port %d", 11581)
logger.Debug("Configuration loaded: %+v", cfg)
logger.Error("Failed to connect: %v", err)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(format string, v ...interface{})

Debug logs a debug message using the global logger

func Error

func Error(format string, v ...interface{})

Error logs an error message using the global logger

func Fatal

func Fatal(format string, v ...interface{})

Fatal logs a fatal error message and terminates the program

func Info

func Info(format string, v ...interface{})

Info logs an informational message using the global logger

func SetDebug

func SetDebug(enable bool)

SetDebug enables or disables debug mode for the global logger

func SetLevel

func SetLevel(level Level)

SetLevel sets the level for the global logger

func Warn

func Warn(format string, v ...interface{})

Warn logs a warning message using the global logger

Types

type Level

type Level int

Level represents the severity level of a log message

const (
	// DebugLevel is for detailed debugging information
	DebugLevel Level = iota

	// InfoLevel is for general informational messages
	InfoLevel

	// WarnLevel is for warning messages
	WarnLevel

	// ErrorLevel is for error messages
	ErrorLevel

	// FatalLevel is for fatal errors that cause program termination
	FatalLevel
)

func ParseLevel

func ParseLevel(s string) Level

ParseLevel converts a string to a log level.

Supported values: "debug", "info", "warn", "error", "fatal"

Returns InfoLevel if the string is not recognized.

func (Level) String

func (l Level) String() string

String returns the string representation of the log level

type Logger

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

Logger represents a logger instance with configurable output and level

func New

func New(out io.Writer, prefix string, flags int) *Logger

New creates a new Logger instance.

Parameters:

  • out: The output writer for log messages
  • prefix: An optional prefix for all log messages
  • flags: Log flags (see log package constants)

Returns:

  • A pointer to a configured Logger

Example:

logger := logger.New(os.Stdout, "[xw] ", log.LstdFlags|log.Lshortfile)

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...interface{})

Debug logs a debug message (only if debug mode is enabled).

Example:

logger.Debug("Processing request: %+v", req)

func (*Logger) Error

func (l *Logger) Error(format string, v ...interface{})

Error logs an error message.

Example:

logger.Error("Failed to connect to database: %v", err)

func (*Logger) Fatal

func (l *Logger) Fatal(format string, v ...interface{})

Fatal logs a fatal error message and terminates the program.

This function does not return.

Example:

logger.Fatal("Cannot start server: %v", err)

func (*Logger) Info

func (l *Logger) Info(format string, v ...interface{})

Info logs an informational message.

Example:

logger.Info("Server started on port %d", 11581)

func (*Logger) Output

func (l *Logger) Output(level Level, format string, v ...interface{})

Output writes a log message at the specified level.

This is the core logging function used by all level-specific functions.

func (*Logger) SetDebug

func (l *Logger) SetDebug(enable bool)

SetDebug enables or disables debug logging.

When enabled, DEBUG level messages are printed.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the minimum log level for this logger.

Messages below this level will be discarded.

func (*Logger) Warn

func (l *Logger) Warn(format string, v ...interface{})

Warn logs a warning message.

Example:

logger.Warn("Deprecated API called: %s", apiName)

Jump to

Keyboard shortcuts

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