This example demonstrates the Observer pattern implementation in the Modular framework. It shows how to:
- Use
ObservableApplicationfor automatic event emission - Create modules that implement the
Observerinterface - Register observers for specific event types
- Emit custom events from modules
- Use the
EventLoggermodule for structured event logging - Handle errors gracefully in observers
- Automatically emits events for module/service registration and application lifecycle
- Thread-safe observer management
- Event filtering by type
- Multiple output targets (console, file, syslog)
- Configurable log levels and formats
- Event type filtering
- Async processing with buffering
- UserModule: Emits custom events for user operations
- NotificationModule: Observes user events and sends notifications
- AuditModule: Observes all events for compliance logging
- Framework events:
module.registered,service.registered,application.started - Custom events:
user.created,user.login
go run .go run . --generate-config yaml config-sample.yamlYou can override configuration using environment variables:
EVENTLOGGER_LOGLEVEL=DEBUG go run .
USERMODULE_MAXUSERS=50 go run .When you run the example, you'll see:
- Application startup events logged by EventLogger
- Module registration events for each registered module
- Service registration events for registered services
- Custom user events when users are created and log in
- Notification handling by the NotificationModule
- Audit logging of all events by the AuditModule
- Application shutdown events during graceful shutdown
The example uses a comprehensive configuration that demonstrates:
- EventLogger with console output and optional file logging
- Configurable log levels and formats
- Event type filtering options
- User module configuration
- ObservableApplication emits framework lifecycle events
- EventLogger observes all events and logs them to configured outputs
- UserModule emits custom events for business operations
- NotificationModule observes user events and sends notifications
- AuditModule observes all events for compliance and security
main.go- Application setup and coordinationuser_module.go- Demonstrates event emission and observationnotification_module.go- Demonstrates event-driven notificationsaudit_module.go- Demonstrates comprehensive event auditingconfig.yaml- Configuration with event logging setup
- Observer Registration: How to register observers for specific event types
- Event Emission: How modules can emit custom events
- Error Handling: How observer errors are handled gracefully
- Configuration: How to configure event logging and filtering
- Integration: How the Observer pattern integrates with the existing framework
Run the example and observe the detailed event logging that shows the Observer pattern in action. The output demonstrates:
- Real-time event processing
- Event filtering and routing
- Error handling and recovery
- Performance with async processing