O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

14.5. Using a Pattern with a Logger's Appender

Problem

You want to create your own logger for a servlet and give the logger an appender.

Solution

Include the appender configuration in the log4j.properties file.

Discussion

This recipe creates a new logger, which brings us to the discussion of log4j's inheritance structure. The root logger is the "super logger" that all logger's inherit from, similar to java.lang.Object in Java's object oriented programming setup. Example 14-7 creates a new logger named com.jspservletcookbook, which inherits the root logger's level (DEBUG) and console appender (named cons). Example 14-7 also creates an appender for the com.jspservletcookbook logger. Place this log4j.properties file in the WEB-INF/classes directory.

Example 14-7. The configuration for a logger named com.jspservletcookbook

log4j.rootLogger=DEBUG, conslog4j.logger.com.jspservletcookbook=, myAppender

#the root logger's appender
log4j.appender.cons=org.apache.log4j.ConsoleAppender

#the com.jspservletcookbook logger's appender
log4j.appender.myAppender=org.apache.log4j.RollingFileAppender

log4j.appender.myAppender.File=h:/home/example.log

log4j.appender.myAppender.MaxBackupIndex=1

log4j.appender.myAppender.MaxFileSize=1MB

#the root logger's layout
log4j.appender.cons.layout=org.apache.log4j.SimpleLayout

#the com.jspservletcookbook logger's layout log4j.appender.myAppender.layout=org.apache.log4j.PatternLayout log4j.appender.myAppender.layout.ConversionPattern=%-5p Logger:%c{1} Date: %d{ISO8601} ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required