Skip to content

Commit

Permalink
adding ERXThreadStorageAppender.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Robert committed May 21, 2012
1 parent 71f46ad commit 6aa52e2
Showing 1 changed file with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) NetStruxr, Inc. All rights reserved.
*
* This software is published under the terms of the NetStruxr
* Public Software License version 0.5, a copy of which has been
* included with this distribution in the LICENSE.NPL file. */
package er.extensions.logging;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

import er.extensions.appserver.ERXSession;
import er.extensions.foundation.ERXThreadStorage;

/**
* Appends all logging in this thread to thread storage if ERXSession.session()
* is set. In case of an exception (or whenever you want to), you can call
* <code>ERXThreadStorageAppender.messages()</code> to get the full log. <br>
* This is useful because you don't clutter up your log with stuff you don't
* really need most of the time, but still get full logging in case something
* bad happens.
*
* @author ak
*/

public class ERXThreadStorageAppender extends AppenderSkeleton {

public void append(LoggingEvent event) {
if (ERXSession.session() != null) {
StringBuffer buf = buffer();
//buf.append(getLayout().format(event));
}
}

protected static StringBuffer buffer() {
StringBuffer buf = (StringBuffer) ERXThreadStorage.valueForKey("ERXThreadStorageAppender.buffer");
if(buf == null) {
buf = new StringBuffer();
ERXThreadStorage.takeValueForKey(buf, "ERXThreadStorageAppender.buffer");
}
return buf;
}

public void close() {
// nothing
}

public static String message() {
return buffer().toString();
}

public boolean requiresLayout() {
return true;
}
}

0 comments on commit 6aa52e2

Please sign in to comment.