[logback-user] ContextSelector and getLogger

Martin Burchard martin.burchard at pentos.com
Thu Mar 5 23:10:54 CET 2009


Thank you, I'll try to understand and implement that.

Using this way, is it possible to have the Logger configuration also on a
per Thread basis?

I know this is also a little bit strange but, we have a lot of different
processes running.
Some do logging using the ALogAppender, some do logging using a
DocumentLogger, some do logging onto the console.
The possibility to configure all LoggerContext using the JoranConfigurator
was also a reason for implementing a ContextSelector.

Reading through Chapter 9: Context Selectors I have seen the
ContextJNDISelector and now it sounds to me, that ContextSelector isn't such
a good idea?


Ceki Gulcu wrote:
> 
> 
> 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
> _______________________________________________
> 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-tp22352655p22361882.html
Sent from the Logback User mailing list archive at Nabble.com.



More information about the Logback-user mailing list