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")
}
Output:
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")
}
Output:
Index ¶
- func Debug(msg string)
- func Debugf(format string, args ...interface{})
- func Error(msg string)
- func Errorf(format string, args ...interface{})
- func Fatal(msg string)
- func Fatalf(format string, args ...interface{})
- func Info(msg string)
- func Infof(format string, args ...interface{})
- func Warn(msg string)
- func Warnf(format string, args ...interface{})
- type Fields
- type Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debugf ¶
func Debugf(format string, args ...interface{})
Debugf logs a formatted debug message using the default logger
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf logs a formatted error message using the default logger
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf logs a formatted error message and exits
Types ¶
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
}
Output:
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")
}
Output:
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")
}
}
Output:
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)
}
Output:
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")
}
Output:
func WithFields ¶
WithFields creates a new logger with fields using the default logger
Click to show internal directories.
Click to hide internal directories.