Logback Configuration Examples

In this post we will provide a few logback configuration examples.

Send logs to console

The first example shows how to send logs to the console, in order to achieve this you will need to use the ConsoleAppender.

Send logs to a file

The next example shows how to send logs to a file using the FileAppender.

Send logs to file with MDC

The following example demonstrates how to send logs to a file with MDC enabled. As you can see below it is very similar to the example above. The only difference is the pattern which include %X

Send logs to file with FixedWindowRollingPolicy and SizeBasedTriggeringPolicy

The final example sends logs to a file, it also rolls the log file once it has reached 500MB. Using the following logback.xml file will produce the following log files.

test.log, test.1.log.gz, test.2.log.gz and test.3.log.gz

I hope the above examples helps you configure logback for your application. If you require any other setup drop a message in the comments below.

Similar posts:

  1. Logback logging with MDC
  2. Java 9 JShell basics
  3. How to parse a JSON file using GSON
  4. Java 8 Streams flatMap examples

References:

  1. Logback Documentation

 

Leave a Reply

Your email address will not be published. Required fields are marked *