Skip to content

Commit

Permalink
Merge 3e5d08b into 1392d3c
Browse files Browse the repository at this point in the history
  • Loading branch information
dmvieira authored Apr 20, 2020
2 parents 1392d3c + 3e5d08b commit 8330d49
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 19 deletions.
22 changes: 19 additions & 3 deletions barterdude/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import logging

import os
from pythonjsonlogger import jsonlogger

BARTERDUDE_DEFAULT_LOG_NAME = os.environ.get(
"BARTERDUDE_DEFAULT_LOG_NAME", "barterdude"
)
BARTERDUDE_DEFAULT_LOG_LEVEL = int(
os.environ.get(
"BARTERDUDE_DEFAULT_LOG_LEVEL", logging.INFO
)
)

handler = logging.StreamHandler()
handler.setFormatter(jsonlogger.JsonFormatter(
'(levelname) (name) (pathname) (lineno)',
timestamp=True
))
logger = logging.getLogger('barterdude')
logger.addHandler(handler)


def getLogger(
name=BARTERDUDE_DEFAULT_LOG_NAME,
level=BARTERDUDE_DEFAULT_LOG_LEVEL
):
logger = logging.getLogger(name)
logger.setLevel(level)
logger.addHandler(handler)
return logger
24 changes: 18 additions & 6 deletions barterdude/hooks/logging.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import json
from traceback import format_tb

from asyncworker.rabbitmq.message import RabbitMQMessage

from barterdude.conf import logger
from barterdude.conf import (
getLogger, BARTERDUDE_DEFAULT_LOG_NAME, BARTERDUDE_DEFAULT_LOG_LEVEL
)
from barterdude.hooks import BaseHook


class Logging(BaseHook):

def __init__(
self, name=BARTERDUDE_DEFAULT_LOG_NAME,
level=BARTERDUDE_DEFAULT_LOG_LEVEL
):
self._logger = getLogger(name, level)

@property
def logger(self):
return self._logger

async def before_consume(self, message: RabbitMQMessage):
logger.info({
self.logger.info({
"message": "Before consume message",
"delivery_tag": message._delivery_tag,
"message_body": json.dumps(message.body),
})

async def on_success(self, message: RabbitMQMessage):
logger.info({
self.logger.info({
"message": "Successfully consumed message",
"delivery_tag": message._delivery_tag,
"message_body": json.dumps(message.body),
})

async def on_fail(self, message: RabbitMQMessage, error: Exception):
logger.error({
self.logger.error({
"message": "Failed to consume message",
"delivery_tag": message._delivery_tag,
"message_body": json.dumps(message.body),
Expand All @@ -32,7 +44,7 @@ async def on_fail(self, message: RabbitMQMessage, error: Exception):
})

async def on_connection_fail(self, error: Exception, retries: int):
logger.error({
self.logger.error({
"message": "Failed to connect to the broker",
"retries": retries,
"exception": repr(error),
Expand Down
5 changes: 3 additions & 2 deletions barterdude/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from asyncio import gather
from asyncworker.rabbitmq.message import RabbitMQMessage

from barterdude.conf import logger
from barterdude.conf import getLogger


class Monitor:
def __init__(self, *hooks: Iterable):
self.__hooks = hooks
self._logger = getLogger()

async def _callback(self,
method: Callable[[RabbitMQMessage], Optional[Any]],
Expand All @@ -18,7 +19,7 @@ async def _callback(self,
try:
return await (method(message, error) if error else method(message))
except Exception as e:
logger.error({
self._logger.error({
"message": f"Error on hook method {method}",
"exception": repr(e),
"traceback": format_tb(e.__traceback__),
Expand Down
62 changes: 62 additions & 0 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import logging
from pythonjsonlogger import jsonlogger

from asynctest import TestCase
from barterdude.conf import (
getLogger, BARTERDUDE_DEFAULT_LOG_NAME, BARTERDUDE_DEFAULT_LOG_LEVEL
)


class TestConf(TestCase):

def setUp(self):
self.log_name = BARTERDUDE_DEFAULT_LOG_NAME
self.log_level = BARTERDUDE_DEFAULT_LOG_LEVEL

async def test_should_get_log_with_default_configs(self):
logger = getLogger()
self.assertEqual(
type(logger.handlers[0]),
logging.StreamHandler
)
self.assertEqual(logger.name, self.log_name)
self.assertEqual(logger.level, self.log_level)
self.assertEqual(
type(logger.handlers[0].formatter),
jsonlogger.JsonFormatter
)

async def test_should_get_log_with_custom_configs(self):
logger = getLogger("test_log", logging.DEBUG)
self.assertEqual(
type(logger.handlers[0]),
logging.StreamHandler
)
self.assertEqual(logger.name, "test_log")
self.assertEqual(logger.level, logging.DEBUG)
self.assertEqual(
type(logger.handlers[0].formatter),
jsonlogger.JsonFormatter
)

async def test_should_get_log_with_custom_configs_even_called_after(self):
logger_first = getLogger()
logger = getLogger("test_log", logging.DEBUG)
logger_last = getLogger()
self.assertEqual(
type(logger.handlers[0]),
logging.StreamHandler
)
self.assertEqual(logger_first.name, self.log_name)
self.assertEqual(logger_first.level, self.log_level)
self.assertEqual(logger.name, "test_log")
self.assertEqual(logger.level, logging.DEBUG)
self.assertEqual(logger_last.name, self.log_name)
self.assertEqual(logger_last.level, self.log_level)
self.assertEqual(
type(logger.handlers[0].formatter),
jsonlogger.JsonFormatter
)
self.assertEqual(len(logger_first.handlers), 1)
self.assertEqual(len(logger.handlers), 1)
self.assertEqual(len(logger_last.handlers), 1)
23 changes: 19 additions & 4 deletions tests/test_hooks/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from asynctest import TestCase, Mock, patch
from barterdude.hooks.logging import Logging

Expand All @@ -9,7 +10,21 @@ def setUp(self):
self.message = Mock()
self.logging = Logging()

@patch("barterdude.hooks.logging.logger")
async def test_should_access_logger(self):
log = Logging("my_log", logging.DEBUG)
logger = log.logger
self.assertEqual(
type(logger),
logging.Logger
)
self.assertEqual(
type(logger.handlers[0]),
logging.StreamHandler
)
self.assertEqual(logger.name, "my_log")
self.assertEqual(logger.level, logging.DEBUG)

@patch("barterdude.hooks.logging.Logging.logger")
@patch("barterdude.hooks.logging.json.dumps")
async def test_should_log_before_consume(self, dumps, logger):
await self.logging.before_consume(self.message)
Expand All @@ -20,7 +35,7 @@ async def test_should_log_before_consume(self, dumps, logger):
"message_body": dumps.return_value,
})

@patch("barterdude.hooks.logging.logger")
@patch("barterdude.hooks.logging.Logging.logger")
@patch("barterdude.hooks.logging.json.dumps")
async def test_should_log_on_success(self, dumps, logger):
await self.logging.on_success(self.message)
Expand All @@ -31,7 +46,7 @@ async def test_should_log_on_success(self, dumps, logger):
"message_body": dumps.return_value,
})

@patch("barterdude.hooks.logging.logger")
@patch("barterdude.hooks.logging.Logging.logger")
@patch("barterdude.hooks.logging.json.dumps")
@patch("barterdude.hooks.logging.repr")
@patch("barterdude.hooks.logging.format_tb")
Expand All @@ -49,7 +64,7 @@ async def test_should_log_on_fail(self, format_tb, repr, dumps, logger):
"traceback": format_tb.return_value,
})

@patch("barterdude.hooks.logging.logger")
@patch("barterdude.hooks.logging.Logging.logger")
@patch("barterdude.hooks.logging.repr")
@patch("barterdude.hooks.logging.format_tb")
async def test_should_log_on_connection_fail(
Expand Down
8 changes: 4 additions & 4 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asynctest import TestCase, Mock, CoroutineMock, patch
from asynctest import TestCase, MagicMock, Mock, CoroutineMock, patch
from barterdude.monitor import Monitor


Expand Down Expand Up @@ -48,15 +48,15 @@ async def test_should_call_hooks_on_connection_fail(self):
self.hook1.on_connection_fail.assert_called_with(exception, retries)
self.hook2.on_connection_fail.assert_called_with(exception, retries)

@patch("barterdude.monitor.logger")
@patch("barterdude.monitor.repr")
@patch("barterdude.monitor.format_tb")
async def test_should_log_if_hook_fail(self, format_tb, repr, logger):
async def test_should_log_if_hook_fail(self, format_tb, repr):
logger = MagicMock()
exception = Exception()

async def before_consume(msg):
raise exception

self.monitor._logger = logger
self.hook1.before_consume = before_consume
self.hook2.before_consume = CoroutineMock()
await self.monitor.dispatch_before_consume({})
Expand Down

0 comments on commit 8330d49

Please sign in to comment.