Logging Generated SQL for CMP


So far we've seen ways to log from our domain objects and the web server. It would also be nice to see what was going on with the CMP layer of our application. The CMP layer takes care of our application's interaction with DB, and it can be very valuable to see what it is doing, and how. Let's see how to turn logging on for the CMP layer so that we can do just that.

How do I do that?

To turn on logging for the CMP, you need to add this category to your log4j.xml file:

     <category name="org.jboss.ejb.plugins.cmp">         <priority value="DEBUG"/>     </category> 

You will also need to change the CONSOLE appender to have a Threshold of DEBUG:

     <appender name="CONSOLE" >         <errorHandler />         <param name="Target" value="System.out"/>         <param name="Threshold" value="DEBUG"/>         <layout >             <!-- The default pattern: Date Priority [Category] Message\n -->             <param name="ConversionPattern"                    value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>         </layout>     </appender> 

Now save the log4j.xml file and in about a minute your change will be applied. Once it is, add a new todo item or complete one you already have, and then look in the logfile. You should see something like this:

     21:52:09,960 DEBUG [Task#findTasksForUser] Executing SQL: SELECT t0_t.id     FROM TASK t0_t WHERE (t0_t.user = ?)     21:52:10,013 DEBUG [Task] Executing SQL: SELECT id, name, user, startedDate,     completedDate FROM TASK WHERE (id=?) OR (id=?) OR (id=?) OR (id=?) OR (id=?)     OR (id=?) OR (id=?)     21:52:10,072 DEBUG [Task] load relation SQL: SELECT task, id FROM COMMENT WHERE     (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?)     21:52:20,003 INFO  [STDOUT] GENKEY!     21:52:20,198 DEBUG [TaskBean] creating task 962b26a9ac100a3800de002d994a5508     for user pinky     21:52:20,198 DEBUG [Task] Executing SQL: SELECT COUNT(*) FROM TASK WHERE id=?     21:52:20,205 DEBUG [Task] Executing SQL: INSERT INTO TASK (id, name, user,     startedDate, completedDate) VALUES (?, ?, ?, ?, ?)     21:52:20,222 INFO  [STDOUT] Created: local/Task@11157534:     962b26a9ac100a3800de002d994a5508     21:52:20,247 DEBUG [Task#findTasksForUser] Executing SQL: SELECT t0_t.id FROM     TASK t0_t WHERE (t0_t.user = ?)     21:52:20,396 DEBUG [Task] Executing SQL: SELECT id, name, user, startedDate,     completedDate FROM TASK WHERE (id=?) OR (id=?) OR (id=?) OR (id=?) OR (id=?) OR      (id=?) OR (id=?) OR (id=?)     21:52:20,403 DEBUG [Task] load relation SQL: SELECT task, id FROM COMMENT WHERE     (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?)     OR (task=?)     21:52:20,537 DEBUG [Task#findTasksForUser] Executing SQL: SELECT t0_t.id FROM     TASK t0_t WHERE (t0_t.user = ?)     21:52:20,541 DEBUG [Task] Executing SQL: SELECT id, name, user, startedDate,     completedDate FROM TASK WHERE (id=?) OR (id=?) OR (id=?) OR (id=?) OR (id=?) OR     (id=?) OR (id=?) OR (id=?)     21:52:20,548 DEBUG [Task] load relation SQL: SELECT task, id FROM COMMENT     WHERE (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?) OR (task=?)     OR (task=?) OR (task=?) 

What just happened?

You enabled CMP logging by creating a category that the CMP layer will use. You also bumped up the logging target level to DEBUG. Then you waited for the server to detect those changes, and you saw some of the kinds of output you would get when your program interacts with the CMP.

What about...

...if you aren't seeing as much information regarding what the CMP layer is doing?

You can use the trACE level of logging. You can enable it by modifying the category you created for CMP logging to look like this:

     <category name="org.jboss.ejb.plugins.cmp">         <priority value="TRACE" />     </category> 

This will allow the CMP layer to put out all the logging information that is configured in the code, even things such as developer tracing information. This may quickly generate a very big logging file due to how much information it will dump out.

And what about the commented out sections of log4j.xml?

One thing you probably noticed is that the appenders are mostly the same each time. You can usually create a new appender by copying an existing one and changing a few of the tags' values. The properties can stay the same most of the time. Another thing you may notice is that much of log4j.xml is commented out. As you learn more about JBoss you'll want to go back and examine these commented-out sections, as they provide preconfigured settings for SMTP logging, JMS logging, etc. To enable these features, in most cases you only need to remove the comment marks. There's a lot you can get logging information on. Be sure to read more of the online docs about logging, as they also have quite a bit more information.



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