In our previous section we have seen that we can append log events in simple file (plain/text) file and to the console also. If we want to write files then we have to use WriterAppender. WriterAppender appends log events to a Writer or an OutputStream depending on the user's choice.
In our example we have created a class WriterAppenderExample . First we need to create logger object and then we have to add appender to it. So we have created an object of WriterAppender and since it requires Writer or OutputStream we are using a file "Writer.html". We have taken layout of HTML format that therefore we have to use HTMLLayout.
logger.addAppender(writeappender); will add writerappender to logger and all logging events will be added to this HTML file.
Here is the example code for WriterAppenderExample class:
WriterAppenderExample.java
import java.io.*;
|
Output:
After compilation and execution of WriterAppenderExample.java will add the log events in "Writer.html" file in the HTML format as since we have used HTMLLayout() . Output will look like this.

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: WriterAppender in Log4j View All Comments
Post your Comment