login about faq

Hi,

I'm currently maintaining a system which is written in JSP and using Java. It has no framework, therefore, some of the business logics are in JSP which is a presentation layer.

I'm planning to use Log4J for our logging, but unfortunately, the Logger class needs to have a class as one of the parameters. The common code for this is

Logger log = Logger.getLogger(YourClass.class);

Example, I have a logger util class LoggerTest:

package somepackage;

public class LoggerTest {
    public void writeLog(Object callingClass) {
        Logger log = Logger.getLogger(callingClass.getClass());
        ...
        ...
    }
}

And then I used this inside JSP

<%@import = "sompackage.LoggerTest"%>

<%
LoggerTest log = new LoggerTest();
log.writeLog(LoggerTest.class);
%>

As you would notice, the log.writeLog(LoggerTest.class) should pass a parameter which is of type class / object.

The problem is, when this is used in JSP, I am having a nullpointer exception, because obviously, JSP is not a class. I'm not sure now if we can use Log4J with JSP. Tablibs on the other hand has already resigned and I think has been deprecated since 2002.

Aside from the System.out.println() approach, what would you now recommend in logging errors/warnings in JSP?

Any questions or comments will be appreciated. Thanks! :)

This question is marked "community wiki".

asked Jul 12 '11 at 13:49

Cris's gravatar image

Cris
1718

edited Jul 12 '11 at 14:05


I therefore conclude that this is still possible. I missed out the right syntax. Instead of the above code in JSP, it should be:

<%@import = "sompackage.LoggerTest"%>

<%! LoggerTest log = new LoggerTest(); %>
<% log.writeLog(LoggerTest.class); %>

The <%! %> must be used since it is a declaration and assignment.

link

answered Jul 13 '11 at 10:20

Cris's gravatar image

Cris
1718

I haven't tried it yet but there is a library called Log for JSP (through JSTL) based on Log4J. You may find it here.

link

answered Jul 12 '11 at 14:28

codix's gravatar image

codix
2395

Why not just use Logger.getLogger(String) instead of going through hoops?

Logger logger = Logger.getLogger("logger.name");
logger.debug("This is a debug message.");
link

answered Jul 13 '11 at 21:18

Alistair%20A.%20Israel's gravatar image

Alistair A. Israel
3.1k210

i think it is not advisable to log messages while on the jsp pages because jsp is just used to display html pages to the user.

It is better for you to log messages on the controller.

try to use SimpleMappingExceptionResolver. it is very helpful for me in logging and displaying error messages in a JSP page.

link

answered Sep 13 '11 at 18:15

maryon's gravatar image

maryon
15010

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×63
×3

Asked: Jul 12 '11 at 13:49

Seen: 1,060 times

Last updated: Sep 13 '11 at 18:15