1. Overview
In this article, we will learn the LogBack File Appender that sends the log statements to a file. The logging framework Logback library is a successor to the famous log4j project.
2. LogBack File Appender
The LogBack File Appender outputs the log statements to a file. You can specify the target file location by using the File option. It is a sub-class of the OutputStreamAppender
class.
In order to work with LogBack File appender, you first need to add the LogBack library as a dependency in your project.
Here, we are using version 1.2.6. However, you can refer to the Log Back release page for the latest version and use it accordingly.
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.6</version> </dependency>

Adding the logback-classic jar will transitively pull in all other required jars logback-core and slf4j-api into the project.
If you don’t explicitly specify the configuration file, then the LogBack uses a default configuration and displays the logs in the console at DEBUG level.
However, you can customize this behavior by defining the configuration files.
Let’s see an example for LogBack FileAppender.
3. LogBack File appender example
You can configure the LogBack either using the XML or Groovy. The Log Back also automatically searches for the configuration files in the class path and configures itself.
You can create any of the below files to configure the File Appender:
- logback-test.xml
- logback.groovy
- logback.xml
Here, we created the configuration file logback.xml and placed it in the /src/main/resources
folder. The Spring adds the file automatically to the class path. Then the LogBack uses the file to configure itself.
<configuration> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>D:\\log\\output.log</file> <append>false</append> <immediateFlush>true</immediateFlush> <encoder> <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="FILE" /> </root> </configuration>
The above configuration file uses the FileAppender class. We have also specified the target file path along with the name by using the <file> element. Since we specified the <append> value as true, the log statement appends to the file.
If you specify the append
value as false, then the existing contents in the file are deleted and overwritten.
The immediateflush property controls the delivery of the logs to the destination. If set to true, it delivers the log statements to the file after each method call. By default, it is true. You can also set it to false to deliver logs to the file in batches.
Let’s create a Logger by using the slf4j LoggerFactory class. Later, you can call the log statements using the Logger instance.
import org.slf4j.Logger; import org.slf4j.LoggerFactory; logger.debug("Debug"); logger.info("Info"); logger.error("Error"); logger.trace("trace");

4. Conclusion
To sum up, we have seen the use of LogBack File Appender class to output the logs to a file.
Pingback: LogBack RollingFileAppender example - TedBlob