-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from meaningfy-ws/feature/TED-180
Feature/ted 180
- Loading branch information
Showing
12 changed files
with
288 additions
and
6 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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import abc | ||
import enum | ||
import logging | ||
|
||
import logstash | ||
|
||
from ted_sws import config | ||
|
||
|
||
class LoggingType(enum.Enum): | ||
ELK = "ELK" | ||
PY = "PY" | ||
|
||
|
||
DOMAIN_LOGGING_TYPES = config.LOGGING_TYPE.split(",") if config.LOGGING_TYPE is not None else [LoggingType.PY.value] | ||
DOMAIN_LOGGING_TYPE = DOMAIN_LOGGING_TYPES[0] | ||
|
||
|
||
class LoggerABC(abc.ABC): | ||
""" | ||
This abstract class provides methods definitions and infos for available loggers | ||
""" | ||
pass | ||
|
||
|
||
class Logger(LoggerABC): | ||
""" | ||
This class provides common features for available loggers | ||
""" | ||
|
||
def __init__(self, name: str = __name__): | ||
self.name = name | ||
self.logger = logging.getLogger(name) | ||
self.level = logging.DEBUG | ||
self.logger.setLevel(self.level) | ||
|
||
def get_logger(self) -> logging.Logger: | ||
return self.logger | ||
|
||
def log(self, msg: str): | ||
msg = "\n" + self.name + "\n" + msg + "\n" | ||
self.logger.log(self.level, msg) | ||
|
||
|
||
class LoggerFactory: | ||
@classmethod | ||
def get(cls, logging_type: LoggingType = LoggingType(DOMAIN_LOGGING_TYPE), name: str = __name__): | ||
"""Factory Method to return the needed Logger, based on logging type/target""" | ||
loggers = { | ||
LoggingType.ELK: ELKLogger, | ||
LoggingType.PY: PYLogger | ||
} | ||
name = "{logging_type} :: {name}".format(logging_type=logging_type, name=name) | ||
return loggers[logging_type](name=name) | ||
|
||
|
||
class ELKLogger(Logger): | ||
""" | ||
LogStash (ELK) Logger | ||
""" | ||
|
||
def __init__(self, name: str = 'logstash-logger'): | ||
super().__init__(name=name) | ||
|
||
host = config.ELK_HOST | ||
port = config.ELK_PORT | ||
version = config.ELK_VERSION | ||
|
||
self.logger.addHandler(logstash.LogstashHandler(host, port, version=version)) | ||
# self.logger.addHandler(logstash.TCPLogstashHandler(host, port, version=version)) | ||
|
||
|
||
class PYLogger(Logger): | ||
""" | ||
Python Logger | ||
""" | ||
|
||
def __init__(self, name: str = __name__): | ||
super().__init__(name=name) | ||
|
||
|
||
logger = LoggerFactory.get(name="domain-logging") |
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
from typing import Callable, List, Dict | ||
|
||
from pymessagebus import MessageBus as PyMessageBus | ||
from pymessagebus.api import Middleware | ||
from pymessagebus.middleware.logger import get_logger_middleware, LoggingMiddlewareConfig | ||
|
||
from ted_sws.core.adapters.logger import logger, Logger | ||
from ted_sws.core.domain import message_handlers | ||
from ted_sws.core.model import message | ||
|
||
""" | ||
Here is instantiated/initialized domain message_bus service | ||
""" | ||
|
||
|
||
class MessageBus(PyMessageBus): | ||
""" | ||
This class provides additional features to MessageBus | ||
""" | ||
HAS_LOGGING_MIDDLEWARE = True | ||
_logger: Logger = logger | ||
|
||
def set_domain_logger(self, _logger: Logger): | ||
self._logger = _logger | ||
|
||
def get_domain_logger(self) -> Logger: | ||
return self._logger | ||
|
||
def set_middlewares(self, _middlewares: List[Middleware] = None): | ||
self._middlewares_chain = self._get_middlewares_callables_chain( | ||
_middlewares, self._trigger_handlers_for_message_as_a_middleware | ||
) | ||
|
||
def add_handlers(self, handlers: Dict[type, List[Callable]]) -> None: | ||
for message_class in handlers: | ||
for message_handler in handlers[message_class]: | ||
super().add_handler(message_class, message_handler) | ||
|
||
|
||
message_bus = MessageBus() | ||
|
||
middlewares: List = [] | ||
if MessageBus.HAS_LOGGING_MIDDLEWARE: | ||
logging_middleware_config = LoggingMiddlewareConfig( | ||
mgs_received_level=logging.INFO, | ||
mgs_succeeded_level=logging.INFO, | ||
mgs_failed_level=logging.CRITICAL | ||
) | ||
logging_middleware = get_logger_middleware(message_bus.get_domain_logger().get_logger(), logging_middleware_config) | ||
middlewares.append(logging_middleware) | ||
|
||
if len(middlewares) > 0: | ||
message_bus.set_middlewares(middlewares) | ||
|
||
message_bus.add_handlers({ | ||
message.Log: [ | ||
message_handlers.handler_log | ||
] | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from ted_sws.core.adapters.logger import LoggerFactory, LoggingType, DOMAIN_LOGGING_TYPES | ||
from ted_sws.core.model import message | ||
|
||
""" | ||
Here are defined all core/domain messages' handlers | ||
""" | ||
|
||
|
||
def handler_log(log: message.Log): | ||
""" | ||
Here is defined the handler for Log Message event | ||
:param log: | ||
:return: | ||
""" | ||
|
||
eol = log.format.new_line | ||
msg = "" | ||
if log.title: | ||
msg += ("{title}" + eol).format(title=log.title) | ||
if log.messages: | ||
msg += ("Messages: " + eol + "{messages}" + eol).format( | ||
messages=eol.join(map(lambda m: " - " + m, log.messages)) | ||
) | ||
|
||
for logging_type_value in DOMAIN_LOGGING_TYPES: | ||
_logger = LoggerFactory.get(LoggingType(logging_type_value), name=logging_type_value + "-logging") | ||
_logger.log(msg) | ||
return msg |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Optional, List | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class MessageFormat(BaseModel): | ||
new_line: str = "\n" | ||
|
||
|
||
class Message(BaseModel): | ||
title: Optional[str] = None | ||
messages: Optional[List[str]] = [] | ||
format: Optional[MessageFormat] = MessageFormat() | ||
|
||
|
||
class Log(Message): | ||
pass |
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
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/python3 | ||
|
||
""" | ||
""" | ||
|
||
from ted_sws.core.domain.message_bus import message_bus | ||
from ted_sws.core.model.message import Log | ||
from ted_sws.core.adapters.logger import Logger, LoggingType, LoggerFactory | ||
|
||
|
||
def log_message(logging_type: LoggingType) -> Log: | ||
str_longing_type = logging_type.value | ||
return Log( | ||
title=str_longing_type + " :: test_message_bus_log", | ||
messages=[str_longing_type + " :: log_message :: 1", str_longing_type + " :: log_message :: 2"] | ||
) | ||
|
||
|
||
def test_message_bus_log(caplog): | ||
logging_type = LoggingType.PY | ||
log = log_message(logging_type) | ||
message_bus.set_domain_logger(LoggerFactory.get(logging_type, name="py-domain")) | ||
message_bus.handle(log) | ||
if log.title: | ||
assert log.title in caplog.text | ||
if log.messages: | ||
for message in log.messages: | ||
assert message in caplog.text | ||
|
||
logging_type = LoggingType.ELK | ||
log = log_message(logging_type) | ||
message_bus.set_domain_logger(LoggerFactory.get(logging_type, name="elk-domain")) | ||
message_bus.handle(log) | ||
if log.title: | ||
assert log.title in caplog.text | ||
if log.messages: | ||
for message in log.messages: | ||
assert message in caplog.text |