-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_eventbus_custom_logger.py
More file actions
57 lines (40 loc) · 1.41 KB
/
14_eventbus_custom_logger.py
File metadata and controls
57 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Custom Logger Configuration
Demonstrates:
- Passing custom logger to EventBus
- Configuring logging for EventBus operations
- Using custom logger names and levels
"""
import asyncio
import logging
from opensecflow.eventbus.memory_broker import AsyncQueueBroker
from opensecflow.eventbus.eventbus import EventBus
from opensecflow.eventbus.event import ScopedEvent, EventScope
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
async def main():
"""Using custom logger with EventBus"""
print("\n=== Custom Logger Configuration ===\n")
# Create custom logger
custom_logger = logging.getLogger("my_eventbus")
custom_logger.setLevel(logging.DEBUG)
process_broker = AsyncQueueBroker()
app_broker = AsyncQueueBroker()
# Pass custom logger to EventBus
bus = EventBus(process_broker, app_broker, logger=custom_logger)
class CustomEvent(ScopedEvent):
type: str = "custom.event"
data: str
scope: EventScope = EventScope.PROCESS
async def handler(event_data: dict):
print(f" ✅ Handler executed: {event_data}")
bus.subscribe("custom.event", handler)
await bus.start()
event = CustomEvent(source="custom-service", data="test data")
await bus.publish(event)
await asyncio.sleep(0.1)
await bus.stop()
if __name__ == "__main__":
asyncio.run(main())