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

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}