Logback Console Appender

Logback ConsoleAppender appends on the console though System.out or System.err print streams. In this Logback tutorial, learn about default configured console logging and how to apply custom configuration.

1. Dependencies

Logback requires three modules in the application runtime i.e. logback-core, logback-classic and slf4j-api.

<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-core</artifactId>
	<version>1.2.10</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.10</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.7.32</version>
</dependency>

The logback-classic module has implemented the SLF4J API natively so there is no extra cost when invoking an SLF4J logger to log messages using the underlying Logback library.

Note that logback-classic will automatically pull in the logback-core and slf4j-api, so adding logback-classic dependency is enough.

2. Zero Configuration Console Logging

By default, if we do not provide any configuration and add the dependencies in the project, Logback automatically configures the console appender and outputs the logs in the console.

The default log level set is DEBUG. It means all INFO, WARN, ERROR and DEBUG messages will be printed in the console.

The following configuration is an equivalent configuration done by Logback, in case no custom configuration file is found in the classpath.

<configuration>
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
		</encoder>
	</appender>

	<root level="DEBUG">
		<appender-ref ref="STDOUT" />
	</root>
</configuration>
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.howtodoinjava.demo.lombok.Article;

public class Main {
	public static void main(final String[] args) {
		Logger logger = LoggerFactory.getLogger(Main.class);

		logger.debug("Debug Message Logged !!!");
		logger.info("Info Message Logged !!!");
		logger.error("Error Message Logged !!!", new NullPointerException("Something is NULL"));

		Article a = Article.builder(1L).title("Test Article").tag("Data").build();
		logger.info("Article fecthed for id : {} is : {}", 1, a);
	}
}
15:23:27.810 [main] DEBUG com.howtodoinjava.demo.slf4j.Main - Debug Message Logged !!!
15:23:27.812 [main] INFO  com.howtodoinjava.demo.slf4j.Main - Info Message Logged !!!
15:23:27.813 [main] ERROR com.howtodoinjava.demo.slf4j.Main - Error Message Logged !!!
java.lang.NullPointerException: Something is NULL
	at com.howtodoinjava.demo.slf4j.Main.main(Main.java:14)
15:23:27.814 [main] INFO  com.howtodoinjava.demo.slf4j.Main - Article fecthed for id : 1 is : Article(id=1, title=Test Article, tags=[Data])

3. Custom Configuration

3.1. Log Level and Pattern

  • We can customize the log message pattern with the help of an encoder tag.
  • We can change the default target from System.outto System.err using the target attribute.
  • To change the log level, change the attribute level to any other Log level such as INFO, WARN or ERROR.

Here is a sample configuration after changes:

<configuration>
  <appender name="STDERR" class="ch.qos.logback.core.ConsoleAppender" target="System.err">
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="STDERR" />
  </root>
</configuration>

3.2. Color-coded Logs

To enable color-coded logs, set the withJansi property set to true. By default, it is set to false.

Note that Unix-based operating systems such as Linux and Mac OSX support ANSI color codes by default. On Windows, we must add jansi dependency in the runtime.

<dependency>
    <groupId>org.fusesource.jansi</groupId>
    <artifactId>jansi</artifactId>
    <version>2.4.0</version>
</dependency>

Spring boot automatically detects and configure the color coded logs. We can further customize it using the property spring.output.ansi.enabled by setting it ALWAYS, NEVER or DETECT (default value).

If we do not want to add the dependency in the project, rather make changes in IDE so they are purely in the development environment, we can use the ANSI in Eclipse Console plugin.

Finally, use a color encoding enabled encoder pattern.

<encoder>
  <pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{15}) - %msg %n</pattern>
</encoder>

See the colored encoded logs in the console.

4. Conclusion

The default configured console logging is good enough for getting started and POC purposes.

To use extensive and more detailed logging in console, we can customize the log level, pattern and even can add the color coding to messages.

Happy Learning !!

Download Sourcecode

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode