Over a million developers have joined DZone.
Platinum Partner

Do You Know Log4j SoundAppender?

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

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,


 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"

<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="WARN" />


<appender-ref ref="appender" />


 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());


 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.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}