我在EC2实例的Glassfish4服务器上部署了一个JavaEE Web应用程序 一切正常,但当我尝试添加日志记录功能时,我找不到日志。 我创建了一个MyLogger静态类只是为了开始(我仍然需要学习很多关于日志记录的事情,我担心这可能是我失败的原因......)。这是代码
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);
}
}
这是使用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;
}
}
servlet在回答网络浏览器方面运行良好,但我不知道如何获取我的日志文件。
我认为我应该在EC2实例的/ tmp目录中找到我的日志文件,但事实并非如此。
我的记录器实现有什么问题,或者我是否需要注意使用特定的日志记录配置来管理EC2中的文件?
新信息 我在我的PC上使用独立的Java应用程序尝试了相同的方法并且它可以工作 它实际上似乎是管理EC2实例上的文件的问题
新信息 关于EC2日志记录与独立的Java应用程序一起正常工作。所以问题不在于EC2,而在于如何在JavaEE Web应用程序中管理日志记录
新信息 将MyLogger的内容放入RequestsApi一切正常。
答案 0 :(得分:0)
我发现了问题:它是关于静态方法和构造函数的基本知识:-(
我认为只有在引用类时,即使调用静态方法并且不需要对象实例,也会调用构造函数。
这不是真的,所以没有添加到记录器的处理程序。