logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ralph Goers <ralph.go...@dslextreme.com>
Subject Re: log4j2 parameterized msg with throwable
Date Sat, 17 Sep 2011 04:01:59 GMT

On Sep 16, 2011, at 10:32 AM, Scott Deboy wrote:

> Does it help to provide the Throwable as the first argument?  That's what some of the
overloaded LogSF/MF methods do to avoid ambiguity, if I recall.

Putting the Throwable before the argument array does help.  Unfortunately it also makes bridging
an API such as the one below from SLF4J a bit harder.

    public void error(Marker marker, String s, Object[] objects) {
        if (isErrorEnabled(marker)) {
            log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR,
                new ParameterizedMessage(s, objects), null);
        }
    }

Since SLF4J doesn't include an explicit Throwable we would need to do

    public void error(Marker marker, String s, Object[] objects) {
        if (isErrorEnabled(marker)) {
            Object[] arrary;
            Throwable t = null;
            if (objects == null || objects.length == 0) {
                array = objects;
            } else if (objects[objects.length - 1] instanceof Throwable) {
                t = objects[objects.length - 1];
               array = new Object[object.length -1];
               System.arraycopy(objects, 0, array, 0, objects.length - 1);
            }
            log((org.apache.logging.log4j.Marker) marker, FQCN, Level.ERROR,
                new ParameterizedMessage(s, array), t);
        }
    }

Currently parameterized message determines whether the last argument is a Throwable.  As John
has pointed out though, it is being ignored when the LogEvent is created and by Layouts, etc.

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


Mime
View raw message