-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9d5e5d
commit 1eee188
Showing
1 changed file
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |