You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by HALESHAPPA SATHEESHA <sa...@mail.nissan.co.jp> on 2003/01/29 09:26:09 UTC

RE: how to map application message codes in the log format using log4

Thanks a lot Jim.
I would look into your code and get back to you if need clarification.

Regards,
Satish

-----Original Message-----
From: Jim Moore [mailto:jim.moore@veritas.com]
Sent: Wednesday, January 29, 2003 3:02 PM
To: Log4J Developers List; Log4J Users List
Subject: RE: how to map application message codes in the log format
using log4


You're sending this to the wrong list.  These kinds of questions are better
for log4j-user@jakarta.apache.org

The way I do it is:

import java.util.ResourceBundle;

/**
 * @see LogMessageRenderer
 */
public class LogMessage {
  private String id;
  private Object[] vals;
  private ResourceBundle resourceBundle;

  private static final LogMessageRenderer RENDERER = new
LogMessageRenderer();

  public LogMessage(String id, ResourceBundle resourceBundle) {
    this.id = id;
    this.resourceBundle = resourceBundle;
    this.vals = null;
  }

  public LogMessage(String id, Object[] vals, ResourceBundle resourceBundle)
{
    this.id = id;
    this.resourceBundle = resourceBundle;
    this.vals = vals;
  }

  public String getId() {
    return id;
  }

  public Object[] getVals() {
    return vals;
  }

  public ResourceBundle getResourceBundle() {
    return resourceBundle;
  }

  /**
   * Returns the message represented by this LogMessage.
   */
  public String getMessage() {
    return RENDERER.doRender(this);
  }
}

---

import java.text.MessageFormat;

public class LogMessageRenderer implements
org.apache.log4j.or.ObjectRenderer {

  public String doRender(Object o) {
    if (o == null) return "null";

    if (o instanceof LogMessage) {
      LogMessage msg = (LogMessage)o;
      final String msgId = msg.getId();

      try {
        String pattern = msg.getResourceBundle().getString(msgId);

        if (pattern == null) return msgId;

        return "[" + msgId + "] " + MessageFormat.format(pattern,
msg.getVals());
      }
      catch (Throwable e) {
        // Don't let anything freak this out too badly.
        return msgId;
      }
    }
    else {
      return o.toString();
    }
  }
}

---

With this line in my log4j.properties file:

log4j.renderer.thepackagename.LogMessage=thepackagename.LogMessageRenderer


So a logging looks something like this:

Logger LOG = Logger.getLogger(MyClass.class);
ResourceBundle RB = ResourceBundle.getBundle("MyMessages");
LOG.info(new LogMessage("GSS00100", RB));
LOG.info(new LogMessage("GSS00101", new Object[] {theDriver}, RB));


For my stuff, I believe strongly in Larry Wall's "Three Virtues of A
Programmer," which includes laziness.  So I wrote a plugin for Intellij IDEA
where when I save a properties file in the form "*ErrorCode.properties" it
creates a .java file from it.  For example, if I have the file
MyErrorCode.properties:

V-23-23344: Could not load the driver called '{0}'.
V-23-23345: Could not load find the file '{0}' on drive {1}.


The file MyErrorCode.java is created upon saving the properties file looking
like this:

import java.util.ResourceBundle;
import ast.common.log.LogMessage;

/**
 * This is an error code class, based on the properties in
MyErrorCode.properties
 *
 * @see LogMessage
 */
public final class MyErrorCode {
  private static final ResourceBundle RESOURCE_BUNDLE =
ResourceBundle.getBundle("MyErrorCode");

  /**
   * MSG: Could not load the driver called '{0}'.
   */
  public static final LogMessage V_23_23344(Object arg0) {
    return new LogMessage("V-23-23344", new Object[] {arg0},
RESOURCE_BUNDLE);
  }

  /**
   * MSG: Could not load find the file '{0}' on drive {1}.
   */
  public static final LogMessage V_23_23344(Object arg0, Object arg1) {
    return new LogMessage("V-23-23345", new Object[] {arg0, arg1},
RESOURCE_BUNDLE);
  }
}


So when I log, it looks like

Logger LOG = Logger.getLogger(MyClass.class);
LOG.info(MyErrorCode.V_23_23344("Clipboard.dll"));


The beauty of that approach is that I'm GUARANTEED that the message code
will be found and that I've passed in all substitutions.  Also, since the
message is in the Javadoc, Ctl-Q (in IDEA) shows me instantly what the
message is associated with.  (And since simply saving the properties file
generates the java file, it's always in sync.)

It makes things very, very easy.

-Jim Moore



-----Original Message-----
From: HALESHAPPA SATHEESHA [mailto:satheesha@mail.nissan.co.jp] 
Sent: Wednesday, January 29, 2003 12:10 AM
To: 'log4j-dev@jakarta.apache.org'
Subject: how to map application message codes in the log format using log4


hi,

 I am developing an application using Java. Also am using log4j for logging
the application messages.  I have developed some log4j subclasses to format
the log messages according to my application requirements.

 In our application, we have a list of messages mapped with codes.  While
displaying formatted log, we need to log messages along with message codes.

 My messages would be something like this. "The driver loaded successfully."
And this message i need  to map into some code like "GSS00100". 

 Does anyone know how to do mapping between codes and messages using log4j.


Thanks in advance,
 Satish.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org