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 Gary Gregory <ga...@gmail.com> on 2012/09/28 20:04:11 UTC

Help understanding StatusLogger

Hi All:

Consider what happens when no one listens to a
org.apache.logging.log4j.status.StatusLogger in log(Marker, String, Level,
Message, Throwable):

    /**
     * Add an event.
     * @param marker The Marker
     * @param fqcn   The fully qualified class name of the <b>caller</b>
     * @param level  The logging level
     * @param msg    The message associated with the event.
     * @param t      A Throwable or null.
     */
    @Override
    public void log(Marker marker, String fqcn, Level level, Message msg,
Throwable t) {
        StackTraceElement element = null;
        if (fqcn != null) {
            element = getStackTraceElement(fqcn,
Thread.currentThread().getStackTrace());
        }
        StatusData data = new StatusData(element, level, msg, t);
        msgLock.lock();
        try {
            messages.add(data);
        } finally {
            msgLock.unlock();
        }
        for (StatusListener listener : listeners) {
            listener.log(data);
        }
    }

Code runs in the methods, creating objects and so on only to have:

        for (StatusListener listener : listeners) {
            listener.log(data);
        }


do nothing. But before that, the method records a data object in the messages
queue.

Why? It does not seem that references are used ATM.

If thought the method could be guarded by this at the top:

        if (listeners.isEmpty()) {
            return;
        }

But then the behavior would be different since the data object would not be
queued.

Please help me understand.

Thank you!

Gary
-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
JUnit in Action, 2nd Ed: <http://goog_1249600977>http://bit.ly/ECvg0
Spring Batch in Action: <http://s.apache.org/HOq>http://bit.ly/bqpbCK
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Re: Help understanding StatusLogger

Posted by Ralph Goers <ra...@dslextreme.com>.
As I recall the intention was to add some JMX support that could go and get messages in the queue, but I haven't implemented much of anything for JMX yet.  I'll have to look at it again but I think the idea was to be able to go back and report errors even if no listener was registered.

Ralph

On Sep 28, 2012, at 11:04 AM, Gary Gregory wrote:

> Hi All:
> 
> Consider what happens when no one listens to a org.apache.logging.log4j.status.StatusLogger in log(Marker, String, Level, Message, Throwable):
> 
>     /**
>      * Add an event.
>      * @param marker The Marker
>      * @param fqcn   The fully qualified class name of the <b>caller</b>
>      * @param level  The logging level
>      * @param msg    The message associated with the event.
>      * @param t      A Throwable or null.
>      */
>     @Override
>     public void log(Marker marker, String fqcn, Level level, Message msg, Throwable t) {
>         StackTraceElement element = null;
>         if (fqcn != null) {
>             element = getStackTraceElement(fqcn, Thread.currentThread().getStackTrace());
>         }
>         StatusData data = new StatusData(element, level, msg, t);
>         msgLock.lock();
>         try {
>             messages.add(data);
>         } finally {
>             msgLock.unlock();
>         }
>         for (StatusListener listener : listeners) {
>             listener.log(data);
>         }
>     }
> 
> Code runs in the methods, creating objects and so on only to have:
> 
>         for (StatusListener listener : listeners) {
>             listener.log(data);
>         }
> 
> 
> do nothing. But before that, the method records a data object in the messages queue.
> 
> Why? It does not seem that references are used ATM.
> 
> If thought the method could be guarded by this at the top:
> 
>         if (listeners.isEmpty()) {
>             return;
>         }
> 
> But then the behavior would be different since the data object would not be queued.
> 
> Please help me understand.
> 
> Thank you!
> 
> Gary
> -- 
> E-Mail: garydgregory@gmail.com | ggregory@apache.org 
> JUnit in Action, 2nd Ed: http://bit.ly/ECvg0
> Spring Batch in Action: http://bit.ly/bqpbCK
> Blog: http://garygregory.wordpress.com 
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory