logger

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logger provides a structured logging system for the projectmemory service.

Index

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

func IsErrorType(err error, errType ErrorType) bool

IsErrorType checks if an error is of a specific ErrorType

func LogError

func LogError(err error)

LogError logs the error with appropriate context

func SetDefaultLogger

func SetDefaultLogger(logger *Logger)

SetDefaultLogger sets the global default logger

func Warn

func Warn(msg string, args ...interface{})

Warn logs to the default logger at WARN level

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 APIError

func APIError(err error, message string) *AppError

APIError creates an API error

func ConfigError

func ConfigError(err error, message string) *AppError

ConfigError creates a configuration error

func DatabaseError

func DatabaseError(err error, message string) *AppError

DatabaseError creates a database error

func ExternalError

func ExternalError(err error, message string) *AppError

ExternalError creates an error from an external system

func InternalError

func InternalError(err error, message string) *AppError

InternalError creates an internal system error

func NetworkError

func NetworkError(err error, message string) *AppError

NetworkError creates a network error

func NewError

func NewError(err error, errType ErrorType, message string) *AppError

NewError creates a new structured error

func PermissionError

func PermissionError(err error, message string) *AppError

PermissionError creates a permission error

func ValidationError

func ValidationError(err error, message string) *AppError

ValidationError creates a validation error

func (*AppError) Error

func (e *AppError) Error() string

Error returns the error string

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying error

func (*AppError) WithField

func (e *AppError) WithField(key string, value interface{}) *AppError

WithField adds a field to the error

func (*AppError) WithFields

func (e *AppError) WithFields(fields map[string]interface{}) *AppError

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 LogFormat

type LogFormat int

LogFormat defines how log messages are formatted

const (
	TEXT LogFormat = iota
	JSON
)

Log format constants

type LogLevel

type LogLevel int

LogLevel represents the severity of a log message

const (
	DEBUG LogLevel = iota
	INFO
	WARN
	ERROR
	FATAL
	DISABLED
)

Log level constants

func ParseLevel

func ParseLevel(level string) LogLevel

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 GetLogger

func GetLogger(name string) *Logger

GetLogger returns a logger with the given name as a field

func New

func New(config *Config) *Logger

New creates a new logger with the given configuration

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...interface{})

Debug logs a message at DEBUG level

func (*Logger) DebugContext

func (l *Logger) DebugContext(ctx string, msg string, args ...interface{})

DebugContext logs a message at DEBUG level with context

func (*Logger) Error

func (l *Logger) Error(msg string, args ...interface{})

Error logs a message at ERROR level

func (*Logger) ErrorContext

func (l *Logger) ErrorContext(ctx string, msg string, args ...interface{})

ErrorContext logs a message at ERROR level with context

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...interface{})

Fatal logs a message at FATAL level and then exits with status code 1

func (*Logger) FatalContext

func (l *Logger) FatalContext(ctx string, msg string, args ...interface{})

FatalContext logs a message at FATAL level with context and then exits

func (*Logger) Info

func (l *Logger) Info(msg string, args ...interface{})

Info logs a message at INFO level

func (*Logger) InfoContext

func (l *Logger) InfoContext(ctx string, msg string, args ...interface{})

InfoContext logs a message at INFO level with context

func (*Logger) SetFormat

func (l *Logger) SetFormat(format LogFormat)

SetFormat sets the logger's output format

func (*Logger) SetLevel

func (l *Logger) SetLevel(level LogLevel)

SetLevel sets the logger's minimum log level

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...interface{})

Warn logs a message at WARN level

func (*Logger) WarnContext

func (l *Logger) WarnContext(ctx string, msg string, args ...interface{})

WarnContext logs a message at WARN level with context

func (*Logger) WithContext

func (l *Logger) WithContext(contexts ...string) *Logger

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) WithField

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

WithField returns a new logger with the field added to its context

func (*Logger) WithFields

func (l *Logger) WithFields(fields map[string]interface{}) *Logger

WithFields returns a new logger with multiple fields added to its context

Jump to

Keyboard shortcuts

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