Web site construction - a file and roll over or back up
a file and roll over or back up the log file while the application is still running. You can also specify the rollover frequency and the date pattern that will be used for the backup names. Having this functionality available out of the box makes log4j invaluable to any application that needs to roll over log files at user-defined intervals. 15.5 Using Commons Logging in JSP Pages We’ve covered how to use the Commons Logging API within Java components, but we haven’t yet discussed how to use them in JSP pages. There are a number of ways to use the library within JSP pages, but we’ll just cover the two easiest approaches here. The first approach is to use the same three steps defined earlier, this time performing them in the JSP page itself: 1. Import the Commons Logand LogFactory classes. 2. Define and initialize a logger variable for the page. 3. Start logging. Example 15-8 illustrates this approach in a basic JSP page. Example 15-8. Using Commons Logging in a JSP page <%@ page import="org.apache.commons.logging.Log" %> <%@ page import="org.apache.commons.logging.LogFactory" %> <%-- Get a reference to the logger for this class --%> <% Log logger = LogFactory.getLog( this.getClass( ) ); %> <% logger.debug( "This is a debug message from a jsp" ); %>
<% logger.info( "This is another log message in the jsp" ); %> There should be two log messages in the log file. You must have the Commons Logging environment configured properly for this to work, just as when using it in the Java classes. Any JSP page that is part of the web application will be able to use the logging utilities. Because most containers use a different class loader for each web application, any JSP page that is not part of the log4j-configured web application may not be able to use the logging utilities. Although Example 15-8 shows just how easy it can be to use the Commons Logging API in your JSP pages, there are a few issues with this approach. The most obvious one is that your JSP pages willFrom our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.