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 "Schumacher Arno,E2" <Ar...@t-mobile.de> on 2003/04/02 15:14:19 UTC

Array Renderer + Configuration

I think there was some discussion about that 
in 1/2003; don't know whether there was already
a proposal for rendering arrays plus XML-configuration.
Here is my suggestion for that. It should work fine 
for all kind of arrays (int[], String[], Object[],...).
Have fun.

Arno Schumacher / Syngenio AG


Configuration:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <renderer renderingClass="de.tmobile.portal.log4j.ArrayRenderer"
        renderedClass="[Ljava.lang.Object;"/>

[..]
</log4j:configuration>

Java:

package de.tmobile.portal.log4j;

import java.lang.reflect.Array;

import org.apache.log4j.or.ObjectRenderer;

public class ArrayRenderer implements ObjectRenderer
{
    
    /* (non-Javadoc)
     * @see org.apache.log4j.or.ObjectRenderer#doRender(java.lang.Object)
     */
    public String doRender(Object arg)
    {
        return asString( arg );
    }
        
    /**
     * Returns a <code>String </code>representation of the provided array object.
     * @param array an array
     * @return a <code>String </code>representation of the passed array
     * @exception a <code>IllegalArgumentException</code> in case the object passed
     *    as parameter is not an array.
     */
    private static String asString(Object array)
    {
        if (array == null)
        {
            return "null";
        }
        else
        {
            if (array.getClass().isArray())
            {
                int iLength = Array.getLength(array);
                if (iLength == 0)
                {
                    return "[]";
                }
                else
                {
                    StringBuffer sb = new StringBuffer();

                    for (int i = 0; i < iLength; i++)
                    {
                        sb.append("[");
                        Object o = Array.get(array, i);
                        String str;
                        try
                        {
                            if (o == null)
                            {
                                str = "null";
                            }
                            else if (o.getClass().isArray())
                            {
                                str = asString(o);
                            }
                            else
                            {
                                str = o.toString();
                            }
                        }
                        catch (Throwable t)
                        {
                            str = "?";
                        }
                        sb.append(str);
                        sb.append("]");
                    }

                    return sb.toString();
                }
            }
            else
            {
                throw new IllegalArgumentException("Not an array: " + array);
            }
        }
    }
}


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