Documentation
¶
Overview ¶
Package logger provides a structured logging system for the projectmemory service.
Index ¶
- func Debug(msg string, args ...interface{})
- func Error(msg string, args ...interface{})
- func Fatal(msg string, args ...interface{})
- func Info(msg string, args ...interface{})
- func IsErrorType(err error, errType ErrorType) bool
- func LogError(err error)
- func SetDefaultLogger(logger *Logger)
- func Warn(msg string, args ...interface{})
- type AppError
- func APIError(err error, message string) *AppError
- func ConfigError(err error, message string) *AppError
- func DatabaseError(err error, message string) *AppError
- func ExternalError(err error, message string) *AppError
- func InternalError(err error, message string) *AppError
- func NetworkError(err error, message string) *AppError
- func NewError(err error, errType ErrorType, message string) *AppError
- func PermissionError(err error, message string) *AppError
- func ValidationError(err error, message string) *AppError
- type Config
- type ErrorType
- type LogFormat
- type LogLevel
- type Logger
- func (l *Logger) Debug(msg string, args ...interface{})
- func (l *Logger) DebugContext(ctx string, msg string, args ...interface{})
- func (l *Logger) Error(msg string, args ...interface{})
- func (l *Logger) ErrorContext(ctx string, msg string, args ...interface{})
- func (l *Logger) Fatal(msg string, args ...interface{})
- func (l *Logger) FatalContext(ctx string, msg string, args ...interface{})
- func (l *Logger) Info(msg string, args ...interface{})
- func (l *Logger) InfoContext(ctx string, msg string, args ...interface{})
- func (l *Logger) SetFormat(format LogFormat)
- func (l *Logger) SetLevel(level LogLevel)
- func (l *Logger) Warn(msg string, args ...interface{})
- func (l *Logger) WarnContext(ctx string, msg string, args ...interface{})
- func (l *Logger) WithContext(contexts ...string) *Logger
- func (l *Logger) WithField(key string, value interface{}) *Logger
- func (l *Logger) WithFields(fields map[string]interface{}) *Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
func Debug(msg string, args ...interface{})
Debug logs to the default logger at DEBUG level
func Error ¶
func Error(msg string, args ...interface{})
Error logs to the default logger at ERROR level
func Fatal ¶
func Fatal(msg string, args ...interface{})
Fatal logs to the default logger at FATAL level and then exits
func Info ¶
func Info(msg string, args ...interface{})
Info logs to the default logger at INFO level
func IsErrorType ¶
IsErrorType checks if an error is of a specific ErrorType
func SetDefaultLogger ¶
func SetDefaultLogger(logger *Logger)
SetDefaultLogger sets the global default logger
Types ¶
type AppError ¶
type AppError struct {
// Original error that we're wrapping
Err error
// Error type category
Type ErrorType
// Message provides additional context
Message string
// Fields contains additional structured data about the error
Fields map[string]interface{}
// Stack contains the call stack when the error was created
Stack []string
}
AppError represents a structured error with context
func ConfigError ¶
ConfigError creates a configuration error
func DatabaseError ¶
DatabaseError creates a database error
func ExternalError ¶
ExternalError creates an error from an external system
func InternalError ¶
InternalError creates an internal system error
func NetworkError ¶
NetworkError creates a network error
func PermissionError ¶
PermissionError creates a permission error
func ValidationError ¶
ValidationError creates a validation error
func (*AppError) WithFields ¶
WithFields adds multiple fields to the error
type Config ¶
type Config struct {
Level LogLevel
Format LogFormat
Output io.Writer
DefaultTags map[string]interface{}
}
Config holds configuration options for the logger
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a default logger configuration
type ErrorType ¶
type ErrorType string
ErrorType represents different categories of errors in the system
const ( // ErrorTypeUnknown is used when the error type is not specified ErrorTypeUnknown ErrorType = "unknown" // ErrorTypeValidation indicates an input validation error ErrorTypeValidation ErrorType = "validation" // ErrorTypeDatabase indicates a database-related error ErrorTypeDatabase ErrorType = "database" // ErrorTypeNetwork indicates a network-related error ErrorTypeNetwork ErrorType = "network" // ErrorTypeAPI indicates an API-related error ErrorTypeAPI ErrorType = "api" // ErrorTypePermission indicates a permission-related error ErrorTypePermission ErrorType = "permission" // ErrorTypeConfiguration indicates a configuration-related error ErrorTypeConfiguration ErrorType = "configuration" // ErrorTypeExternal indicates an error from an external system ErrorTypeExternal ErrorType = "external" // ErrorTypeInternal indicates an internal system error ErrorTypeInternal ErrorType = "internal" )
Error types used throughout the system
type LogLevel ¶
type LogLevel int
LogLevel represents the severity of a log message
func ParseLevel ¶
ParseLevel converts a string level to a LogLevel
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger represents a structured logger
func GetDefaultLogger ¶
func GetDefaultLogger() *Logger
GetDefaultLogger returns the global default logger
func (*Logger) DebugContext ¶
DebugContext logs a message at DEBUG level with context
func (*Logger) ErrorContext ¶
ErrorContext logs a message at ERROR level with context
func (*Logger) FatalContext ¶
FatalContext logs a message at FATAL level with context and then exits
func (*Logger) InfoContext ¶
InfoContext logs a message at INFO level with context
func (*Logger) WarnContext ¶
WarnContext logs a message at WARN level with context
func (*Logger) WithContext ¶
WithContext returns a new logger with a context path
Example ¶
// This example shows how to use contextual logging
var buf bytes.Buffer
logger := New(&Config{
Level: DEBUG,
Format: TEXT,
Output: &buf,
})
// Create a component logger
componentLogger := logger.WithContext("auth", "login")
// Log with the component context
componentLogger.Info("User login successful")
// The output would include the context path [auth.login]
fmt.Println("Contains context:", strings.Contains(buf.String(), "[auth.login]"))
Output: Contains context: true
func (*Logger) WithFields ¶
WithFields returns a new logger with multiple fields added to its context