|Home | Products | Documents | Downloads | Purchase | Support | Company | Partners | Contact |

Logging with whereabouts using open source and freeware

Check out our new initiative LimpidLog: you do not need to hard-code any log statement.

The purpose of logging is to find out what has 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 a bug in your logging logic and the log messages are useless at 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 difficulty modifying this example for Log4J. Both JDK logging and Log4J are excellent logging software programs 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 >>>>>>>>>> <br>
* This program (com.acelet.opensource.logging.Alog) ("Software") is an
* open source software. <p>
*
* 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: <br>
* 1. this copyright paragraph remains with this file. <br>
* 2. this source code (this file) must be included with the distributed
* binary code.<br>
*
* NO WARRANTY. This comes with absolutely no warranty. <p>
*
* <<<<<<<<<< License agreement ends <p><p>
*
* The purpose of releasing this open source program is to prevent vendor
* lock in. <p>
*
* You can code your program using this class to indirectly use Acelet
* SuperLogging (com.acelet.logging). If you want to switch to another
* logging package later, you do not need to modify your program. All you have
* to do is: <p>
* 1. modify this file to redirect to other logging packages. <br>
* 2. replace existing com.acelet.opensource.Alog with your modified one. <br>
* 3. you may have to reboot your EJB server to make the changes effective.<br>
* <p>
*
* This program is just a wrapper. For detailed information about the methods,
* see documents of underline package, such as com.acelet.logging.Logging.
*
* <p>
* Visit http://www.ACElet.com for more information.
* <p>
*
* This file is modified for using JDK logging as an EXAMPLE.
* <br>
* You can use Redress tool to keep your whereabouts information
* always correct. See http://www.ACElet.com/freeware for detail.
* <p>
* 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.


©Copyright Acelet Corporation. All rights reserved.
Site Map Home Products: Acelet-Filer Super Suite Audit Log Easy EJB Easy JMS Easy JMX Easy Webservice Limpid Debugger Super Environment Super Log SuperStress Super Scheduler Super Log Reader Super Watchdog Super Workflow OpenSource Limpid Log Freeware Mortgage software Downloads Purchase Support Consulting Company Partners Contact us


© 2014 Acelet Corporation.