Hi there,

I've hacked up an Orion Appender to allow you to log to the
application.log file, via the Logger instance that Orion installs at
java:comp/Logger. Here it is in all it's glory, use it however you wish.

Cheers

Geoff

PS, did anyone figure out if it's possible to get orion to roll it's log
files when they get too big? ;-)

import com.evermind.util.LogEvent;
import com.evermind.util.Logger;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Layout;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.ErrorCode;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

/*
    Example Test Code

    Deploy OrionAppender with log4j in the Orion lib directory,
    then add this code to an EJB or JSP and execute it.

    Note it doesn't seem to be able to find a Logger instance when
running
    in an application client, so in this case use a normal log4j
Appender.

    Category log = Category.getInstance("for.bar.Category");
    BasicConfigurator.resetConfiguration(); // for jsp reloading
    BasicConfigurator.configure(new OrionAppender());
    log.info("A test INFO message");
    NDC.push("demo-ndc");
    log.warn("A test WARN message with NDC");
    NDC.pop();
    log.fatal("A test FATAL message with exception", new
Exception("fatal"));
*/

public class OrionAppender extends AppenderSkeleton {

    // NOTE: Most simple layouts are not useful for this Appender
because
    // the design of log4j is such that Layouts are responsible for
adding
    // line endings, and we require that the Layout not add line
endings.
    // This is IMHO a design flaw in log4j...

    // NOTE: Orion includes a "2/5/02 5:46 PM" format date in each log
entry.
    // NOTE: Orion doesn't generate unique names for each thread, so the
thread
    // name pattern entry is not much use.

    // NOTE: Using a hardcoded layout now, can make this configurable
later.
    // Basically it's "priority category [ndc]: message"
    private static final Layout sLayout = new PatternLayout(
            "%-5p %c{1} [%x]: %m");

    private Logger iLogger;

    public OrionAppender() {
        try {
            Context lContext = new InitialContext();
            Object lBoundObject = lContext.lookup("java:comp/Logger");
            iLogger = (Logger) PortableRemoteObject.narrow(lBoundObject,
                    Logger.class);
        } catch (Exception e) {
            errorHandler.error("Error finding Orion Logger for appender
[" +
                    name + "].", e, ErrorCode.GENERIC_FAILURE);
        }
    }

    public boolean requiresLayout() {
        // Tell the configurator we have our own hardcoded layout.
        // See PropertyConfigurator for how this method is used.
        return false;
    }

    protected void append(LoggingEvent event) {
        if (iLogger == null) {
            errorHandler.error("No Logger for appender [" + name +
"].");
            return;
        }
        try {
            LogEvent lEvent = null;
            String lMessage = sLayout.format(event);
            ThrowableInformation lInfo =
event.getThrowableInformation();
            if (lInfo == null) {
                lEvent = new LogEvent(lMessage);
            } else {
                lEvent = new LogEvent(lMessage, lInfo.getThrowable());
            }
            iLogger.log(lEvent);
        } catch (Exception lEx) {
            errorHandler.error("Could not log message in appender [" +
name
                    + "].", lEx, ErrorCode.GENERIC_FAILURE);
        }
    }

    public synchronized void close() {
        iLogger = null;
    }
}


Reply via email to