1. Overview
In this article, we will learn the LogBack ConsoleAppender that sends logs to the console. The Logback library is a logging framework and a successor of the famous log4j project.
2. LogBack ConsoleAppender
In the first place, let’s add the LogBack library as a dependency in the project to use it.
We are using version 1.2.6. You can also refer to the Log Back release page for the latest version.
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.6</version> </dependency>
The above jar will also transitively pull in all other logback-core and slf4j-api dependencies:

By default, it uses the default configuration. So displays the logs in the console at DEBUG level. However, you can customize this behavior by defining the configuration files.
For example, 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; private static final Logger logger = LoggerFactory.getLogger(Runner.class); logger.debug("This is LogBack testing");

By default, the LogBack uses System.out
as the console target. But you can change this behavior by defining the configuration file.
3. Custom LogBack ConsoleAppender
You can configure the LogBack either using the XML or Groovy. The Log Back also automatically searches for the configuration file in the class path and configures itself.
You can name your file as any of the below:
- logback-test.xml
- logback.groovy
- logback.xml
Here, we will create a file with the name logback.xml and place it in the /src/main/resources
folder. The Spring automatically adds the file to the class path. Then the LogBack uses the file to configure itself.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} %-4relative %logger{35} - %msg %n</pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="STDOUT" /> </root> </configuration>
The ConsoleAppender
formats the log statements with the help of the encoder specified. The pattern inside the encoder defines the format.
3.1. LogBack ConsoleAppender target as error
By default, the ConsoleAppender uses the System.out
target to print logs to the console. However, you can change the target as System.err
by using the <target>
element as below:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} %-4relative %logger{35} - %msg %n</pattern> </encoder> <target>System.err</target> </appender> <root level="ERROR"> <appender-ref ref="STDERR" /> </root> </configuration>
We specified the root level as ERROR. so this outputs only the error logs to the console. Note that the info, trace, debug log statements will not appear on the console.
logger.debug("Debug"); logger.info("Info"); logger.error("Error"); logger.trace("trace");

If you change the root level to DEBUG, then it prints info, debug and error log statements to the console.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} %-4relative %logger{35} - %msg %n</pattern> </encoder> <target>System.err</target> </appender> <root level="DEBUG"> <appender-ref ref="STDERR" /> </root> </configuration>

4. Conclusion
To sum up, we have discussed the LogBack CustomAppender to send logs to the console and also to change the logging level.