Do You Know Log4j SoundAppender?
Today, I was looking at the maven dependencies of one of my projects and found a jar called apache-log4j-extras. I fired the maven dependency:tree command to find out where this jar was getting picked up from. I found out that this jar was referred from a third party jar. I decided to take a look at the classes inside this jar and found an interesting class called SoundAppender. SoundAppender is a 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 SoundAppender.
Before you start using this appender, you should add apache-log4j-extras dependency in your project. For maven users,
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.0</version>
</dependency>
Once you have added apache-log4j-extras dependency to your project, you should define the SoundAppender in log4j.xml as shown below
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appender" class="org.apache.log4j.varia.SoundAppender">
<param name="audioURL"
value="http://www.nch.com.au/acm/11k16bitpcm.wav"></param>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />
</filter>
</appender>
<root>
<appender-ref ref="appender" />
</root>
</log4j:configuration>
So, I have defined a SoundAppender which takes a parameter called audioURL (url of the audio file). I have also specified a LevelMatchFilter which matches the value of the LevelToMatch option and the level of the LoggingEvent, and then decides whether to accept the LoggingEvent or not. Now, that we have configured SoundAppender in log4j.xml, we should add log statement in a Java class.
public class ExampleService {
private static final Logger logger = Logger.getLogger(ExampleService.class);
public String getMessage() {
logger.info("Hello World");
return "Hello world!";
}
}Now, lets test this with a JUnit test case
public class ExampleServiceTests extends TestCase {
private ExampleService service = new ExampleService();
public void testReadOnce() throws Exception {
assertEquals("Hello world!", service.getMessage());
Thread.sleep(10000);
}
}Now, each time you run the test, a sound clip will be played at the log statement. I don't know if this appender has any practical usecase but I found it interesting to share.
- Login or register to post comments
- 5315 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Wujek Srujek replied on Tue, 2010/09/07 - 9:22am
This article just made my day ;d
There is a use case - configure the appended to log only errors or whatever, and use it together with another appender (like file), start a test run and go over to a collegaue to talk about his / her problems, or just chat. Now, whenever there is an error being logged, you will know it and can come back to check.
Gerard COLLIN replied on Tue, 2010/09/07 - 9:21am
Or maybe have an alarm sound to alert support people something is happening on the server.
Oups, I'm not sure they have access to the server sound....
Gérard.
Chris Clark replied on Tue, 2010/09/07 - 9:45am
Sorry, but I can't resist it...
How about a SmellAppender so that when a fatal error is logged, a smell of burning electrical equipment is generated?
Wujek Srujek replied on Tue, 2010/09/07 - 10:04am
in response to: cc1590
Looks like you didn't get the irony.
Or maybe I didn't get yours!
Andrew McVeigh replied on Tue, 2010/09/07 - 10:12am
in response to: cc1590
Chris Clark replied on Tue, 2010/09/07 - 2:37pm
in response to: andrewm
I think you I hear the global screams of delight from numerous BOFHs on that one, Andrew :-)
Careful what you wish for ;-)
Cyril Karpenko replied on Tue, 2010/09/07 - 3:35pm
Justin Forder replied on Tue, 2010/09/07 - 10:28pm
If you are using Mac OS X, the say command lets you convert text to speech from the command line. Piping directly into the say command doesn't seem to work, but a small script can be used to call say with each line to be spoken.
For example, to have any log lines beginning with 'ERROR' spoken as soon as they appear in the log, create a small bash script, speak-errors, with this content:
while read line do if [[ $line =~ ^ERROR ]] then say $line fi doneMake it executable:
Then tail your log into it:
Have fun!
Cosmin Mutu replied on Wed, 2010/09/08 - 1:14am
well, if you think about it, microsoft implemented this into windows ... you remember those annoying sounds you get every time you get an error? :) ... ofc, everybody turns those off, but anyway :)
so, at some point, in a desktop application, this sound appender could come in handy ;)
Tim O'farrell replied on Wed, 2010/09/08 - 3:53am
I coded something like this before at a SAAS company I used to work at. The product had some stability problems which I was tasked with tracking down. One of the first steps was monitoring the health of the system in real time. I wrote a quick client app that would monitor various system stats (Memory, average response times, usage, etc..), and display it on a graph on a screen in the support center. When something went wrong, it would sound the "Red Alert" alarm from Star Trek. That lasted all of about an hour before the staff started to complain, so I changed it to the sound of a bird tweeting - they called it "the canary"!!!
Funny thing was it lasted a lot longer than I intended - when I left they were still using it...
Martijn Verburg replied on Wed, 2010/09/08 - 4:01am
Khent Johnson replied on Fri, 2011/09/02 - 2:19pm