Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 33 additions & 4 deletions loggly/handlers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
self.handleError(record)

7 changes: 4 additions & 3 deletions loggly/tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
packages=find_packages(),
install_requires=[
"requests-futures >= 0.9.4",
"pytz >= 2014.7"
],
include_package_data=True,
platform='any',
Expand Down