views:

640

answers:

3

I'm in the process of migrating the logging of a middle-sized application from a custom solution to something more standard. I've decided on using Logback and SLF4J, and I have successfully migrated most of the Java code. However, I have quite a bit of JSPs that simply use System.out for logging. I've never worked much with JSPs, and started to wonder: how am I supposed to use proper logging in a JSP?

<%@page import="org.slf4j.Logger"%>
<%@page import="org.slf4j.LoggerFactory"%>
<%
    Logger log = LoggerFactory.getLogger(getClass());
%>
<!-- ... -->
<%
    log.info("Hello Logging!");
%>

This is what came to mind first, but it seems wrong on several points:

  • way too verbose, and needs a lot of work to convert the existing JSPs
  • a call is made to LoggerFactory.getLogger() every time the page is rendered (as opposed to a static logger field in a standard Java class)
  • I think the name of the logger is also going to be something not really straightforward this way

Is there some kind of standard, or a best practice, or anything for logging in JSPs?

Also, IIRC, there was some kind of taglib for Log4J. Is there something similar for SLF4J (or maybe Logback)?

A: 
public enum MyLoggerFactory {

    INSTANCE;

    @SuppressWarnings("unchecked")
    private final Map<Class, Logger> loggers = new HashMap<Class, Logger>();

    @SuppressWarnings("unchecked")
    public Logger getLogger(Class clazz) {
        if (loggers.get(clazz) == null) {
            loggers.put(clazz, LoggerFactory.getLogger(clazz));
        }
        return loggers.get(clazz);
    }
}

Then your JSP page could look like:

<%
    MyLoggerFactory.INSTANCE.getLogger(getClass()).info("Hello Logging!");
%>
Boris Pavlović
I don't think this is any better. This way I'd have to call getLogger() every time I wanted to log something, there are thread safety issues, and this is even more verbose then the original solution.
Lóránt Pintér
You could employ Aspect Oriented Programming techniques to wrap Java method calls that your tag libraries do and log before and after every execution. Anyway, JSP is not a place for business logic. It should be in JAVA controller and model parts.
Boris Pavlović
+1  A: 

You could try (Note the "!")

<%! org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("JSPname"); %>

Then replace your System.out's with

<% log.info("Hello Logging!"); %>

Boris's comment should really be taken into concideration though, JSPs should not need logging in general. I would only use this technique (or anything similiar) to replace existing logging that needed to be kept.

Michael Rutherfurd
+3  A: 

Have a look at slf4j-taglib.

Ceki