logger

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package logger provides logging facilities for the gitbak application.

This package implements a simple, structured logging system with different log levels, colors for terminal output, and the ability to write logs to both the console and a file simultaneously. It defines both the logging interface and the standard implementation used throughout the application.

Core Components

  • Logger: The main interface for logging used throughout the application
  • DefaultLogger: Standard implementation that writes to console and/or file

Features

  • Multiple log levels (Info, Warning, Error, Success)
  • Emoji-prefixed output for terminal visibility
  • File logging with rotation
  • User-facing vs. debug-only messages
  • Conditional logging based on verbosity settings

Log Levels

The logger supports the following distinct message types:

  • Info: General information messages
  • InfoToUser: Important information to display to the user
  • Warning: Warning messages for potential issues
  • WarningToUser: Important warnings to display to the user
  • Error: Error messages for failures
  • Success: Success messages for completed operations
  • StatusMessage: Current status updates

Usage

Basic usage pattern:

// Create a new logger
logger := logger.New(true, "/path/to/log.file", true)

// Log different types of messages
logger.Info("Debug-only information: %v", details)
logger.InfoToUser("Important information: %v", userInfo)
logger.Warning("Potential issue: %v", warning)
logger.Error("An error occurred: %v", err)
logger.Success("Operation completed: %v", result)

Usage With Dependency Injection

The Logger interface is typically injected into components that need logging capabilities:

type MyComponent struct {
    logger logger.Logger
    // other fields
}

func NewMyComponent(logger logger.Logger) *MyComponent {
    return &MyComponent{
        logger: logger,
    }
}

func (c *MyComponent) DoSomething() error {
    // Internal logging (debug information)
    c.logger.Info("Starting operation")

    // User-facing information
    c.logger.InfoToUser("Processing your request")

    // Success message shown to the user
    c.logger.Success("Operation completed successfully")

    return nil
}

Console Output

Console output is formatted with emoji prefixes to distinguish different message types:

  • Info: ℹ️ prefix (for InfoToUser messages)
  • Warning: ⚠️ prefix (only shown when verbose, unless WarningToUser)
  • Error: ❌ prefix
  • Success: ✅ prefix
  • Debug Log Enabled: 🔍 prefix

Messages directed specifically to users (InfoToUser, WarningToUser) are always displayed regardless of verbosity settings.

File Logging

When a log file is specified, all messages (regardless of verbosity settings) are written to the file. File logging includes timestamps and does not include ANSI color codes.

Resource Management

The Logger interface provides a Close method that should be called before application termination to ensure all buffered logs are flushed to disk:

defer logger.Close()

Thread Safety

The DefaultLogger implementation is safe for concurrent use by multiple goroutines. All logging methods can be called from different goroutines without additional synchronization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultLogger

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

DefaultLogger provides structured logging capability and implements the Logger interface

func NewWithOutput

func NewWithOutput(enabled bool, logFile string, verbose bool, stdout, stderr io.Writer) *DefaultLogger

NewWithOutput creates a DefaultLogger with custom output writers

func (*DefaultLogger) Close

func (l *DefaultLogger) Close() error

Close ensures any buffered data is written and closes open log file handles

func (*DefaultLogger) Error

func (l *DefaultLogger) Error(format string, args ...interface{})

Error logs an error message

func (*DefaultLogger) Info

func (l *DefaultLogger) Info(format string, args ...interface{})

Info logs an informational message (file only)

func (*DefaultLogger) InfoToUser

func (l *DefaultLogger) InfoToUser(format string, args ...interface{})

InfoToUser logs an informational message to both file and stdout

func (*DefaultLogger) SetStderr

func (l *DefaultLogger) SetStderr(w io.Writer)

SetStderr sets a custom writer for user-facing stderr messages only. NOTE: This does not affect where structured log messages from slog are directed. This method is thread-safe and is primarily intended for testing.

func (*DefaultLogger) SetStdout

func (l *DefaultLogger) SetStdout(w io.Writer)

SetStdout sets a custom writer for user-facing stdout messages only. NOTE: This does not affect where structured log messages from slog are directed. This method is thread-safe and is primarily intended for testing.

func (*DefaultLogger) StatusMessage

func (l *DefaultLogger) StatusMessage(format string, args ...interface{})

StatusMessage prints a status message to stdout only (no logging)

func (*DefaultLogger) Success

func (l *DefaultLogger) Success(format string, args ...interface{})

Success logs a success message to both file and stdout

func (*DefaultLogger) Warning

func (l *DefaultLogger) Warning(format string, args ...interface{})

Warning logs a warning message

func (*DefaultLogger) WarningToUser

func (l *DefaultLogger) WarningToUser(format string, args ...interface{})

WarningToUser logs a warning message to both file and stdout

type Logger

type Logger interface {

	// Info logs an informational message for debugging purposes.
	// These messages are typically only written to log files and are not shown to users
	// unless verbose mode is enabled.
	//
	// The format string follows fmt.Printf style formatting.
	Info(format string, args ...interface{})

	// Warning logs a warning message for debugging purposes.
	// These messages indicate potential issues that are not critical failures.
	// They are typically only written to log files and are not shown to users
	// unless verbose mode is enabled.
	//
	// The format string follows fmt.Printf style formatting.
	Warning(format string, args ...interface{})

	// Error logs an error message for debugging purposes.
	// These messages indicate operational failures or errors that occurred
	// during program execution. They are typically written to log files and
	// may also be shown to users depending on the logger implementation.
	//
	// The format string follows fmt.Printf style formatting.
	Error(format string, args ...interface{})

	// InfoToUser logs an informational message intended for users.
	// These messages are always shown to users regardless of verbose settings,
	// and are also written to log files.
	//
	// The format string follows fmt.Printf style formatting.
	InfoToUser(format string, args ...interface{})

	// WarningToUser logs a warning message intended for users.
	// These messages highlight important issues that users should be aware of,
	// and are always shown regardless of verbose settings.
	//
	// The format string follows fmt.Printf style formatting.
	WarningToUser(format string, args ...interface{})

	// Success logs a success message to the user.
	// These messages indicate successful completion of operations and are
	// typically styled differently (e.g., green text) to stand out.
	//
	// The format string follows fmt.Printf style formatting.
	Success(format string, args ...interface{})

	// StatusMessage logs a status message to the user.
	// These messages provide information about the current state of the application
	// and are always shown to users. They are typically used for displaying
	// configuration information, progress updates, and other operational status.
	//
	// The format string follows fmt.Printf style formatting.
	StatusMessage(format string, args ...interface{})

	// Close ensures any buffered data is written and closes open log file handles.
	// This should be called before the application exits to ensure all logs are properly saved.
	Close() error
}

Logger defines the common logging interface used throughout the application. It provides a standardized way to emit log messages at different levels of importance, with a clear separation between internal (debug) logs and user-facing messages.

The interface is designed to handle both internal logging needs (Info, Warning, Error) and user communication (InfoToUser, WarningToUser, Success, StatusMessage).

func New

func New(enabled bool, logFile string, verbose bool) Logger

New creates a new Logger instance

Jump to

Keyboard shortcuts

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