simply logging

Friday, 5 August, 2005

Up until now, I have used Log4j in most of my development efforts — be they personal, open-source or corporate (there’s an argument I should have been using a logging bridge of some kind, to insulate any 3rd party users of my code from having to use my logging system of choice — but most of the work I’ve been doing hasn’t had a huge number of 3rd party users, so it hasn’t been that much of an issue). However, I recently came across Graham Lea’s simple-log; the basic premise of which appeals to my drive to simplify things as much as possible. So, bye bye, Log4J, it’s been great!

I’m currently running a couple of java applications which rely on the commons-logging bridge, alongside of which I use velocity (which itself uses Avalon LogKit or Log4J by default). In order to integrate simple-log and drop log4j out of my lib path, I need to provide a logging implementation for commons-logging, and also change the default velocity runtime logsystem.

SimpleLogger.java (stick your own package in front of it), does both. To use it, make sure you set the runtime property for commons, e.g.:


-Dorg.apache.commons.logging.Log=yourpackagehere.SimpleLogger

and then set the logsystem for velocity (where ve is the VelocityEngine), using the same property:


ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,     System.getProperty('org.apache.commons.logging.Log'))


package yourpackagehere;

import org.apache.commons.logging.Log;
import org.apache.velocity.runtime.log.LogSystem;
import org.apache.velocity.runtime.RuntimeServices;

/**
* wrapper for simple-log to use with jakarta-commons and velocity
*/
public class SimpleLogger implements Log, LogSystem {

 org.grlea.log.SimpleLogger log;

 /**
  * used for velocity initialisation
  */
 public SimpleLogger() {
 }

 /**
  * used for jakarta commons-logging initialisation
  */
 public SimpleLogger(String name) {
     init(name);
 }

 /**
  * initialise the log system with a name (simple-log instanceId)
  */
 private void init(String name) {
     log = new org.grlea.log.SimpleLogger(SimpleLogger.class, name);
 }

 public void debug(Object obj) {
     log.debug(obj.toString());
 }

 public void debug(Object obj, Throwable throwable) {
     log.debug(obj.toString());
     log.debug(throwable.toString());
 }

 public void error(Object obj) {
     log.error(obj.toString());
 }

 public void error(Object obj, Throwable throwable) {
     log.error(obj.toString());
     log.error(throwable.toString());
 }

 public void fatal(Object obj) {
     log.fatal(obj.toString());
 }

 public void fatal(Object obj, Throwable throwable) {
     log.fatal(obj.toString());
     log.fatal(throwable.toString());
 }

 public void info(Object obj) {
     log.info(obj.toString());
 }

 public void info(Object obj, Throwable throwable) {
     log.info(obj.toString());
     log.info(throwable.toString());
 }

 public boolean isDebugEnabled() {
     return log.getDebugLevel() == org.grlea.log.DebugLevel.L5_DEBUG;
 }

 public boolean isErrorEnabled() {
     return log.getDebugLevel() == org.grlea.log.DebugLevel.L2_ERROR;
 }

 public boolean isFatalEnabled() {
     return log.getDebugLevel() == org.grlea.log.DebugLevel.L1_FATAL;
 }

 public boolean isInfoEnabled() {
     return log.getDebugLevel() == org.grlea.log.DebugLevel.L4_INFO;
 }

 public boolean isTraceEnabled() {
     return log.getDebugLevel() == org.grlea.log.DebugLevel.L5_DEBUG;
 }

 public boolean isWarnEnabled() {
     return log.getDebugLevel() == org.grlea.log.DebugLevel.L3_WARN;
 }

 public void trace(Object obj) {
     log.debug(obj.toString());
 }

 public void trace(Object obj, Throwable throwable) {
     log.debug(obj.toString());
     log.debug(throwable.toString());
 }

 public void warn(Object obj) {
     log.warn(obj.toString());
 }

 public void warn(Object obj, Throwable throwable) {
     log.warn(obj.toString());
     log.warn(throwable.toString());
 }

 /**
  * initialise logging for velocity
  */
 public void init(RuntimeServices runtimeServices) throws Exception {
     init("velocity");
 }

 /**
  * log a velocity message
  */
 public void logVelocityMessage(int level, String str) {
     switch (level) {
         case LogSystem.DEBUG_ID:
             log.debug(str);
             break;
         case LogSystem.INFO_ID:
             log.info(str);
             break;
         case LogSystem.WARN_ID:
             log.warn(str);
             break;
         case LogSystem.ERROR_ID:
             log.error(str);
             break;
         default:
             log.ludicrous(str);
             break;
     }
  }
}

Leave a Reply