Creating a New Logfile


Having application logging handled by the server is very convenient, but mixing application and server logging can be confusing. You will probably want to create a separate logfile for each of your applications. This is quite easy to do in JBoss.

How do I do that?

Let's see how we can create a new logfile for the ToDo application. Appenders represent logging destinations, so creating a new logfile is as simple as creating a new appender. To make it easy, we'll use the FILE appender, which represents the server.log file, as a starting point:

     <appender name="TODO"               >         <errorHandler />         <param name="File"        value="${jboss.server.home.dir}/log/todo.log"/>         <param name="Append"      value="false"/>         <param name="DatePattern" value="'.'yyyy-MM-dd"/>         <layout >             <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>         </layout>     </appender> 

We've changed two things here: the logical name of the appender and the location of the logfile. The appender name is important because we need a way to refer to the appender later in the file, specifically to tell log4j which categories should be logged to this file. You may recall that the root category had two appender-ref elements on it that we didn't talk about. The appender-ref elements tell the category which appenders to send the generated log messages to:

     <root>         <appender-ref ref="CONSOLE"/>         <appender-ref ref="FILE"/>     </root> 

This tells log4j to send all messages to both the CONSOLE and FILE appenders. Nothing will be logged to our new todo.log file until we tell a category to send its messages there. To do that, add an appender-ref element to the category for the ToDo application:

     <category name="com.oreilly.jbossnotebook.todo">         <priority value="DEBUG" />         <appender-ref ref="TODO"/>     </category> 

At this point, you'll see a todo.log file created in the log directory. If you create a few tasks, the server.log file will start filling up with messages such as the following:

     2005-04-12 09:50:50,366 DEBUG [com.oreilly.jbossnotebook.todo.ejb.TaskBean]     creating task 36e4227e7f00000100197971e1e97a97 for user pinky     2005-04-12 09:50:53,597 DEBUG [com.oreilly.jbossnotebook.todo.ejb.TaskBean]     creating task 36e42f1d7f000001005b6f0acf4c151c for user pinky 

Creating a new logfile for the ToDo application doesn't stop the messages from being logged to the console or to server.log file if the threshold of the logfiles permits it.

What just happened?

You created a new logfile to capture application-specific logging information. Creating a new logfile is the easiest way to get the log messages you want in a place that is easy to monitor. You have complete control over the layout of the new file and can make sure that you get the information you want without affecting the primary logs.

You don't have to log messages just to files. The log4j.xml file contains examples of appenders that log messages to email, syslog servers, and even JMS queues. If you have priority log messages, you can even write an appender that sends the log messages out as SMS messages to your cell phone. With log4j, you have the flexibility to change the logging policy without having to change any of your code.



JBoss. A Developer's Notebook
JBoss: A Developers Notebook
ISBN: 0596100078
EAN: 2147483647
Year: 2003
Pages: 106

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net