|
@ -125,11 +125,24 @@ class LogzioHandler(logging.Handler): |
|
|
|
|
|
|
|
|
return return_json |
|
|
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): |
|
|
def drain_messages(self): |
|
|
try: |
|
|
try: |
|
|
self.buffer_event.clear() |
|
|
self.buffer_event.clear() |
|
|
self.drain_lock.acquire() |
|
|
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 |
|
|
# Not configurable from the outside |
|
|
sleep_between_retries = 2000 |
|
|
sleep_between_retries = 2000 |
|
|
number_of_retries = 4 |
|
|
number_of_retries = 4 |
|
@ -138,18 +151,28 @@ class LogzioHandler(logging.Handler): |
|
|
headers = {"Content-type": "text/plain"} |
|
|
headers = {"Content-type": "text/plain"} |
|
|
|
|
|
|
|
|
for current_try in range(number_of_retries): |
|
|
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(sleep_between_retries) |
|
|
sleep_between_retries *= 2 |
|
|
sleep_between_retries *= 2 |
|
|
else: |
|
|
else: |
|
|
success_in_send = True |
|
|
success_in_send = True |
|
|
break |
|
|
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: |
|
|
finally: |
|
|
self.buffer_event.set() |
|
|
self.buffer_event.set() |
|
@ -157,5 +180,3 @@ class LogzioHandler(logging.Handler): |
|
|
|
|
|
|
|
|
def emit(self, record): |
|
|
def emit(self, record): |
|
|
self.add_to_buffer(self.format_message(record)) |
|
|
self.add_to_buffer(self.format_message(record)) |
|
|
|
|
|
|
|
|
|
|
|
|