Shekhar Gulati is a Java Consultant with over 5 years experience.He is currently working with Xebia India an Agile Software Development company.The postings on this site and on his blog are his own and do not necessarily represent opinion of his employer. His own blog is at http://whyjava.wordpress.com/ and you can follow him on twitter here. Shekhar has posted 25 posts at DZone. View Full User Profile

Do You Know Log4j SoundAppender?

09.07.2010
| 7255 views |
  • submit to reddit

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.

Published at DZone with permission of its author, Shekhar Gulati.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

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: Chris Clark

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: Chris Clark

an electric shock appender?

Chris Clark replied on Tue, 2010/09/07 - 2:37pm in response to: Andrew McVeigh

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

Very nice :-D

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
done

Make it executable:


$ chmod +x speak-errors

Then tail your log into it:


$ tail -f log | ./speak-errors

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

This reminds me of the old police siren we had attached to a bunch of vital PRD servers, one would go down and the whole world would know about it :)

Khent Johnson replied on Fri, 2011/09/02 - 2:19pm

Excellent learning! This article thought me a lot about this Log4j logging utility. And I am so grateful you've shared this superb info to us. So, I am thinking to share a short additional info about log4j. Some people argue that log statements pollute source code and decrease legibility. In the Java language where a preprocessor is not available, log statements increase the size of the code and reduce its speed, even when logging is turned off. Given that a reasonably sized application may contain thousands of log statements, speed is of particular importance. With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary. More powers! GAR Labs

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.