You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Veerappan Saravanan <ve...@integral.com> on 2001/07/12 23:36:02 UTC

Log a localized and parameterized message

Category has a method called l7dlog to help with parameterization and
localization. But it doesn't quite do what I would like it to do. 

If I have a resource bundle that has the following entry

ERROR_101=some error in the system

then if I call 

l7dlog(Priority.ERROR, ERROR_101, null);

the output would be 

"some error in the system"

instead I would like it to print the error code along with the message,
something like

"ERROR_101: some error in the system"

I know of two solutions to this...
1. change the resource bundle entry to 
ERROR_101=ERROR_101:some error in the system
but, this is erorr prone, since developers could always enter the wrong id
in the message.

2. Hack the Category class' l7dlog method to do customize it.
In this case, every upgrade to log4j, I need to change my Category class,
which could be a pain.

Are there any other solutions to this?


Help appreciated.

Thanks
Van

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


Re: Log a localized and parameterized message

Posted by Claus Wagner <Cl...@ascom.ch>.
Veerappan Saravanan wrote:
> 
> Category has a method called l7dlog to help with parameterization and
> localization. But it doesn't quite do what I would like it to do.
> 
> If I have a resource bundle that has the following entry
> 
> ERROR_101=some error in the system
> 
> then if I call
> 
> l7dlog(Priority.ERROR, ERROR_101, null);
> 
> the output would be
> 
> "some error in the system"
> 
> instead I would like it to print the error code along with the message,
> something like
> 
> "ERROR_101: some error in the system"
> 
> I know of two solutions to this...
> 1. change the resource bundle entry to
> ERROR_101=ERROR_101:some error in the system
> but, this is erorr prone, since developers could always enter the wrong id
> in the message.
> 
> 2. Hack the Category class' l7dlog method to do customize it.
> In this case, every upgrade to log4j, I need to change my Category class,
> which could be a pain.
> 
> Are there any other solutions to this?
> 
> Help appreciated.
> 
> Thanks
> Van
> 

There is another possibility to achieve this (although it is not the
proper place to do it):

override protected void forcedLog(), which is called from l7dlog, and
manipulate your message string therein:

  /**
     This method creates a new logging event and logs the event
     without further checks.  */
  protected
  void forcedLog(String fqcn, Priority priority, Object message,
Throwable t) {

    // message is "some error in the system"
    // have a getKey() method
    // squeeze the key in front of the original message 
    // or make a new string --> new_message

    callAppenders(new LoggingEvent(fqcn, this, priority, new_message,
t));
  }

HTH
Claus

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


Re: Log a localized and parameterized message

Posted by Claus Wagner <Cl...@ascom.ch>.
Veerappan Saravanan wrote:
> 
> Category has a method called l7dlog to help with parameterization and
> localization. But it doesn't quite do what I would like it to do.
> 
> If I have a resource bundle that has the following entry
> 
> ERROR_101=some error in the system
> 
> then if I call
> 
> l7dlog(Priority.ERROR, ERROR_101, null);
> 
> the output would be
> 
> "some error in the system"
> 
> instead I would like it to print the error code along with the message,
> something like
> 
> "ERROR_101: some error in the system"
> 
> I know of two solutions to this...
> 1. change the resource bundle entry to
> ERROR_101=ERROR_101:some error in the system
> but, this is erorr prone, since developers could always enter the wrong id
> in the message.
> 
> 2. Hack the Category class' l7dlog method to do customize it.
> In this case, every upgrade to log4j, I need to change my Category class,
> which could be a pain.
> 
> Are there any other solutions to this?
> 
> Help appreciated.
> 
> Thanks
> Van
> 

Hi Van and list,

I need this, too. 

Ceki: is it possible, e.g., to outsource the message building part of
the l7dlog methods into a separate method? Something similar to

public String buildMessage () {
      String msg = getResourceBundleString(key);
      // if message corresponding to 'key' could not be found in the
      // resource bundle, then default to 'key'.
      if(msg == null) {
	msg = key;
      }

      return msg;
}

Users are then able to override and perform some more exotic string
handling.

Regards
Claus

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


Re: Log a localized and parameterized message

Posted by Paul Glezen <pg...@atdial.net>.
Veerappan Saravanan wrote:
> 
> 1. change the resource bundle entry to
> ERROR_101=ERROR_101:some error in the system
> but, this is erorr prone, since developers could always enter the wrong id
> in the message.
> 
Veerappan,

I use that very method on my projects.  I don't find it error prone. 
The two "ERROR_101" strings are right next to each other.  It's pretty
easy to eyeball.

If, nevertheless, you feel it's a problem, consider running the
following AWK script on your message file.

======= messageCheck.awk =============
BEGIN { FS = "[=:]" }
/.+=.+:.+/ { if ($1 != $2) print "Whoa, better check ", $1 }
======= messageCheck.awk =============

The first line tells awk to use either '=' or ':' as field separators. 
The second line says (in English) "If the line has (some stuff)=(other
stuff):(more stuff), then print a message if (some stuff) is different
than (other stuff)."

With the following input file

======= msgid.txt =========
# This is just a comment.

ERROR_101=ERROR_101:this is message 101
ERROR_102=ERROR_102:this is message 102
ERROR_103=ERROR_103:this message has a = and a : inside it.
ERROR_104=ERROR_101:Cut and paste gone bad.
ERROR_105=ERROR_105:This is message 105
INFO_108=This message does not repeat its message id.
======= msgid.txt ==========

Then you get the following result:

$ awk -f messageCheck.awk msgid.txt
Whoa, better check ERROR_104
$

I suppose this would be a problem if you didn't have AWK. :(

- Paul

-- 
Beware of bugs in the above code.  I have
only proved it correct, not tried it.
-Don Knuth

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