Didier's post conn, ife, sop & try inspired me to work out an idea I had for some time: create a code template for declaring a log4j member variable in a class. Log4j is pre-configured in every JHeadstart generated application, and is quite easy to use. The only cumbersome thing is having to add a log member variable to each class where you want to log something.
You have to add something like this:import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
...
// used for logging on 6 levels: trace, debug, info, warn, error, fatal
private static Log log = LogFactory.getLog(MyClass.class);
It is ideal for turning into a JDeveloper code template! A code template can add both imports and code (already available in JDeveloper 10.1.2), and even replace certain variables like class name (new in JDeveloper 10.1.3).
Step by step (analogous to Frank's post JDeveloper: How to automatically add the class name and creation date to a Java file and its follow-up post)
- Tools --> Preferences
- Code Editor --> Code Templates
- Press the Add button to create a new template
- Name it log
- Under the Code tab, put
(non-indented):
// used for logging on 6 levels: trace, debug, info, warn, error, fatal
private static Log log = LogFactory.getLog($class$.class); - Under
the Imports tab, put (without import keywords and
semicolons):
org.apache.commons.logging.Log
org.apache.commons.logging.LogFactory - Under
the Variables tab, change the Type of the class variable to curClass
and uncheck the Editable checkbox (if you leave the variable editable,
you must confirm the variable using Tab or Enter each time you use the
template, otherwise the template won't work in a second file).
In JDeveloper 10.1.2, instead of putting a variable in the code, leave the cursor at the position of the variable when you save the code template. - Confirm and close the preference dialog
log and press ctrl+enter
This will have
the following effect (possibly after restarting JDeveloper):
"YourOwnClassName"
was automatically filled in. If you have unchecked Editable
(recommended) when defining the Code Template, then the value is not
highlighted and you don't have to confirm the value using Tab or Enter.
The
next step of course is to create a code template for the code that
actually produces a log message. We might as well incorporate a best
practice for the performance of log4j: don't do any expensive
construction of a log message unless you know the relevant log level is
enabled.
{
log.debug(... expensive calculation of result...);
}
So let's create a code template called debug, that adds the following code (no imports or variable properties):
if (log.isDebugEnabled()){
log.debug($myMessage$);
}
Of course, this time the $myMessage$ variable must be editable. In the code editor if you type debug and press ctrl+enter, the if-statement is added to your class and "myMessage" is highlighted, ready for typing over. Confirm the new message value using Tab or Enter. In a similar way you can define code templates called trace, info, warn, error and fatal!
Comments (2)
I have been adding code to my templates, now all my templates have disappeared, and when I press control-enter, nothing happens even though I have added my own template again to what is now a list containing only one template.
Posted by Bradlee Sargent | May 8, 2007 1:40 PM
Posted on May 8, 2007 13:40
Bradlee,
Can you ask this question in the JDeveloper discussion forum on OTN? The url is http://forums.oracle.com/forums/forum.jspa?forumID=83
Thanks,
Posted by Sandra Muller | May 10, 2007 7:20 AM
Posted on May 10, 2007 07:20