Fun with Java: Log4j appender using notify-send on ubuntu

January 18th, 2010

I’ve discovered yet another -maybe quite useless- trick to combine java-development, JBoss AS and Ubuntu (or any other debian-based distro).

I created a very simple log4j-appender which used notify-send (commandline-tool for libnotify) to report errors. This way I will get a visual notification when something went wrong, immediately showing the class of the error and the message.

As an example, I use my favorite Application Server, JBoss AS, but any java-app using log4j (or commons-logging on top of log4j or slf4j-log4j) will do.

Step 1

Make sure notify-send is installed on your machine. You can check this by running this command in your terminal:

notify-send -i info "Hello world"

Step 2

Place the notify-send-log4j.jar in the classpath of your application. In case of JBoss, it’s in the node’s /lib/ folder.

Step 3

Configure Log4j to use the appender. For JBoss, the jboss-log4j.xml is found in the server-node’s /conf/ folder. Add a new appender, at ERROR-level using onlyOnceErrorhandler:

<appender name="SEND-NOTIFY"
  	 class="be.stacktrace.log4j.notifysend.NotifySendAppender">
     <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
     <param name="Threshold" value="ERROR"/>
</appender>

Add the appender to the root-category:

<root>
     <appender-ref ref="CONSOLE"/>
     <appender-ref ref="FILE"/>
     <appender-ref ref="SEND-NOTIFY"/>
</root>

Step 4

Cause an error :D

Step 5 (optional)

If you’re using compiz, you can add a custom effect to the notification-popup. For example, use the ‘Burn’-effect when appearing (see screenshot on top of post):

  • Open compiz-config
  • Open “Animations” in tab “Animate when opening”
  • Add new animation, use window-match: (type=Notification)

Categories: Development, Java, Linux, Uncategorized | Tags: , , , ,

1 Comment

  1. Joram

    This is cool stuff! Not quite sure about its usefulness in reality, but nonetheless cool ;-)

Leave a comment