[logback-user] ContextSelector and getLogger

Ceki Gulcu ceki at qos.ch
Thu Mar 5 19:22:31 CET 2009


Looking at the getNLog() method reproduced below, it seems like
discriminating appenders based on process name would be sufficient.

   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;
   }

The discriminator class I mentioned previously could be modified as:

public class ProcessBasedDiscriminator implements Discriminator<LoggingEvent>{

   public String getDiscriminatingValue(LoggingEvent e) {
     IProcess process = ProcessManager.getProcess();
     return process.getName();
   }

   public String getKey() {
     // always use the same fixed key
     return "threadId";
   }

With the help ProcessBasedDiscriminator just described and
SiftingAppender, you could have *one* ALogAppender instance per domino
*process* regardless of the containing Java thread. As far as I could
tell, that would be quite an elegant solution would it not?

Martin Burchard wrote:
> 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
>>
>>
> 

-- 
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for Java.
http://logback.qos.ch


More information about the Logback-user mailing list