flybywire
flybywire

Reputation: 273452

what is the right way to use log4j within jsp pages

I mean, I want the logger name to reflect the source.jsp file, no matter if it is included in another file or compiled to a class or whatever.

Upvotes: 16

Views: 55311

Answers (7)

Scott Chu
Scott Chu

Reputation: 992

Though an old question, I'd like to provide some options. Say jsp filename = for_example.jsp:

1. Use file name directly but replace dot with underscore

Logger log = Logger.getLogger("for_example_jsp");

(note: constantly mistake is to use 'for_example.jsp' directly, it will be treated as class name and then when AP server, say Tomcat, finds no such class path, it will output log message in catalina.out as "...(jsp) ....".

2. Use request URI

Logger log = Logger.getLogger(request.getRequestURI());

Somebody likes this. I don't know why but I've seen this before in somebody's codes.

3. Use Class name

Logger log = Logger.getLogger(this.getClass());

this will usually get 'for_example_jsp" except when for_example.jsp is included in some other servlet, say 'test_servlet', it will be 'including file's log name' + 'an ordered number', e.g. test_servlet_include_005.

4. Programatically get jsp file name

String __jspName = this.getClass().getSimpleName(); // Get jsp program name
Logger log = Logger.getLogger(__jspName);
__jspName = __jspName.replaceAll("_","."); // get back true jsp file name

However, this is not necessarily right with respect to what AP server and version you are using.

I personally use method 1 as I think it's most reliable.

Upvotes: 1

Pavan Dave
Pavan Dave

Reputation: 241

Firstly, import the required package i.e.

<%@page import="org.apache.log4j.Logger"%>

then,

 <%! static Logger logger = Logger.getLogger(jsppagename_jsp.class); %>

the jsppagename_jsp may change, according to the server which you are using. And then, use anywhere inside jsp like:

<% logger.info("This is test."); %>

The IDE may show an error message at the declaration of logger object. But, don't worry, the server like tomcat will automatically create the corresponding servlet class of each jsp page inside tomcat directly itself.

Upvotes: 24

k3b
k3b

Reputation: 14755

Since it is likely that you want to output the content of jsp-el-variables in log4net you can use this code

<%@page import="org.apache.log4j.Logger"%>

<%-- output of the jsp-el variables "orderID" and "orderDate" in log4j --%>
<c:set var="Parameter4Log4J" value="orderID=${orderID} orderDate=${orderDate}" />
<% Logger.getLogger("jsp.order.orderconfirmation").info(
               pageContext.getAttribute("Parameter4Log4J")); %> 

Upvotes: 1

Ryan
Ryan

Reputation: 2886

The following is the code. All the configuration file placement and configuration are the same as how it is use in Servlet or other class.

<%@ page import="org.apache.log4j.Logger" %>
<html>
    <head>
        <title>Demonstration log4j usage in jsp</title>
    </head>
    <body>
        <% Logger log = Logger.getLogger("com.mobilefish.demo.test");
           log.debug("Show DEBUG message");
           log.info("Show INFO message");
           log.warn("Show WARN message");
           log.error("Show ERROR message");
           log.fatal("Show FATAL message"); %>
        <b>The log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.</b>
    </body>
</html>

Upvotes: 4

NT_
NT_

Reputation: 2670

Use the appropriate ConversionPattern when configuring log4j, e.g:

%d [%C] %-5p %c - %m%n

Here, the %C outputs the fully qualified class name when you call any of the Logger class methods.

Upvotes: 1

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

What's wrong with:

Logger logger = Logger.getLogger( "source.jsp" );

You can prepend it with a better non-ambiguous prefix, of course. Actually, something along the lines JSPS.source.jsp is better, as you can set up logging rules for JSPS logger, that would later be applied to all sub-loggers.

Having said this, why do you need to log from JSP directly?

Upvotes: 9

skaffman
skaffman

Reputation: 403441

You could write a factory method which takes the current request as a parameter, and which obtains a Logger based on the JSP name, something like this:

public static Logger getLogger(HttpServletRequest request) {
    String requestUri = request.getRequestURI();
    String jspName = requestUri.substring(requestUri.lastIndexOf('/'));
    return Logger.getLogger(jspName);
}

You might have to play with it a bit to make it work (I haven't tested the above code), but that's the gist of it.

This could be used directly from the JSP, or from a bean or tag class which is used by the JSP, as long as it has access to the request object.

Upvotes: 5

Related Questions