Log4j within Web Application...

S

schmen

Currently, I expose my logger through a static method on a utility
class, the logger is statically configured within the utility class.
I originally went this route in that I needed the logger available to
my business classes as well as my servlets/jsps.

Can anyone offer any suggestion that may be better suited for web
deployment?

Thanks for any suggestions!
 
A

Aidan

schmen said:
Currently, I expose my logger through a static method on a utility
class, the logger is statically configured within the utility
class. I originally went this route in that I needed the logger
available to my business classes as well as my servlets/jsps.

Can anyone offer any suggestion that may be better suited for web
deployment?

Thanks for any suggestions!

Hmmm, Here is my sloppy solution for class by class logging. Not
ideal I'm sure but works for me.

I put log4-?.??.??.jar into WEB-INF/lib and log4j.properties into
WEB-INF/classes, and code thusly ...

// Servlet
import org.apache.log4j.*;
public class JobParserServlet extends HttpServlet {
static Logger logger = Logger.getLogger(JobParserServlet.class);
public void init() {
logger.debug("Initialising JobParserServlet");
}
....
}

or

//Base Class
import org.apache.log4j.*;
public abstract class BasicCommand implements Command {
public static Logger logger = Logger.getLogger(Command.class);
public void execute(CommandContext c) throws JobStepException {}
}

//Derived Class
public class CommandSequence extends BasicCommand {
public void execute() throws JobStepException {
logger.debug("CommandSequence Executing");
}
}

and here is my untidy hackable log4j.properties file ...

# Set root logger level to DEBUG and its only appender to LogServer.
log4j.rootCategory=DEBUG, LogServer

#log4j.appender.LogServer=org.apache.log4j.net.SocketAppender
#log4j.appender.LogServer=org.apache.log4j.lf5.LF5Appender
#log4j.appender.LogServer.Port=4445
#log4j.appender.LogServer.RemoteHost=aachen

#Rolling File
log4j.appender.LogServer=org.apache.log4j.RollingFileAppender
log4j.appender.LogServer.File=/var/tmp/webapp.log
log4j.appender.LogServer.MaxFileSize=2048KB
log4j.appender.LogServer.MaxBackupIndex=2

# uses PatternLayout.
log4j.appender.LogServer.layout=org.apache.log4j.PatternLayout
log4j.appender.LogServer.layout.ConversionPattern=%d [%t] %-5p
(%c{1}:%L) %m%n
#log4j.appender.LogServer.layout.ConversionPattern=[%t] %-5p
(%c{1}:%L) %m%n

log4j.logger.com.dautelle=WARN, LogServer
log4j.logger.org.apache.commons=WARN, LogServer
log4j.logger.org.apache.commons.digester.Digester=WARN, LogServer


Any help?

Aidan
 
S

schmen

I am actually servicing an existing web app, and there are a couple
key services that are immutable (i.e.) logging, connection pool,
etc...

So, Log4j is it for this project.

Any ideas or opinions as to the best approach to where code should be
for the log4j initialization and how to expose the loggers to the
various servlets and business classes?
 
C

Chris

I am actually servicing an existing web app, and there are a couple
key services that are immutable (i.e.) logging, connection pool,
etc...

So, Log4j is it for this project.

Any ideas or opinions as to the best approach to where code should be
for the log4j initialization and how to expose the loggers to the
various servlets and business classes?
Only one suggestion: put the log4j jar file in the main /lib directory for
your app server, not in WEB-INF/lib. You'll have classloader headaches if
you use log4j from a servlet and then from another class which was not
created within your webapp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top