Skip to content

Commit

Permalink
Correct in asynchronous logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellikrox committed Apr 3, 2019
1 parent 95e3c96 commit 18a5362
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion server/bin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -60,6 +60,7 @@ def main():
sys.exit(1)

args = {
'logger': LOGGER,
'postgres': {
'dbname': options.postgres_dbname,
'hostname': options.postgres_host,
Expand Down
18 changes: 9 additions & 9 deletions server/huntflow_reloaded/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
'the type %s', self._req_type)
2 changes: 1 addition & 1 deletion server/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiologger==0.3.0
alembic==1.0.7
APScheduler==3.5.3
fakeredis==0.16.*
Expand All @@ -6,4 +7,3 @@ psycopg2==2.7.7
redis==3.0.*
testing.postgresql==1.3.0
tornado==5.1.*
aiologger==0.3.0
3 changes: 3 additions & 0 deletions server/test/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 18a5362

Please sign in to comment.