Since I cannot find a logger easy enough for me to use, I made one.
The jar file is here: linky
This logger is thread safe.
Example usage in a servlet:
package com.logtest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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 log4andrew.Logger;
import log4andrew.LogLevel;
/**
* Servlet implementation class Test
*/
@WebServlet("/Test")
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String filePath = "";
private static String fileName = "";
Logger fLogger = null;
final static String FS = File.seperator;
/**
* @see HttpServlet#HttpServlet()
*/
public Test() {
super();
}
@Override
public void init(ServletConfig config){
ServletContext scxt = config.getServletContext();
filePath = scxt.getRealPath("WEB-INF") + FS + "logs" + FS;
fileName = "servletTest";
fLogger = new Logger(filePath, fileName, LogLevel.INFO);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("file location: " + filePath + fileName);
fLogger.critical("some message");
out.println("debug message logged");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
No comments:
Post a Comment