logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Schumacher Arno,E2" <Arno.Schumac...@t-mobile.de>
Subject Array Renderer + Configuration
Date Wed, 02 Apr 2003 13:14:19 GMT
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


Mime
View raw message