I deployed a JavaEE web app on Glassfish4 server in an EC2 instance.
Everything works fine, but when I tried to add logging capabilities I cannot find the logs.
I created a MyLogger static class just to begin (I still have to learn a lot about logging and I'm afraid this can be the cause of my failure...). This is the code
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class MyLogger {
private final String LOG_FILENAME = "/tmp/app.log";
private static Logger logger = Logger.getLogger("MyLogger");
private static FileHandler fh;
public MyLogger() {
try {
// This block configure the logger with handler and formatter
fh = new FileHandler(LOG_FILENAME);
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void logInfo(String msg) {
logger.info(msg);
}
public static void logWarning(String msg) {
logger.warning(msg);
}
public static void logSevere(String msg) {
logger.severe(msg);
}
}
and this is the class the uses MyLogger
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
/**
* Servlet implementation class RequestsApi
*/
@WebServlet("/api/requests")
public class RequestsApi extends HttpServlet {
private static final long serialVersionUID = 1L;
// set the ArrayList where to put the offers that can satisfy
// the request
List<Offer> goodOffers;
Request liftRequest;
// The Bean for the requests
@EJB
RequestEJB requestEjb;
// the Bean for the offers
@EJB
OfferEJB offerEjb;
/**
* @see HttpServlet#HttpServlet()
*/
public RequestsApi() {
super();
// TODO Auto-generated constructor stub
goodOffers = new ArrayList<Offer>();
liftRequest = new Request();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Request> listRequest = new ArrayList<Request>();
String origin = request.getParameter("origin");
String destination = request.getParameter("destination");
if (origin != null) {
Request liftRequest = new Request();
liftRequest.setOrigin(origin);
liftRequest.setDestination(destination);
requestEjb.createRequest(liftRequest);
}
MyLogger.logSevere(origin + ", "+ destination);
// get all the athletes in the DB
listRequest = requestEjb.findRequests();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
List<Offer> offers = offerEjb.findOffers();
for (Offer offer : offers)
if (isGood(offer))
goodOffers.add(offer);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
// create the JSON file
JSONArray array = new JSONArray(listRequest);
response.getWriter().append(array.toString());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
final String origin = request.getParameter("origin");
final String destination = request.getParameter("destination");
final String username = request.getParameter("username");
liftRequest.setOrigin(origin);
liftRequest.setDestination(destination);
liftRequest.setRequesterUsername(username);
requestEjb.createRequest(liftRequest);
MyLogger.logInfo(username + ": " + origin + ", "+ destination);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
List<Offer> offers = offerEjb.findOffers();
for (Offer offer : offers)
if (isGood(offer))
goodOffers.add(offer);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
thread.start();
}
public boolean isGood(Offer offer) throws Exception {
return true;
}
}
The servlet works fine in terms of answer to the web browser but I don't know how to get my log files.
I thought that I should have found my log file in the /tmp directory of EC2 instance, but this is not the case.
Is there anything wrong with my logger implementation or do I need to pay attention in managing files in EC2 with a particular logging configuration?
New info
I tried the same approach with a standalone Java application on my PC and it works.
It actually seems an issue in managing files on EC2 instances
New info on EC2 logging is working properly with a standalone Java application. So the problem is not with EC2, but with how logging is managed in a JavaEE web application
New info Putting the content of MyLogger into RequestsApi everything works fine.