[logback-user] ContextSelector and getLogger

Martin Burchard martin.burchard at pentos.com
Thu Mar 5 18:32:41 CET 2009


This one...

But the assumption is that I use a Context Selector so only Log events reach
my appender instance that belong to this appender instance.

package de.pentos.domino.logging;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import lotus.domino.Database;
import lotus.domino.Log;
import lotus.domino.NotesException;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.AppenderBase;
import de.pentos.domino.base.DominoHelper;
import de.pentos.domino.base.IProcess;
import de.pentos.domino.base.ProcessManager;

/**
 * @author Martin Burchard
 * @version $Revision: $
 */
public class ALogAppender<E> extends AppenderBase<E> {
	public static final String CVS_VERSION = "$Revision: $";
	private String dbPath;
	private Log nLog;
	private Database logDB;
	private final List<E> logCache = new ArrayList<E>();
	private static final String ERROR_WRITING_ALOG = "Error while writing
message in ALogAppender";
	private static final String ERROR_OBJECT_RECYCLING = "Object ALogAppender
recycled during finalize!";
	private boolean closed = false;

	@Override
	protected void append(final E event) {
		if (!closed) {
			logCache.add(event);
			writeCache();
		}
	}

	/**
	 * 
	 */
	private void cleanUP() {
		IProcess process = ProcessManager.getProcess();
		closed = true;
		try {
			nLog.close();
		} catch (NotesException e) {}
		process.disposeDB(logDB, this);
		DominoHelper.recycle(nLog);
		logDB = null;
		nLog = null;
	}

	/*
	 * (non-Javadoc)
	 * @see de.pentos.base.domino.backend.AbstractDominoContext#finalize()
	 */
	public void finalize() {
		if (!closed) {
			System.err.println(ERROR_OBJECT_RECYCLING);
			cleanUP();
		}
	}

	/**
	 * @return
	 */
	private boolean getNLog() {
		if (nLog == null) {
			try {
				IProcess process = ProcessManager.getProcess();
				logDB = process.getDB("", dbPath, this);
				nLog = process.getSession().createLog(process.getName());
				nLog.openNotesLog(logDB.getServer(), logDB.getFilePath());
				return true;
			} catch (Throwable th) {
				th.printStackTrace();
			}
			return false;
		}
		return true;
	}

	/**
	 * @param dbPath
	 */
	public void setDBPath(final String dbPath) {
		this.dbPath = dbPath;
	}

	/*
	 * (non-Javadoc)
	 * @see ch.qos.logback.core.AppenderBase#stop()
	 */
	@Override
	public void stop() {
		cleanUP();
		super.stop();
	}

	/**
	 * 
	 */
	private void writeCache() {
		if (getNLog()) {
			for (Iterator<E> it = logCache.iterator(); it.hasNext();) {
				E event = it.next();
				try {
					if (event instanceof LoggingEvent) {
						if (((LoggingEvent) event).getLevel().equals(Level.ERROR)) {
							nLog.logError(0, layout.doLayout(event));
						} else {
							nLog.logAction(layout.doLayout(event));
						}
					} else {
						nLog.logAction(layout.doLayout(event));
					}
				} catch (NotesException e) {
					System.err.println(ERROR_WRITING_ALOG);
					e.printStackTrace();
				}
			}
			logCache.clear();
		}
	}
}



Ceki Gulcu wrote:
> 
> 
> By the way, what type of appender are you using? Is it a custom made
> appender 
> for Domino?
> 
> -- 
> Ceki Gülcü
> Logback: The reliable, generic, fast and flexible logging framework for
> Java.
> http://logback.qos.ch
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://qos.ch/mailman/listinfo/logback-user
> 
> 

-- 
View this message in context: http://www.nabble.com/ContextSelector-and-getLogger-tp22352655p22356470.html
Sent from the Logback User mailing list archive at Nabble.com.



More information about the Logback-user mailing list