Configuring log4j


The log4j.xml file in the conf directory controls all the logging for the server. This file has the definition of all the appenders that specify the logfiles, the categories of messages going to those logfiles, the layouts (formats) of those messages, and any filtering that will be done on those messages.


Note: For further information on log4j, see http://logging.apache.org.

JBoss keeps two logs. The first is the core server.log file. The FILE appender provides the configuration:

     <!-- A time/date based rolling appender -->     <appender name="FILE"               >         <errorHandler />         <param name="File" value="${jboss.server.home.dir}/log/server.log"/>         <param name="Append" value="false"/>         <!-- Rollover at midnight each day -->         <param name="DatePattern" value="'.'yyyy-MM-dd"/>         <layout >             <!-- The default pattern: Date Priority [Category] Message\n -->             <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>         </layout>     </appender> 

All log messages generated in the server are written to the server.log file. If you can't find the log message you are looking for, check this log. If the log message isn't in the server.log file, it isn't being generated. The appender used is DailyRollingFileAppender, which creates a new logfile each day. The log4j.xml file has an alternate configuration for the FILE appender that uses RollingFileAppender. That appender uses a size-based policy, rather than a date-based policy, to rotate logfiles. The alternative configuration is shown here:

     <appender name="FILE"               >         <errorHandler />         <param name="File" value="${jboss.server.home.dir}/log/server.log"/>         <param name="Append" value="false"/>         <param name="MaxFileSize" value="500KB"/>         <param name="MaxBackupIndex" value="1"/>         <layout >             <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>         </layout>     </appender> 

The other log is the console log. When you run JBoss from the command line, as we have been doing, JBoss provides some basic logging information directly to the terminal window. This is actually the console log, and it is defined by the following appender definition:


Note: There are five basic levels of logging: DEBUG, INFO, WARN, ERROR, and FATAL. JBoss also has a special trace level that is used for fine-grained details of the server's operation.
     <appender name="CONSOLE" >         <errorHandler />         <param name="Target" value="System.out"/>         <param name="Threshold" value="INFO"/>         <layout >             <!-- The default pattern: Date Priority [Category] Message\n -->             <param name="ConversionPattern"                    value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>         </layout>     </appender> 

The big difference between the console log and server.log is that the console log has a threshold value of INFO. The console's threshold value limits the log messages to those with a level of INFO or higher. The console will contain INFO, WARN, and ERROR messages, but you won't see DEBUG messages there. You'll have to look in the server.log file to see those messages.


Note: log4j.xml is the only file in the conf directory that JBoss pays attention to after startup.

If you want DEBUG messages to show up on the console, you need to change the Threshold value to DEBUG. TRy changing the value while the server is running. You may have to wait for up to a minute for JBoss to notice the change, as it checks for changes to the log4.xml file every 60 seconds. When JBoss notices the change, it updates the logging configuration in memory and generates the following log message:

     22:48:23,305 INFO  [Log4jService$URLWatchTimerTask] Configuring from     URL: resource:log4j.xml 

Try undeploying or redepolying the ToDo application now. You'll notice that the console log displays much more in-depth information than it did before. If you get tired of the extra messages, set Threshold back to INFO. If you want even fewer messages on the console, try the WARN or ERROR level. WARN or ERROR is an appropriate threshold for a production server.

What just happened?

You found the global log4j.xml file and saw the configuration of the two core logfiles. You also changed the JBoss log level while it was running. JBoss monitors the log4j.xml file every 60 seconds.

Of course, you can customize the time between checks. By now, you should know enough about JBoss to guess that the log4j service is managed through a managed bean (MBean). You can find that MBean in the main jboss-service.xml file:

     <mbean code="org.jboss.logging.Log4jService"            name="jboss.system:type=Log4jService,service=Logging"            xmbean-dd="resource:xmdesc/Log4jService-xmbean.xml">         <attribute name="ConfigurationURL">resource:log4j.xml</attribute>         <attribute name="Log4jQuietMode">true</attribute>         <!-- How frequently in seconds the ConfigurationURL is checked for              changes -->         <attribute name="RefreshPeriod">60</attribute>     </mbean> 

The RefreshPeriod attribute controls the time between checks. For development use, 5 or 10 seconds would be a more useful setting. Setting the value to a low value now will make the remaining labs go more quickly.



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