Last active
August 29, 2015 13:57
-
-
Save anlcan/9348751 to your computer and use it in GitHub Desktop.
Logentries on Google App Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.*; | |
import java.net.Socket; | |
import java.net.SocketAddress; | |
import java.util.concurrent.locks.Condition; | |
import java.util.concurrent.locks.Lock; | |
import java.util.concurrent.locks.ReentrantLock; | |
/** | |
* Add this servlet to your in web.xml, and send POST request with log parameter | |
* | |
* User: anlcan | |
* Date: 3/4/14 | |
* Time: 2:03 PM | |
*/ | |
public class LogentriesHandler extends AppEngineTaskHandler { | |
public static Socket socket = null; | |
public static Lock lock = new ReentrantLock(); | |
public static Condition cond = lock.newCondition(); | |
public static String LOG_TOKEN; | |
public void connect() { | |
if (socket != null && socket.isConnected()) return; | |
try{ | |
if ( lock.tryLock()){ | |
socket = new Socket("data.logentries.com", 10000); | |
cond.signalAll(); | |
lock.unlock(); | |
}else { | |
cond.await(); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
String log = req.getParam("log"); | |
try { | |
connect(); | |
Writer out = new OutputStreamWriter(socket.getOutputStream(), "UTF-8"); | |
socket.setSoTimeout(10000); | |
out.write(LOG_TOKEN); | |
out.write(" "); | |
out.write(log); | |
out.write("\r\n"); | |
out.flush(); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
System.err.println(e.toString()); | |
} finally { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment