0 Replies Latest reply on Oct 22, 2002 1:27 PM by Wei Jiang

    Logging with whereabouts using open source and freeware

    Wei Jiang Newbie

      You can find the html version of this at:
      http://www.acelet.com/whitepaper/loggingWithWhereabouts.html

      Logging with whereabouts using open source and freeware

      The purpose of logging is to find out what had happened when needed. When the time comes to read log messages, you want to know both the log message and its whereabouts (class name, method name, file name and line number). So you need to hard code whereabouts.

      But hard coded whereabouts are very difficult to maintain: when you modify your source code, line number changes; when you copy and paste a line, its class name and method name change. If whereabouts are wrong, you introduce bugs in your logging logic and the log messages are useless at the best.

      This article shows you an example of using freeware Redress tool to rectify whereabouts programmatically in your Makefile or Ant build file. So your whereabouts are always correct for both Java and JSP source file.

      Redress tool is part of SuperLogging at http://www.ACElet.com. SuperLogging also provides an open source wrapper Alog.java, which redirects log method calls to your favorite logging package. Redress tool can rectify whereabouts information on all Alog's method calls in your application. So, if you call Alog's log methods, these calls will be rectified by Redress.

      JDK 1.4 introduces a new utility package java.util.logging. The example in this article is based on JDK logging. Log4J is a cousin of JDK logging. Log4J users should have no difficulties to modify this example for Log4J. Both JDK logging and Log4J are excellent logging software for single JVM.

      Note: Redress tool rectifies method calls on Alog, not JDK logging. You need to call Alog instead of JDK logging in your application.



      Source code of Alog.java

      The following is the source code of Alog's JDK logging version. It serves as an library file and should be on your CLASSPATH:

      /**
      * Copyright Acelet Corp. 2000. All rights reserved
      *
      * License agreement begins >>>>>>>>>>
      * This program (com.acelet.opensource.logging.Alog) ("Software") is an
      * open source software.
      *
      * LICENSE GRANT. The Software is owned by Acelet Corporation ("Acelet").
      * The Software is licensed to you ("Licensee"). You are granted a
      * non-exclusive right to use, modify, distribute the Software for either
      * commercial or non-commercial use for free, as long as:
      * 1. this copyright paragraph remains with this file.
      * 2. this source code (this file) must be included with distributed
      * binary code.
      *
      * NO WARRANTY. This comes with absolutely no warranty.
      *
      * <<<<<<<<<< License agreement ends
      *
      * The purpose of releasing this open source program is to prevent vendor
      * lock in.
      *
      * You can code your program using this class to indirectly use Acelet
      * SuperLogging (com.acelet.logging). If later you want to swith to other
      * logging package, you do not need to modify your program. All you have
      * to do is:
      * 1. modify this file to redirect to other logging packages.
      * 2. replace existing com.acelet.opensource.Alog with your modified one.
      * 3. you may have to reboot your EJB server to make the changes effect.
      *
      *
      * This program is just a wrapper. For detail information about the methods
      * see documents of underline package, such as com.acelet.logging.Logging.
      *
      *
      * Visit http://www.ACElet.com for more information.
      *
      *
      * This file is a modified for using JDK logging as an EXAMPLE.
      *
      * You can use Redress tool to keep your whereabouts information
      * always correct. See http://www.ACElet.com/freeware for detail.
      *
      * Please see http://www/ACElet.com/opensource if you want to see the
      * original version.
      *
      **/

      package com.acelet.opensource.logging;

      import java.util.logging.*;

      public final class Alog {
      /**
      * Log level value: something will prevent normal program execution.
      */
      public static int SEVERE = 1000;

      /**
      * Log level value: something has potential problems.
      */
      public static int WARNING = 900;

      /**
      * Log level value: for significant messages.
      */
      public static int INFO = 800;

      /**
      * Log level value: for config information in debugging.
      */
      public static int CONFIG = 700;

      /**
      * Log level value: for information such as recoverable failures.
      */
      public static int FINE = 500;

      /**
      * Log level value: for information about entering or returning a
      * method, or throwing an exception.
      */
      public static int FINER = 400;

      /**
      * Log level value: for detail tracing information.
      */
      public static int FINEST = 300;


      static Logger logger;

      static {
      logger = Logger.getLogger("");
      }

      public Alog() {
      }

      public static void alert(String subject, String message) {
      }

      public static void error(String text, int level, String fullClassName,
      String methodName, String baseFileName, int lineNumber) {
      String[] para = {lineNumber + "", baseFileName};
      logger.logp(getLevel(level), fullClassName, methodName, text, para);
      }

      public static Level getLevel(int levelValue) {
      if (levelValue == SEVERE)
      return Level.SEVERE;
      else if (levelValue == WARNING)
      return Level.WARNING;
      else if (levelValue == INFO)
      return Level.INFO;
      else if (levelValue == CONFIG)
      return Level.CONFIG;
      else if (levelValue == FINE)
      return Level.FINE;
      else if (levelValue == FINER)
      return Level.FINER;
      else if (levelValue == FINEST)
      return Level.FINEST;
      else
      return Level.ALL;
      }

      public static void log(String text, int level, String fullClassName,
      String methodName, String baseFileName, int lineNumber) {
      String[] para = {lineNumber + "", baseFileName};
      logger.logp(getLevel(level), fullClassName, methodName, text, para);
      }

      public static void sendMail(String to, String from, String subject,
      String text) throws Exception {
      }

      public static void sendMail(String to, String cc, String bcc, String from,
      String subject, String text) throws Exception {
      }
      }


      Test program

      The simple test program is Test.java:

      import com.acelet.opensource.logging.Alog;

      public class Test {
      public static void main(String argv[]){
      Alog.log("Holle world", Alog.SEVERE, "wrongClassName", "wrongMethod",
      "wrongFileName", -1);
      }
      }
      How to run the test program

      1. Compile Alog.java (JDK 1.4 or later, not before):

      javac Alog.java

      2. Download freeware Redress tool from http://ACElet.com/freeware.

      3. Run Redress tool:

      java -cp redress.jar Test.java

      4. Check Test.java. The Alog.log method call should be rectified.

      5. Run test program:

      java Test

      You should see log message with correct class name and method name.