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 eric rose <er...@forge.com.au> on 2003/01/31 06:16:46 UTC

Default rendering and arrays

-----BEGIN PGP SIGNED MESSAGE-----

Hi,

I'm new to this list so forgive me if I'm asking about something that
has been kicked to death previously - I've looked through mailing list
archives and googled for the topic but found nothing.

I'm working on moving trace/debug logging from an in-house package to
log4j, and one of the things most developers here want is a way to log
an array (generally an array of primitives, as well). The default
rendering behaviour is toString() which isn't particularly useful, so
I've been looked into changing this.

What I've ended up with is a specialised Hierarchy, which contains a
specialised RendererMap, which contains a specialised DefaultRenderer -
all because it's not possible to set the default rendering behaviour
directly. What would be ideal, AFAICS, is to allow a setter on
RendererMap.defaultRenderer or to maybe allow initial configuration of
this as well as allowing new renderer mappings to be added.

Are there reasons why these suggestions are a bad idea?

Eric

- --
Eric Rose           | If you can't see the  , they can't eat you.
eric@forge.com.au   | Don't see the  , don't see the  .

***********************************************************************
This message contains privileged and confidential information intended
only for the use of the addressee named above.  If you are not the
intended recipient of this message you must not disseminate, copy or
take any action in reliance on it.  If you have received this message
in error please notify the sender immediately.  Any views expressed in
this message are those of the individual sender, except where the
sender specifically states them to be the views of another (including
a Body Corporate).
************************************************************************


-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.8
Comment: Using PGP with Mozilla - http://enigmail.mozdev.org

iQCVAwUBPjl6HzbHwE/t2hedAQEJqAQAjVcrApW0cSEYNZC4OXikTWKbUeFhdOt2
xQYaoESgk3u5wbucBGiwgwNQoiE+FYHpkbsx7xEEpwy6uGEdOuQ5TFmpBzf8NPlB
K8lThTPa8SQzCdXw/EBmmphYzue2t+kzeq8tmesAgrXbpjZMnE7VqPIUZ37709Ck
Uv+KQRmbQuw=
=PPFT
-----END PGP SIGNATURE-----


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


Re: Default rendering and arrays

Posted by eric rose <er...@forge.com.au>.
-----BEGIN PGP SIGNED MESSAGE-----

Ceki Gülcü wrote:
| Hi Eric,
|
| Although it is not possible to configure rendering for array classes in
| a configuration script, it is possible to do so programattically. Have
| you tried to do the following?

This was the first idea I had, but it was quickly apparent to me that
this approach is cumbersome if there are a large number of classes
involved. We develop management systems and typically we have in the
order of 100 various 'managed object' types.

A quick test of trying to log an array of doubles shows that the
suggested rendering doesn't work as suggested unless you add the statement.

~    h.setRenderer(double[].class, ar);


Unfortunately this trick doesn't work with Object[].class, and we would
have to associate the ArrayRenderer with all 100 types of managed
objects, and then have a very large if() construction to maintain.

As far as I can see the only way to render an arbitrary array is to
change the behaviour of the default renderer and let all arrays fall
through to it. What I have ended up doing, is create my own Hierarchy
with a default renderer that has the following logic:

~   if (o.getClass().isArray()) {
~      Object[] array = (Object[]) o;
~      StringBuffer buffy = new StringBuffer("[");
~      Object elt;
~      String renderedElt;
~      for (int i = 0; i < array.length-1; i++) {
~         elt = array[i];
~         renderedElt = myRenderer.findAndRender(elt);
~         buffy.append(renderedElt).append(", ");
~      }
~      elt = array[array.length-1];
~      renderedElt = myRenderer.findAndRender(elt);
~      buffy.append(renderedElt).append("]");
~      return buffy.toString();
~   } else {
~      return o.toString();
~   }

I'm still ironing out some issues on the Hierarchy side, but this has
enabled me to print arrays in useful form.

Eric

- --
Eric Rose           | If you can't see the  , they can't eat you.
eric@forge.com.au   | Don't see the  , don't see the  .

***********************************************************************
This message contains privileged and confidential information intended
only for the use of the addressee named above.  If you are not the
intended recipient of this message you must not disseminate, copy or
take any action in reliance on it.  If you have received this message
in error please notify the sender immediately.  Any views expressed in
this message are those of the individual sender, except where the
sender specifically states them to be the views of another (including
a Body Corporate).
************************************************************************


-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.8
Comment: Using PGP with Mozilla - http://enigmail.mozdev.org

iQCVAwUBPj0pXjbHwE/t2hedAQE+aAP/TTI1dEPS2nYdJPZ8e/Nb2KveE1WZeDuR
shzNaHqOFQoUDrqjMQOs/Fv2NbELWQ/bX9xRnol0Uf5Fs+eXU7wwWHdKY5mOI65N
z/cR9Dw1P8AqSetPT3bTg2Va5nl6Ib1ZYP8eEDNEgklQT+iJfhuZvo4vZqqiSxzM
AhDEgN2m4w8=
=Q1/4
-----END PGP SIGNATURE-----


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


Re: Default rendering and arrays

Posted by Ceki Gülcü <ce...@qos.ch>.
Hi Eric,

Although it is not possible to configure rendering for array classes in a 
configuration script, it is possible to do so programattically. Have you 
tried to do the following?

import org.apache.log4j.*;
import org.apache.log4j.spi.*;
import org.apache.log4j.or.ObjectRenderer;

public class xy {

   public static void main(String[] args) {

     Logger logger = Logger.getLogger(xy.class);
     BasicConfigurator.configure();
     Hierarchy h = (Hierarchy) LogManager.getLoggerRepository();
     ArrayRenderer ar = new ArrayRenderer();
     h.setRenderer(int[].class, ar);
     logger.debug(new int[] {1, 2, 3});
   }
}

class ArrayRenderer implements ObjectRenderer {
   public String doRender(Object o) {
     if(o instanceof int[]) {
       int[] a = (int[]) o;
       // code could be written better but its early in the morning
       StringBuffer buf = new StringBuffer();
       buf.append("[");
       for(int i = 0; i < a.length; i++) {
         buf.append(a[i] + " ");
       }
       buf.append("]");
       return buf.toString();
     } else if (o instanceof double[])
       // handle double[]
       return "this is a double array";
       else {
       return "---"+o.toString();
     }
   }
}

Would the above work for you?

At 16:16 31.01.2003 +1100, eric rose wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>
>Hi,
>
>I'm new to this list so forgive me if I'm asking about something that
>has been kicked to death previously - I've looked through mailing list
>archives and googled for the topic but found nothing.
>
>I'm working on moving trace/debug logging from an in-house package to
>log4j, and one of the things most developers here want is a way to log
>an array (generally an array of primitives, as well). The default
>rendering behaviour is toString() which isn't particularly useful, so
>I've been looked into changing this.
>
>What I've ended up with is a specialised Hierarchy, which contains a
>specialised RendererMap, which contains a specialised DefaultRenderer -
>all because it's not possible to set the default rendering behaviour
>directly. What would be ideal, AFAICS, is to allow a setter on
>RendererMap.defaultRenderer or to maybe allow initial configuration of
>this as well as allowing new renderer mappings to be added.
>
>Are there reasons why these suggestions are a bad idea?
>
>Eric
>
>- --
>Eric Rose           | If you can't see the  , they can't eat you.
>eric@forge.com.au   | Don't see the  , don't see the  .
>
>***********************************************************************
>This message contains privileged and confidential information intended
>only for the use of the addressee named above.  If you are not the
>intended recipient of this message you must not disseminate, copy or
>take any action in reliance on it.  If you have received this message
>in error please notify the sender immediately.  Any views expressed in
>this message are those of the individual sender, except where the
>sender specifically states them to be the views of another (including
>a Body Corporate).
>************************************************************************
>
>
>-----BEGIN PGP SIGNATURE-----
>Version: PGP 6.5.8
>Comment: Using PGP with Mozilla - http://enigmail.mozdev.org
>
>iQCVAwUBPjl6HzbHwE/t2hedAQEJqAQAjVcrApW0cSEYNZC4OXikTWKbUeFhdOt2
>xQYaoESgk3u5wbucBGiwgwNQoiE+FYHpkbsx7xEEpwy6uGEdOuQ5TFmpBzf8NPlB
>K8lThTPa8SQzCdXw/EBmmphYzue2t+kzeq8tmesAgrXbpjZMnE7VqPIUZ37709Ck
>Uv+KQRmbQuw=
>=PPFT
>-----END PGP SIGNATURE-----
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-dev-help@jakarta.apache.org
>

--
Ceki



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