logger

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package logger provides structured logging capabilities for CCMD.

Example (GlobalFunctions)
package main

import (
	"errors"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	// Use global logger functions
	logger.Info("Application starting")
	logger.Debug("Debug mode enabled")

	// With formatting
	logger.Infof("Listening on port %d", 8080)
	logger.Errorf("Failed to bind to port %d", 8080)

	// With fields
	logger.WithField("version", "1.0.0").Info("Application initialized")
	logger.WithFields(logger.Fields{
		"user": "admin",
		"role": "superuser",
	}).Info("User logged in")

	// With error
	err := errors.New("file not found")
	logger.WithError(err).Error("Failed to load configuration")
}
Example (PackageInstallation)
package main

import (
	"fmt"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	// Real-world example: package installation logging
	log := logger.WithField("component", "installer")

	packageName := "example-cli"
	repository := "github.com/user/example-cli"

	log.WithFields(logger.Fields{
		"package":    packageName,
		"repository": repository,
	}).Info("Starting package installation")

	// Simulate installation steps
	log.WithField("step", "download").Info("Downloading package")
	log.WithField("step", "extract").Info("Extracting files")
	log.WithField("step", "build").Info("Building package")

	// Handle potential error
	err := fmt.Errorf("build failed: exit code 1")
	if err != nil {
		log.WithFields(logger.Fields{
			"package": packageName,
			"step":    "build",
		}).WithError(err).Error("Installation failed")
		return
	}

	log.WithField("package", packageName).Info("Installation completed successfully")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

Debug logs a debug message using the default logger

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a formatted debug message using the default logger

func Error

func Error(msg string)

Error logs an error message using the default logger

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs a formatted error message using the default logger

func Fatal

func Fatal(msg string)

Fatal logs an error message and exits

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs a formatted error message and exits

func Info

func Info(msg string)

Info logs an info message using the default logger

func Infof

func Infof(format string, args ...interface{})

Infof logs a formatted info message using the default logger

func Warn

func Warn(msg string)

Warn logs a warning message using the default logger

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs a formatted warning message using the default logger

Types

type Fields

type Fields map[string]interface{}

Fields represents structured fields for logging

type Logger

type Logger interface {
	Debug(msg string)
	Debugf(format string, args ...interface{})
	Info(msg string)
	Infof(format string, args ...interface{})
	Warn(msg string)
	Warnf(format string, args ...interface{})
	Error(msg string)
	Errorf(format string, args ...interface{})
	Fatal(msg string)
	Fatalf(format string, args ...interface{})
	WithField(key string, value interface{}) Logger
	WithFields(fields Fields) Logger
	WithError(err error) Logger
}

Logger interface defines the logging contract

Example (Basic)
package main

import (
	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	// Create a logger
	log := logger.New()

	// Log at different levels
	log.Debug("This might not be shown depending on log level")
	log.Info("Application started")
	log.Warn("Low memory")
	log.Error("Connection failed")

	// Note: Since we're using slog internally, output goes to stderr
}
Example (Contextual)
package main

import (
	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	baseLog := logger.New()

	// Create component-specific loggers
	dbLog := baseLog.WithField("component", "database")
	apiLog := baseLog.WithField("component", "api")

	// Use them throughout the component
	dbLog.Info("Connecting to database")
	dbLog.WithField("pool_size", 10).Info("Connection pool initialized")

	apiLog.Info("Starting API server")
	apiLog.WithField("port", 8080).Info("Listening for requests")
}
Example (ErrorHandling)
package main

import (
	"errors"

	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	log := logger.New()

	// Log with error context
	err := errors.New("connection timeout")
	log.WithError(err).Error("Failed to connect to database")

	// Log with multiple contexts
	log.WithFields(logger.Fields{
		"host":    "db.example.com",
		"port":    5432,
		"timeout": "30s",
	}).WithError(err).Error("Database connection failed")

	// Conditional error logging
	if err != nil {
		log.WithField("operation", "user_create").
			WithError(err).
			Error("Operation failed")
	}
}
Example (Formatted)
package main

import (
	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	log := logger.New()

	// Use formatted logging methods
	count := 42
	name := "test-command"

	log.Infof("Processing %d items", count)
	log.Debugf("Installing command: %s", name)
	log.Warnf("Retrying operation, attempt %d of %d", 2, 3)
	log.Errorf("Failed to download %s: timeout after %d seconds", name, 30)
}
Example (WithFields)
package main

import (
	"github.com/gifflet/ccmd/pkg/logger"
)

func main() {
	log := logger.New()

	// Add fields to provide context
	log.WithFields(logger.Fields{
		"user":   "john",
		"action": "login",
		"ip":     "127.0.0.1",
	}).Info("User authentication successful")

	// Chain multiple fields
	log.WithField("request_id", "12345").
		WithField("method", "GET").
		WithField("path", "/api/users").
		Info("API request received")
}

func Default

func Default() Logger

Default creates a logger with default settings

func GetDefault

func GetDefault() Logger

GetDefault returns the default logger

func New

func New() Logger

New creates a new logger instance

func WithError

func WithError(err error) Logger

WithError creates a new logger with an error field using the default logger

func WithField

func WithField(key string, value interface{}) Logger

WithField creates a new logger with a field using the default logger

func WithFields

func WithFields(fields Fields) Logger

WithFields creates a new logger with fields using the default logger

Jump to

Keyboard shortcuts

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