# coding=utf-8
"""
[Logentries: Log Management & Analytics Made Easy ](https://logentries.com/).
Send Diamond stats to your Logentries Account where you can monitor and alert
based on data in real time.

#### Dependencies


#### Configuration

Enable this handler

 * handers = diamond.handler.logentries.LogentriesDiamondHandler
 * log_token = [Your Log Token](https://logentries.com/doc/input-token/)


"""

from Handler import Handler
import logging
import time
import urllib2
import json


class LogentriesDiamondHandler(Handler):
    """
      Implements the abstract Handler class
    """
    def __init__(self, config=None):
        """
        New instance of LogentriesDiamondHandler class
        """

        Handler.__init__(self, config)
        self.log_token = self.config.get('log_token', None)
        if self.log_token is None:
            raise Exception

    def get_default_config_help(self):
        """
        Help text
        """
        config = super(LogentriesDiamondHandler,
                       self).get_default_config_help()

        config.update({
            'log_token': None,
        })

        return config

    def get_default_config(self):
        """
        Return default config for the handler
        """
        config = super(LogentriesDiamondHandler, self).get_default_config()

        config.update({
            'log_token': None,
        })

        return config

    def process(self, metric):
        """
        Process metric by sending it to datadog api
        """

        time.sleep(1)
        self._send(metric)

    def _send(self, metric):
        """
        Convert message to a json object and send to Lognetries
        """
        logging.debug("Sending logs.")
        topic, value, timestamp = str(metric).split()
        msg = json.dumps({"event": {topic: value}})
        req = urllib2.Request("https://js.logentries.com/v1/logs/"
                              + self.log_token, msg)
        urllib2.urlopen(req)
        time.sleep(1)

@@ -323,16 +323,17 @@
 Handler%0A
+%0A
  * log_t
@@ -398,16 +398,47 @@
 oken/)%0A%0A
+ * queue_size = Integer value%0A%0A
 %0A%22%22%22%0A%0Afr
@@ -482,20 +482,8 @@
 ing%0A
-import time%0A
 impo
@@ -505,16 +505,46 @@
 rt json%0A
+from collections import deque%0A
 %0A%0Aclass 
@@ -854,16 +854,104 @@
 , None)%0A
+        self.queue_size = int(self.config%5B'queue_size'%5D)%0A        self.queue = deque(%5B%5D)%0A
         
@@ -1233,37 +1233,64 @@
    'log_token': 
-None,
+'',%0A            'queue_size': ''
 %0A        %7D)%0A%0A   
@@ -1543,13 +1543,40 @@
 n': 
-None,
+'',%0A            'queue_size': ''
 %0A   
@@ -1727,47 +1727,175 @@
     
-time.sleep(1)%0A        self._send(metric
+self.queue.append(metric)%0A        if len(self.queue) %3E= self.queue_size:%0A            logging.debug(%22Queue is full, sending logs to Logentries%22)%0A            self._send(
 )%0A%0A 
@@ -1907,32 +1907,24 @@
 f _send(self
-, metric
 ):%0A        %22
@@ -2014,39 +2014,81 @@
     
-logging.debug(%22Sending logs.%22)%0A
+while len(self.queue) %3E 0:%0A            metric = self.queue.popleft()%0A    
     
@@ -2145,16 +2145,20 @@
         
+  
+  
 msg = js
@@ -2193,16 +2193,20 @@
 alue%7D%7D)%0A
+    
         
@@ -2294,16 +2294,20 @@
         
+    
 + self.l
@@ -2321,16 +2321,41 @@
 n, msg)%0A
+            try:%0A        
         
@@ -2387,18 +2387,114 @@
     
-time.sleep(1
+    except urllib2.URLError as e:%0A                logging.error(%22Can't send log message to Logentries %25s%22, e
 )%0A

use a queue to process metric data, add try except for urlopen