Posted by : Munish Gogna Saturday, January 22, 2011

This post is in continuation of my earlier post - "using log4j for various logging events"

We all are familiar with log4j (if you are not then you can read out here). We all know about different types of appenders available like - File Appender, Console Appender, JDBC Appender, Socket Appender, Telnet Appender etc., but most of us might not know that there is another 'Sound Appender' available which can play wav sound formats when configured log events are fired during runtime.


SoundAppender is a another Log4J appender which play an audio clip created using Applet.newAudioClip when an logging event is received. You can use filters in combination with this appender to control when the appender is trigger.

Let's configure a sample maven Project to test Sound Appender:

 4.0.0

 com.mg.log4j.sound
 log4j_sound
 0.0.1-SNAPSHOT
 jar

 log4j_sound
 http://maven.apache.org
 
  
   Munish Gogna
   http://gognamunish.blogspot.com
   gognamunish@gmail.com
   +8:00 GMT
  
 

 
  UTF-8
 

 
  
   log4j
   apache-log4j-extras
   1.0
  
  
   junit
   junit
   4.0
   test
  
 

Next we will define the SoundAppender in log4j.xml as shown below:


 
  
   
 

 
  
 


SoundAppender 'gameOver' takes a parameter called audioURL (url of the audio file which will be played). LevelMatchFilter decides whether to accept the LoggingEvent or not, I have configured this logger to accept only ERROR event. Now, that we have configured SoundAppender in log4j.xml, we should add log statement in a Java class and see what happens:

package com.mg.log4j.sound;

import org.apache.log4j.Logger;

/**
 * Game Over Man !!!
 * 
 */
public class SoundAppender {

 private static Logger log = Logger.getLogger(SoundAppender.class);

 public static void main(String[] args) throws Exception {
  try {
   somethingSilly();
  } catch (GameOverException e) {
   log.error("Game over man... game over !!!");
   // just to let sound complete
   Thread.sleep(10000);

  }
 }

 private static void somethingSilly() throws GameOverException {
  System.out.println("Doing some silly work !!!");
  throw new GameOverException();
 }

 static class GameOverException extends Exception {
  private static final long serialVersionUID = 1L;

  GameOverException() {
   super();
  }
 }
}

Naturally any one's game will be over when something very serious happens in production, so if you have written some code that can fail in near future you must run this code :)

Now, each time you run this class a sound clip will be played at the log statement.
Now only thing that you need to do is replace audioURL in log4j.xml to point to 'sheela ki jawani.wav' that you might have on your computer somewhere (or can download it from here also)

That's all. Enjoy Sheela ki Jawani with log4j.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Labels

enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)