diff --git a/.travis.yml b/.travis.yml index 11e0ea0..e96a536 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,8 @@ python: install: - "pip install -r requirements.txt --use-mirrors" - pip install coveralls --use-mirrors + - pip install pytz + - pip install tzlocal # command to run tests script: - "nosetests --with-coverage --cover-erase --cover-branches --cover-package=loggly" diff --git a/loggly/handlers.py b/loggly/handlers.py index d205d0a..166d5a6 100644 --- a/loggly/handlers.py +++ b/loggly/handlers.py @@ -1,18 +1,44 @@ import logging import logging.handlers - import socket import traceback - +import json +import pytz +from datetime import datetime +from tzlocal import get_localzone from requests_futures.sessions import FuturesSession session = FuturesSession() - def bg_cb(sess, resp): """ Don't do anything with the response """ pass +# add utc in timestamp +def get_utc_date(local_date): + datefrmt = datetime.strptime(str(local_date), '%Y-%m-%d %H:%M:%S,%f') + tz = get_localzone() + local_dt = tz.localize(datefrmt, is_dst=None) + utc_dt = local_dt.astimezone(pytz.utc) + return utc_dt.isoformat() + +# chack data is valid json or not +def is_json(data): + try: + json_object = json.loads(data) + except ValueError, e: + return False + return True + +# convert log to Json Data if log have key value pair +def format_timestamp(log): + jsondata = json.loads(log) + if 'timestamp' in jsondata and jsondata['timestamp'] is not None: + jsondata['timestamp'] = get_utc_date(jsondata['timestamp']) + else: + # if time stamp not present added timestamp with current utc time + jsondata['timestamp'] = datetime.utcnow().isoformat() + return jsondata class HTTPSHandler(logging.Handler): def __init__(self, url, fqdn=False, localname=None, facility=None): @@ -31,8 +57,11 @@ def get_full_message(self, record): def emit(self, record): try: payload = self.format(record) + if is_json(payload): + payload = format_timestamp(payload) session.post(self.url, data=payload, background_callback=bg_cb) except (KeyboardInterrupt, SystemExit): raise except: - self.handleError(record) \ No newline at end of file + self.handleError(record) + diff --git a/loggly/tests/test_handlers.py b/loggly/tests/test_handlers.py index 5872082..2e9900f 100644 --- a/loggly/tests/test_handlers.py +++ b/loggly/tests/test_handlers.py @@ -70,7 +70,7 @@ def test_emit(self): handler.format.return_value = 'msg' handler.emit(self.record) - + handler.format.assert_called_once_with(self.record) self.session.post.assert_called_once_with( @@ -80,7 +80,7 @@ def test_emit_interrupt(self): """ it should raise the interrupt """ handler = self.handler handler.format = Mock() - + self.session.post.side_effect = KeyboardInterrupt('Boom!') self.assertRaises(KeyboardInterrupt, handler.emit, self.record) @@ -89,7 +89,8 @@ def test_emit_exit(self): """ it should raise the exit """ handler = self.handler handler.format = Mock() - + self.payload = "hello" + self.session.post.side_effect = SystemExit('Boom!') self.assertRaises(SystemExit, handler.emit, self.record) diff --git a/setup.py b/setup.py index e71b52d..dca4208 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,7 @@ packages=find_packages(), install_requires=[ "requests-futures >= 0.9.4", + "pytz >= 2014.7" ], include_package_data=True, platform='any',