JDeveloper Code Template for log4j

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 Nimphius's (no longer online)
post "JDeveloper:
How to automatically add the class name and creation date to a Java
file"
  1. Tools -->
    Preferences
  2. Code Editor --> Code
    Templates
  3. Press the Add button
    to create a new template
  4. Name it
    log
  5. 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);

  6. Under
    the Imports tab, put (without import keywords and
    semicolons):
    org.apache.commons.logging.Log
    org.apache.commons.logging.LogFactory

  7. 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.
  8. Confirm and close the preference
    dialog
In the code
editor of the opened file
type

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.

if (log.isDebugEnabled())
{
   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:

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 on May 08, 2007 at 06:40 AM PDT #

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 on May 10, 2007 at 12:20 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Java EE Consultants - JHeadstart, ADF, JSF

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today