Adding log4J to a JSP

I have been asked to add log4J to a JSP so I can test application logging. Im not sure where to begin and what to put in the jsp?

any help would be appreciated.
Angela123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrcoffee365Commented:
You need to:

* get the log4j jar
* configure log4j
* add log4j code to your JSP page

This is the main log4j site:
http://logging.apache.org/

You can download log4j from there.

Then create a simple log4j.properties file, and put it in your WEB-INF/classes directory.  Something like this:

log4j.properties file:

log4j.rootCategory=DEBUG, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{MM/dd/yy HH:mm:ss} [%c{2}]:(%x):%-5p: %m%n

This will append log4j messages to your Tomcat console log for stdout.  It's a good way to start, then after you get it working, you can make the log4j logs go to a separate log file if you wish.

Then make your jsp file use the logger.  This is a sample program:
<%@ page  import="org.apache.log4j.*, org.apache.log4j.PropertyConfigurator" %>
<%
      String loggerName = "TestLogger";
      loggerName = ":" + this.getClass();
      logger = Logger.getLogger(loggerName);
      PropertyConfigurator pc = new PropertyConfigurator();
      String log4jPropsFile = "D://Program Files//Apache Software Foundation//Tomcat 5.5//webapps//WEB-INF//classes//log4j.properties";
      pc.configure(log4jPropsFile);
%>
<html>
<head><title>Test logger</title>
</head>
<body>
<h1>Test log4j logger</h1>
<%  logger.debug("Writing logger.debug"); %>
</body>
<%!
      static Logger logger; %>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim CakalicSenior Developer/ArchitectCommented:
I agree with the above except for the part about manually configuring log4j using PropertyConfigurator. For one thing, the pattern above is going to reconfigure log4j every time you hit the page. I always like to leverage log4j's automatic configuration by putting the configuration file (and I prefer the XML form) somewhere on the classpath so that log4j can find it and configure itself when necessary. If you prefer a more JSP-ish way of logging, have a look at http://jakarta.apache.org/taglibs/doc/log-doc/intro.html.

Regards,
Jim Cakalic
0
mrcoffee365Commented:
If you are going to use taglibs, then this site has a very nice walk-through of how to use log4j tags in a JSP page:
http://www.mobilefish.com/developer/log4j/log4j_quickguide_jsp.html

However, if you don't use taglibs, it's easier to set up a more basic configuration as I documented above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.