Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

6.4 Create Code Templates for Logging

Although not part of Java project configuration, the creation of code templates is naturally a part of our setup to improve development productivity. We will create several new templates that insert common statements used by the log4j logging facility.

Open the preferences page for Java > Editor > Templates and press the New... button. Fill in the template as shown in Figure 6-11. Press the Insert Variable... button while filling the template pattern to get a list of variables that automatically substitute values when the template is applied in your code.

Figure 6.11Figure 6-11 Create a new code template to insert log4j variable declaration.

Save the template and apply it within your CatalogItem class. Position the cursor below the class declaration, type "logger", and press Ctrl+Space to activate Content Assist. Select the logger template from the suggestion list and press Enter. Notice that the enclosing class name is automatically substituted when the following code is generated:

public class CatalogItem {
  private static Logger logger = Logger

However, you still receive an error that the Logger class cannot be resolved. Position the cursor over the Logger class name and press the shortcut Ctrl+Shift+M. Two different classes with this name are available:


A logging facility was added to Java version 1.4 that is similar in name and function to the Apache log4j library. But many developers still choose to use log4j instead of the java.util.logging package. You can prevent accidental choice of the wrong class and simplify use of Content Assist by filtering available types within Eclipse to exclude the java.util.logging package. Open the preferences page for Java > Type Filters as shown in Figure 6-12 and add a new type filter.

Figure 6.12Figure 6-12 Filter the available types to exclude built-in Java logging, leaving the Apache log4j logging types as default.

Now when you press Ctrl+Shift+M to add an import statement for Logger, it is inserted immediately without prompting you because only one class by this name is available.

Now we'll create one more template for logging error messages. In Chapter 5, we used Quick Fix to automatically wrap a try/catch block around a URL class constructor to catch the MalformedURLException. We can now replace the default generated catch block with an error log message.

Create a new template named logerr and assign this pattern:

logger.error(${message}, ${e});

When you want to apply this pattern to log errors, simply type "logerr," press Ctrl+Space to activate Content Assist, select the template name, and press Enter. A single line is inserted, which is shown as bold here:

try {
  URL url = new URL("http://www.eclipse.org");
} catch (MalformedURLException e) {
  logger.error("message", e);

As with the other templates described in Chapter 5, you are prompted to replace the template variables. The two variables named message and e are highlighted in this pattern so that you can replace them with appropriate values, although the default name e is correct in this case.

  • + Share This
  • 🔖 Save To Your Account