Documentation
¶
Overview ¶
Copyright (c) 2025 Guilherme Silva Sousa Licensed under the MIT License See LICENSE file in the project root for full license information. Package logger provides structured logging capabilities for CCMD.
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 SetDefault(l Logger)
- func Warn(msg string)
- func Warnf(format string, args ...interface{})
- type Fields
- type Level
- 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 fatal message using the default logger and exits
Types ¶
type Logger ¶
type Logger interface {
Debug(msg string)
Debugf(format string, args ...interface{})
DebugWithFields(msg string, fields Fields)
Info(msg string)
Infof(format string, args ...interface{})
InfoWithFields(msg string, fields Fields)
Warn(msg string)
Warnf(format string, args ...interface{})
WarnWithFields(msg string, fields Fields)
Error(msg string)
Errorf(format string, args ...interface{})
ErrorWithFields(msg string, fields Fields)
Fatal(msg string)
Fatalf(format string, args ...interface{})
FatalWithFields(msg string, fields Fields)
WithField(key string, value interface{}) Logger
WithFields(fields Fields) Logger
WithError(err error) Logger
SetLevel(level Level)
GetLevel() Level
}
Logger interface defines the logging contract
Example (Basic) ¶
package main
import (
"bytes"
"fmt"
"github.com/gifflet/ccmd/pkg/logger"
)
func main() {
// Create a logger writing to a buffer for testing
var buf bytes.Buffer
log := logger.New(&buf, logger.InfoLevel)
// Log at different levels
log.Debug("This won't be shown") // Below threshold
log.Info("Application started")
log.Warn("Low memory")
log.Error("Connection failed")
// Check output (timestamps will vary)
output := buf.String()
fmt.Println(len(output) > 0)
}
Output: true
Example (Chaining) ¶
package main
import (
"bytes"
"fmt"
"github.com/gifflet/ccmd/pkg/logger"
)
func main() {
var buf bytes.Buffer
baseLog := logger.New(&buf, logger.InfoLevel)
// Create specialized loggers
apiLog := baseLog.WithField("component", "api")
authLog := apiLog.WithField("module", "auth")
// Each logger inherits parent fields
authLog.Info("Authentication successful")
output := buf.String()
fmt.Println(len(output) > 0)
}
Output: true
Example (WithError) ¶
package main
import (
"bytes"
"fmt"
"github.com/gifflet/ccmd/pkg/logger"
)
func main() {
var buf bytes.Buffer
log := logger.New(&buf, logger.ErrorLevel)
// Log with error context
err := fmt.Errorf("database connection failed")
log.WithError(err).Error("Failed to process request")
output := buf.String()
fmt.Println(len(output) > 0)
}
Output: true
Example (WithFields) ¶
package main
import (
"bytes"
"fmt"
"github.com/gifflet/ccmd/pkg/logger"
)
func main() {
var buf bytes.Buffer
log := logger.New(&buf, logger.InfoLevel)
// Add fields to provide context
log.WithFields(logger.Fields{
"user_id": 123,
"action": "login",
}).Info("User logged in")
// Fields are included in output
output := buf.String()
fmt.Println(len(output) > 0)
}
Output: true
func Default ¶
func Default() Logger
Default creates a logger with default settings
Example ¶
package main
import (
"github.com/gifflet/ccmd/pkg/logger"
)
func main() {
// Use the default logger
logger.Debug("Debug message")
logger.Info("Info message")
logger.Warn("Warning message")
logger.Error("Error message")
// With fields
logger.WithField("request_id", "abc123").Info("Processing request")
// With multiple fields
logger.WithFields(logger.Fields{
"user": "john",
"action": "upload",
"size": 1024,
}).Info("File uploaded")
}
Output:
func WithFields ¶
WithFields creates a new logger with fields using the default logger
Click to show internal directories.
Click to hide internal directories.