eclipsecoding

logging fix tool

An eclipse plugin that adds some small refactoring tools, aimed at fixing logging code. A few fixes are provided:

The logging framework the tool converts to is a configurable item. Preferences on the framework Logger class, factory method, levels available and prefered in difference circumstances can be configured in the Preference page "Code Fixing Tools":

The tools are provided from the context menu, when right clicking on any Java file, or any parent directory, such as a package, source folder, or project. Multiple selection is supported.

The context menu "Code Fixing Tools" will show several items:

Fix System Out Logging

This tool looks for invocations of java.lang.System.out.println, and variations, converting it to use a logging framework such as Log4J.

All "print" and "println" method calls to either System.out or System.err are converted.

Catch blocks with convertable logging statements will also be changed to pass in the throwable argument. In catch-blocks, if printStackTrace() calls are found against the catch-block throwable argument, they are also converted to logging framework calls - including printStackTrace(System.out) or printStackTrace(System.err).

A private static final "log" variable will be created (if it doesn't already exist). If possible the tool will import the logging classes, rather than use fully qualified names.

For example - code that looks like this:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class StdOutLogging {

  public static void main(String[] args) {
    System.out.print("some logging");
    System.out.println(7);
    String logging = "more logging";
    System.err.println(logging);
    try {
      Integer.parseInt("cause logging");
    } catch (NumberFormatException nfe) {
      nfe.printStackTrace();
      nfe.printStackTrace(System.err);
      nfe.printStackTrace(System.out);
      try {
        nfe.printStackTrace(new PrintStream(new File("err.log")));
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      }
      System.out.print("some logging");
      System.out.println(7);
      String errlogging = "more logging";
      System.err.println(errlogging);
      System.err.println(nfe);
    }
  }

}
will be transformed to code like this:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import org.apache.log4j.Logger;

public class StdOutLogging {

  private static final Logger log = Logger.getLogger(StdOutLogging.class);

  /**
   * @param args
   */
  public static void main(String[] args) {
    log.info("some logging");
    log.info(7);
    String logging = "more logging";
    log.info(logging);
    try {
      Integer.parseInt("cause logging");
    } catch (NumberFormatException nfe) {
      log.error(nfe);
      log.error(nfe);
      log.error(nfe);
      try {
        nfe.printStackTrace(new PrintStream(new File("err.log")));
      } catch (FileNotFoundException e) {
        log.error(e);
      }
      log.error("some logging", nfe);
      log.error(7, nfe);
      String errlogging = "more logging";
      log.error(errlogging, nfe);
      log.error(nfe, nfe);
    }
  }

}

Fix Error Logging Statement Arguments

This tool looks for existing logging framework calls in catch blocks, and ensures the call passes in the throwable argument from the catch clause.

For example - code that looks like this:
    try {
      Integer.parseInt("cause logging");
    } catch (NumberFormatException nfe) {
      log.error("Uh oh");
    }
will be transformed to code like this:
    try {
      Integer.parseInt("cause logging");
    } catch (NumberFormatException nfe) {
      log.error("Uh oh", nfe);
    }

Fix Error Logging Statement Levels

This tool looks for existing logging framework calls in catch blocks, and ensures the call uses the desired error logging level. See preferences "Allowed catch block logging levels" and "Preferred &catch block logging level".

For example - code that looks like this:
    try {
      Integer.parseInt("cause logging");
    } catch (NumberFormatException nfe) {
      log.info("Uh oh");
    }
will be transformed to code like this:
    try {
      Integer.parseInt("cause logging");
    } catch (NumberFormatException nfe) {
      log.error("Uh oh");
    }

All Logging Fixes

Simply runs all the above tools for the selection.