diff --git a/madsci/madsci_common/madsci/common/events.py b/madsci/madsci_common/madsci/common/events.py index b351491..7fdb1ef 100644 --- a/madsci/madsci_common/madsci/common/events.py +++ b/madsci/madsci_common/madsci/common/events.py @@ -1,6 +1,7 @@ """MADSci Event Handling.""" import logging +from typing import Optional from madsci.common.types.event_types import Event @@ -8,11 +9,47 @@ 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()