diff --git a/server/bin/server.py b/server/bin/server.py index 7e71a37..be47f20 100755 --- a/server/bin/server.py +++ b/server/bin/server.py @@ -25,7 +25,7 @@ import huntflow_reloaded.scheduler from huntflow_reloaded import handler -LOGGER = logging.getLogger('tornado.application') +LOGGER = Logger.with_default_handlers() define('channel-name', @@ -60,6 +60,7 @@ def main(): sys.exit(1) args = { + 'logger': LOGGER, 'postgres': { 'dbname': options.postgres_dbname, 'hostname': options.postgres_host, diff --git a/server/huntflow_reloaded/handler.py b/server/huntflow_reloaded/handler.py index ea7babd..88f5a2e 100644 --- a/server/huntflow_reloaded/handler.py +++ b/server/huntflow_reloaded/handler.py @@ -20,9 +20,9 @@ import re from datetime import datetime +from aiologger import Logger from tornado.escape import json_decode from tornado.web import RequestHandler -from aiologger import Logger import huntflow_reloaded.scheduler from huntflow_reloaded import models @@ -63,8 +63,7 @@ def __init__(self, application, request, **kwargs): **kwargs) self._channel_name = self._channel_name or None self._decoded_body = {} - self._handlers = {} - self._logger = Logger.with_default_handlers() #('tornado.application') + self._handlers = {} self._redis_conn = self._redis_conn or None self._req_type = None @@ -74,10 +73,11 @@ def __init__(self, application, request, **kwargs): val = self._get_attr_or_stub('{}_handler'.format(i.lower())) self._handlers[key] = val - def initialize(self, redis_conn, channel_name, postgres): # pylint: disable=arguments-differ + def initialize(self, redis_conn, channel_name, postgres, logger): # pylint: disable=arguments-differ self._channel_name = channel_name self._redis_conn = redis_conn self._postgres_data = postgres + self._logger = logger def _classify_request(self): try: @@ -125,22 +125,22 @@ async def post(self): # pylint: disable=arguments-differ try: self._classify_request() except UndefinedType: - await self._logger.debug(body) + await self._logger.debug(json.dumps(body)) self.write('Undefined type') self.set_status(500) return except UnknownType: - await self._logger.debug(body) + await self._logger.debug(json.dumps(body)) self.write('Unknown type') self.set_status(500) return - await self._logger.debug(self._decoded_body) + await self._logger.debug(json.dumps(self._decoded_body)) try: await self._handlers[self._req_type]() except IncompleteRequest: - await self._logger.debug(body) + await self._logger.debug(json.dumps(body)) self.write('Incomplete request') self.set_status(500) return @@ -229,4 +229,4 @@ async def stub_handler(self): which is responsible for dealing with the requests of the type. """ await self._logger.info('Invoking the stub handler to serve the request of ' - 'the type %s', self._req_type) \ No newline at end of file + 'the type %s', self._req_type) diff --git a/server/requirements.txt b/server/requirements.txt index fc6c089..b73975e 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -1,3 +1,4 @@ +aiologger==0.3.0 alembic==1.0.7 APScheduler==3.5.3 fakeredis==0.16.* @@ -6,4 +7,3 @@ psycopg2==2.7.7 redis==3.0.* testing.postgresql==1.3.0 tornado==5.1.* -aiologger==0.3.0 \ No newline at end of file diff --git a/server/test/runtests.py b/server/test/runtests.py index 8e651c4..9db0982 100755 --- a/server/test/runtests.py +++ b/server/test/runtests.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +import logging + import fakeredis import testing.postgresql from tornado.testing import AsyncHTTPTestCase @@ -65,6 +67,7 @@ class HuntflowWebhookHandlerTest(WebTestCase): def get_handlers(self): conn = fakeredis.FakeStrictRedis() args = { + 'logger': logging.getLogger('tornado.application') 'postgres': { 'dbname': 'test', 'hostname': 'localhost',