Documentation
¶
Overview ¶
This file provides examples for integrating the logging package with the MCP server application.
Package logging provides a wrapper around zap for structured logging
Example ¶
package main import ( "context" "fmt" "github.com/FreePeak/cortex/internal/infrastructure/logging" ) func main() { // Create a development logger (with debug level enabled) logger, err := logging.NewDevelopment() if err != nil { panic(err) } defer func() { _ = logger.Sync() // Intentionally ignoring sync errors in examples }() // Simple logging logger.Debug("Debug message") logger.Info("Info message") logger.Warn("Warning message") logger.Error("Error message") // logger.Fatal("Fatal message") // This would exit the program // logger.Panic("Panic message") // This would panic // Logging with fields logger.Info("User logged in", logging.Fields{ "user_id": 123, "email": "user@example.com", }) // Using With to create a logger with default fields userLogger := logger.With(logging.Fields{ "user_id": 123, "email": "user@example.com", }) userLogger.Info("User profile updated") userLogger.Error("Failed to update password") // Formatted logging (using sugar) logger.Infof("User %d logged in from %s", 123, "192.168.1.1") logger.Errorf("Failed to process payment: %v", fmt.Errorf("insufficient funds")) // Context-aware logging ctx := context.Background() logger.InfoContext(ctx, "Starting operation") logger.ErrorContext(ctx, "Operation failed", logging.Fields{ "error": "connection timeout", }) // Using the default logger (production level) defaultLogger := logging.Default() defaultLogger.Info("Using default logger") }
Example (CustomConfig) ¶
package main import ( "github.com/FreePeak/cortex/internal/infrastructure/logging" ) func main() { // Create a custom logger configuration config := logging.Config{ Level: logging.DebugLevel, Development: true, OutputPaths: []string{"stdout", "logs/app.log"}, InitialFields: logging.Fields{ "app": "example-app", "env": "testing", }, } // Create a logger with custom config logger, err := logging.New(config) if err != nil { panic(err) } defer func() { _ = logger.Sync() // Intentionally ignoring sync errors in examples }() logger.Info("Application started") }
Example (ProductionLogger) ¶
package main import ( "github.com/FreePeak/cortex/internal/infrastructure/logging" ) func main() { // Create a production logger logger, err := logging.NewProduction() if err != nil { panic(err) } defer func() { _ = logger.Sync() // Intentionally ignoring sync errors in examples }() // Production loggers typically use JSON format // and have DEBUG level disabled logger.Debug("This won't be logged in production") // Not shown logger.Info("System is running") logger.Error("Failed to connect to database", logging.Fields{ "error": "connection refused", "database": "postgres", "reconnect": true, }) }
Index ¶
- func LogJSONRPCRequest(ctx context.Context, request domain.JSONRPCRequest)
- func LogJSONRPCResponse(ctx context.Context, response domain.JSONRPCResponse)
- func Middleware(logger *Logger) func(http.Handler) http.Handler
- func ServerStartupLogger(logger *Logger, serverName, version, address string)
- func SetDefault(logger *Logger)
- type Config
- type Fields
- type LogLevel
- type Logger
- func (l *Logger) Debug(msg string, fields ...Fields)
- func (l *Logger) DebugContext(ctx context.Context, msg string, fields ...Fields)
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Error(msg string, fields ...Fields)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, fields ...Fields)
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Fatal(msg string, fields ...Fields)
- func (l *Logger) FatalContext(ctx context.Context, msg string, fields ...Fields)
- func (l *Logger) Fatalf(format string, args ...interface{})
- func (l *Logger) Info(msg string, fields ...Fields)
- func (l *Logger) InfoContext(ctx context.Context, msg string, fields ...Fields)
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) Panic(msg string, fields ...Fields)
- func (l *Logger) PanicContext(ctx context.Context, msg string, fields ...Fields)
- func (l *Logger) Panicf(format string, args ...interface{})
- func (l *Logger) Sync() error
- func (l *Logger) Warn(msg string, fields ...Fields)
- func (l *Logger) WarnContext(ctx context.Context, msg string, fields ...Fields)
- func (l *Logger) Warnf(format string, args ...interface{})
- func (l *Logger) With(fields Fields) *Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LogJSONRPCRequest ¶
func LogJSONRPCRequest(ctx context.Context, request domain.JSONRPCRequest)
LogJSONRPCRequest logs JSON-RPC request details
func LogJSONRPCResponse ¶
func LogJSONRPCResponse(ctx context.Context, response domain.JSONRPCResponse)
LogJSONRPCResponse logs JSON-RPC response details
func Middleware ¶
Middleware creates an HTTP middleware that adds a logger to the request context.
func ServerStartupLogger ¶
ServerStartupLogger logs server startup information
Types ¶
type Config ¶
type Config struct { Level LogLevel Development bool OutputPaths []string InitialFields Fields LogToFile bool LogDir string }
Config represents the logging configuration
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a default configuration for the logger
func DevelopmentConfig ¶
func DevelopmentConfig() Config
DevelopmentConfig returns a development configuration for the logger
func ProductionConfig ¶
func ProductionConfig() Config
ProductionConfig returns a production configuration for the logger
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a wrapper around zap.Logger providing a simplified API
func GetLogger ¶
GetLogger retrieves the logger from the context. If no logger is found, returns a default logger.
func NewDevelopment ¶
NewDevelopment creates a new development logger
func NewProduction ¶
NewProduction creates a new production logger
func WithRequestID ¶
WithRequestID returns a new logger with the request ID field
func (*Logger) DebugContext ¶
DebugContext logs a message at debug level with context and optional fields
func (*Logger) ErrorContext ¶
ErrorContext logs a message at error level with context and optional fields
func (*Logger) Fatal ¶
Fatal logs a message at fatal level with optional fields and then calls os.Exit(1)
func (*Logger) FatalContext ¶
FatalContext logs a message at fatal level with context and optional fields and then calls os.Exit(1)
func (*Logger) InfoContext ¶
InfoContext logs a message at info level with context and optional fields
func (*Logger) PanicContext ¶
PanicContext logs a message at panic level with context and optional fields and then panics
func (*Logger) WarnContext ¶
WarnContext logs a message at warn level with context and optional fields