DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Do You Know Log4j SoundAppender?

Do You Know Log4j SoundAppender?

Shekhar Gulati user avatar by
Shekhar Gulati
·
Sep. 07, 10 · Java Zone · Interview
Like (0)
Save
Tweet
8.30K Views

Join the DZone community and get the full member experience.

Join For 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.

Log4j

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Handle Early Startup Technical Debt (Or Just Avoid it Entirely)
  • Testing Schema Registry: Spring Boot and Apache Kafka With JSON Schema
  • Message Queuing and the Database: Solving the Dual Write Problem
  • How Dynamic Rendering Works Using HTML and CSS?

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo