Skip to content

Commit

Permalink
Slight tweak to events
Browse files Browse the repository at this point in the history
  • Loading branch information
LuckierDodge committed Nov 26, 2024
1 parent b9d5e5d commit 1eee188
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions madsci/madsci_common/madsci/common/events.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,55 @@
"""MADSci Event Handling."""

import logging
from typing import Optional

from madsci.common.types.event_types import Event


class MADSciEventLogger:
"""A logger for MADSci events."""

def __init__(self, name: str = __name__, log_level: int = logging.NOTSET) -> None:
def __init__(
self, name: str = None, log_level: int = logging.NOTSET, server_url: str = None
) -> None:
"""Initialize the event logger."""
self.logger = logging.getLogger(name)
if name:
self.logger = logging.getLogger(__name__ + "." + name)
else:
self.logger = logging.getLogger(__name__)
self.logger.setLevel(log_level)
self.server_url = server_url

def log_event(self, event: Event) -> None:
def log(self, event: Event, level: Optional[int] = None) -> None:
"""Log an event."""
event.log_level = level if level else event.log_level
logging.log(event.log_level, event.event_data)
if self.server_url:
self.send_event(event)

def log_debug(self, event: Event) -> None:
"""Log an event at the debug level."""
self.log(event, logging.DEBUG)

def log_info(self, event: Event) -> None:
"""Log an event at the info level."""
self.log(event, logging.INFO)

def log_warning(self, event: Event) -> None:
"""Log an event at the warning level."""
self.log(event, logging.WARNING)

def log_error(self, event: Event) -> None:
"""Log an event at the error level."""
self.log(event, logging.ERROR)

def log_critical(self, event: Event) -> None:
"""Log an event at the critical level."""
self.log(event, logging.CRITICAL)

def send_event(self, event: Event) -> None:
"""Send an event to the event manager."""
pass


default_event_logger = MADSciEventLogger()

0 comments on commit 1eee188

Please sign in to comment.