Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of filtering events to different logging handlers #23

Closed
Zsailer opened this issue Aug 7, 2019 · 1 comment
Closed

Example of filtering events to different logging handlers #23

Zsailer opened this issue Aug 7, 2019 · 1 comment

Comments

@Zsailer
Copy link
Member

Zsailer commented Aug 7, 2019

After chatting with some collaborators last week, the following use-case came up. They want two "event sinks" that collect different sets of events (mostly non-overlapping).

The current API design allows them to route the same set of events to two different logs.

I've been sketching out a design for this and curious what others think. This introduces a EventRouter object that takes a list of EventLog objects. Each EventLog has a different set of allowed_schemas (a.k.a. events to record) and handlers. The EventRouter interface has the same methods as the EventLog, but loops through the EventLogs, recording events that are listed in the allowed_schemas.

Here's an example of what the API might look like:

import logging
from jupyter_telemetry import EventLog, EventRouter

router = EventRouter([
    EventLog(
        handlers=[
            logging.FileHandler('events_set1.log')
        ],
        allowed_schemas=[
            'my.events/set1/event1',       # all events are coming from set 1.
            'my.events/set1/event2',
        ]
    ),
    Eventlog(
        handlers=[
            logging.FileHandler('events_set2.log')
        ],
        allowed_schemas=[
            'my.events/set2/event1',       # all events are coming from set 2.
            'my.events/set2/event2',
        ]
    )
])

# Log an event to 'events_set1.log' but not 'events_set2.log'
router.record_event(
    'my.events/set1/event1', 1,
    {...}
)
@Zsailer
Copy link
Member Author

Zsailer commented Aug 13, 2019

I realized this is the wrong approach. We should just let users leverage Python's standard logging library tools (and probably help them with documentation).

The ideal approach is using logging.Filter objects.

For example,

import logging

schema_set_1 = [
    'my.events/set1/event1',       # all events are coming from set 1.
    'my.events/set1/event2'
]

schema_set_2 = [
    'my.events/set2/event1',       # all events are coming from set 2.
    'my.events/set2/event2',
]

class FilterSet1(logging.Filter):
    """Filter all events that occur in set 1."""
    def filter(self, record):
        if record.msg['__schema__'] in schema_set_1:
            return True
        return False 

handler_set_1 = logging.FileHandler('events_set_1.log')
handler_set_1.addFilter(FilterSet1())

class FilterSet2(logging.Filter):
    """Filter all events that occur in set 2."""
    def filter(self, record):
        if record.msg['__schema__'] in schema_set_2:
            return True
        return False 

handler_set_2 = logging.FileHandler('events_set_2.log')
handler_set_2.addFilter(FilterSet2())

eventlog = EventLog(
    handlers=[
        handler_set_1,
        handler_set_2
    ],
    allowed_schemas=schema_set_1 + schema_set_2
)

@Zsailer Zsailer changed the title multiple eventlogs and an event router class Example of filtering events to different logging handlers Aug 13, 2019
@Zsailer Zsailer closed this as completed May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant