Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Do You Know Log4j SoundAppender?

DZone's Guide to

Do You Know Log4j SoundAppender?

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}