Short tip: NotesExceptionRenderer for Log4J

Ever tried the Log4J logging package, and wanted your NotesExceptions to render properly in the log? As you might now, the NotesException class doesn’t implement the getMessage() method (stupid L*tus), and you end up doing a lot of:

... } catch (NotesException e) { logger.error(e, e.toString() + ", " + + ", " + e.text); }

when you want to do a simple:

... } catch (NotesException e) { logger.error(e, e); }

There is a cure for this: the org.apache.log4j.or.ObjectRenderer interface of the Log4J package. An ObjectRenderer is a renderer of objects to Strings, and can be used to format known (and unknown) objects with the doRender(Object) method. Log4J keeps a map of all renderer objects in the org.apache.log4j.or.RendererMap, and we can use it to add our own ObjectRenderer for the NotesException.

The attached source of the NotesExceptionRenderer can be added to your script library or agent, and all you have to do to get it working is a few lines to your Log4J config file / view (please note that it should be on a single line / entry):

log4j.renderer.lotus.domino.NotesException = net.kanngard.util.logging.NotesExceptionRenderer

Or, you could add it manually in an agent with:

 import org.apache.log4j.LogManager; import org.apache.log4j.spi.RendererSupport; import org.apache.log4j.or.RendererMap; ... RendererMap.addRenderer( (RendererSupport) LogManager.getLoggerRepository(), "lotus.domino.NotesException", "net.kanngard.util.logging.NotesExceptionRenderer");