Browse Source

Better handle logz.io responses, and drain to file in case of an unrecoverable error

opensearch
Roi Rav-Hon 9 years ago
parent
commit
74f2f37718
2 changed files with 28 additions and 8 deletions
  1. +28
    -7
      logzio/handler.py
  2. +0
    -1
      setup.py

+ 28
- 7
logzio/handler.py View File

@ -125,11 +125,24 @@ class LogzioHandler(logging.Handler):
return return_json
def backup_logs(self, logs):
timestamp = datetime.datetime.now().strftime("%d%m%Y-%H%M%S")
print("Backing up your logs to logzio-failures-{0}.txt".format(timestamp))
with open("logzio-failures-{0}.txt".format(timestamp), "a") as f:
f.writelines('\n'.join(logs))
def drain_messages(self):
try:
self.buffer_event.clear()
self.drain_lock.acquire()
# Copy buffer
temp_logs = list(self.logs)
self.logs = []
# Release the event
self.buffer_event.set()
# Not configurable from the outside
sleep_between_retries = 2000
number_of_retries = 4
@ -138,18 +151,28 @@ class LogzioHandler(logging.Handler):
headers = {"Content-type": "text/plain"}
for current_try in range(number_of_retries):
response = requests.post(self.url, headers=headers, data='\n'.join(self.logs))
response = requests.post(self.url, headers=headers, data='\n'.join(temp_logs))
if response.status_code != 200: # 429 400, on 400 print stdout
if response.status_code == 400:
print("Got unexpected 400 code from logz.io, response: {0}".format(response.text))
self.backup_logs(temp_logs)
if response.status_code == 401:
print("You are not authorized with logz.io! dropping..")
break
if response.status_code != 200:
sleep(sleep_between_retries)
sleep_between_retries *= 2
else:
success_in_send = True
break
if success_in_send: # Only clear the logs from the buffer if we managed to send them
# Clear the buffer
self.logs = []
if not success_in_send:
# Write to file
self.backup_logs(temp_logs)
finally:
self.buffer_event.set()
@ -157,5 +180,3 @@ class LogzioHandler(logging.Handler):
def emit(self, record):
self.add_to_buffer(self.format_message(record))

+ 0
- 1
setup.py View File

@ -16,7 +16,6 @@ setup(
"requests"
],
include_package_data=True,
platform='any',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',


Loading…
Cancel
Save