You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Samya Ghosh <sa...@mindtree.com> on 2002/09/12 21:08:21 UTC
Context Sensitive Logging in Servlets
Hi,
I have just started using Log4j and was experimenting with it. One of
the things that looked interesting was the ability to log messages/thread,
but the drawback was that the messages logged by different instances of a
class running on different threads, end up being jumbled and the reader has
to comb through the log looking for the thread number in the messages. To
over come this, I stored a stack in a ThreadLocal variable and popped
messages and severity into them rather than logging them. While, exiting the
class, I pop out the messages and log them.
This approach works fine with normal classes - but for a servlet ( which is
logging ) - I still end up getting the logs in a jumbled format. Any clues
how this can be overcome ? Just for information : I don't want to push a
thread identifier and then sort jumbled messages with this identifier from
one log file..and I definitely don't want to append the thread id to the
appender name and create new appenders for each thread.
The class implementation and the calling servlet code is attached :
Thanks
SG
1. Class Code :
package myApp.log;
import java.util.Stack;
import org.apache.log4j.*;
public class XLogger1 extends ThreadLocal{
public Object initialValue() {
return new Stack();
}
public Stack getList() {
return (Stack) super.get();
}
public void clear() {
this.getList().clear();
}
public void debug(String message) {
MessageObject temp = new MessageObject(message, MessageObject.DEBUG
);
this.getList().push(temp);
}
public void error(String message) {
this.getList().push(new MessageObject(message, MessageObject.ERROR
));
}
public void info(String message) {
this.getList().push(new MessageObject(message, MessageObject.INFO ));
}
public void warn(String message) {
this.getList().push(new MessageObject(message, MessageObject.WARN ));
}
public void fatal(String message) {
this.getList().push(new MessageObject(message, MessageObject.FATAL
));
}
public void persist(Logger logger, String name) {
MessageObject temp = null;
System.out.println("1:" + name);
int i=2;
while ( !this.getList().empty() ) {
temp = (MessageObject)this.getList().pop();
switch(temp.getLevel()) {
case MessageObject.DEBUG :
System.out.println(i + " : " + name);
i++;
logger.debug(temp.getMessage());
break;
case MessageObject.ERROR :
logger.error(temp.getMessage());
break;
case MessageObject.WARN :
logger.warn(temp.getMessage());
break;
case MessageObject.INFO :
logger.info(temp.getMessage());
break;
case MessageObject.FATAL :
logger.fatal(temp.getMessage());
break;
}
}
}
}
2. Servlet Code
public class XYZServlet extends HttpServlet {
private static Logger myActualLog = null;
private static myApp.log.XLogger1 myLog = new XLogger1();
public void init() throws ServletException {
if ( myActualLog == null ) {
myActualLog =
LogManager.getLogger("servlet.searchsphonebookservlet");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String firstname = null;
String[] query=new String[6];
String lastname = null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// check which if any fields in the submitted form are empty
if (req.getParameter("FirstName").length() > 0)
firstname = req.getParameter("FirstName");
else firstname = null;
myLog.debug("L2: " + firstname + " ");
query[0] = "1stName";
query[1] = "1stNumber";
query[2] = "2ndName";
myLog.debug("L3: " + firstname + " ");
query[3] = "2ndNumber";
query[4] = firstname;
myLog.debug("L4: " + firstname + " ");
query[5] = lastname;
if (req.getParameter("LastName").length() > 0)
lastname = req.getParameter("LastName");
else lastname = null;
myLog.debug("L5: " + firstname + " ");
req.setAttribute("query", query);
RequestDispatcher rd = null;
rd =
getServletConfig().getServletContext().getRequestDispatcher("/jsp/display_sea
rch_results.jsp");
// Route request to display_search_results.jsp.
myLog.persist(myActualLog, firstname);
if (rd != null) {
rd.forward(req, res);
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: Context Sensitive Logging in Servlets
Posted by Richard Doust <rd...@comcast.net>.
How about using a text processor to filter the log output?
How about writing log entries to a database (as well as to a flat file) and
querying the database where thread id = ? With both of these solutions,
there's no code to write.
-----Original Message-----
From: Samya Ghosh [mailto:samyag@mindtree.com]
Sent: Thursday, September 12, 2002 3:08 PM
To: log4j-user@jakarta.apache.org
Subject: Context Sensitive Logging in Servlets
Hi,
I have just started using Log4j and was experimenting with it. One of
the things that looked interesting was the ability to log messages/thread,
but the drawback was that the messages logged by different instances of a
class running on different threads, end up being jumbled and the reader has
to comb through the log looking for the thread number in the messages. To
over come this, I stored a stack in a ThreadLocal variable and popped
messages and severity into them rather than logging them. While, exiting the
class, I pop out the messages and log them.
This approach works fine with normal classes - but for a servlet ( which is
logging ) - I still end up getting the logs in a jumbled format. Any clues
how this can be overcome ? Just for information : I don't want to push a
thread identifier and then sort jumbled messages with this identifier from
one log file..and I definitely don't want to append the thread id to the
appender name and create new appenders for each thread.
The class implementation and the calling servlet code is attached :
Thanks
SG
1. Class Code :
package myApp.log;
import java.util.Stack;
import org.apache.log4j.*;
public class XLogger1 extends ThreadLocal{
public Object initialValue() {
return new Stack();
}
public Stack getList() {
return (Stack) super.get();
}
public void clear() {
this.getList().clear();
}
public void debug(String message) {
MessageObject temp = new MessageObject(message, MessageObject.DEBUG
);
this.getList().push(temp);
}
public void error(String message) {
this.getList().push(new MessageObject(message, MessageObject.ERROR
));
}
public void info(String message) {
this.getList().push(new MessageObject(message, MessageObject.INFO ));
}
public void warn(String message) {
this.getList().push(new MessageObject(message, MessageObject.WARN ));
}
public void fatal(String message) {
this.getList().push(new MessageObject(message, MessageObject.FATAL
));
}
public void persist(Logger logger, String name) {
MessageObject temp = null;
System.out.println("1:" + name);
int i=2;
while ( !this.getList().empty() ) {
temp = (MessageObject)this.getList().pop();
switch(temp.getLevel()) {
case MessageObject.DEBUG :
System.out.println(i + " : " + name);
i++;
logger.debug(temp.getMessage());
break;
case MessageObject.ERROR :
logger.error(temp.getMessage());
break;
case MessageObject.WARN :
logger.warn(temp.getMessage());
break;
case MessageObject.INFO :
logger.info(temp.getMessage());
break;
case MessageObject.FATAL :
logger.fatal(temp.getMessage());
break;
}
}
}
}
2. Servlet Code
public class XYZServlet extends HttpServlet {
private static Logger myActualLog = null;
private static myApp.log.XLogger1 myLog = new XLogger1();
public void init() throws ServletException {
if ( myActualLog == null ) {
myActualLog =
LogManager.getLogger("servlet.searchsphonebookservlet");
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String firstname = null;
String[] query=new String[6];
String lastname = null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
// check which if any fields in the submitted form are empty
if (req.getParameter("FirstName").length() > 0)
firstname = req.getParameter("FirstName");
else firstname = null;
myLog.debug("L2: " + firstname + " ");
query[0] = "1stName";
query[1] = "1stNumber";
query[2] = "2ndName";
myLog.debug("L3: " + firstname + " ");
query[3] = "2ndNumber";
query[4] = firstname;
myLog.debug("L4: " + firstname + " ");
query[5] = lastname;
if (req.getParameter("LastName").length() > 0)
lastname = req.getParameter("LastName");
else lastname = null;
myLog.debug("L5: " + firstname + " ");
req.setAttribute("query", query);
RequestDispatcher rd = null;
rd =
getServletConfig().getServletContext().getRequestDispatcher("/jsp/display_se
a
rch_results.jsp");
// Route request to display_search_results.jsp.
myLog.persist(myActualLog, firstname);
if (rd != null) {
rd.forward(req, res);
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>